Skip to content

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed Mar 29, 2024
1 parent ad955e1 commit 7e757cf
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
21 changes: 18 additions & 3 deletions src/lib/tests/components/default.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
import { task, type SvelteConcurrencyUtils } from '../../task';
export let fn: (
args: unknown,
args: number,
utils: SvelteConcurrencyUtils,
) => Promise<unknown> | AsyncGenerator<unknown, unknown, unknown>;
export let return_value: (value: unknown) => void = () => {};
export let argument = 0;
const default_task = task(fn);
let latest_task_instance: ReturnType<typeof default_task.perform>;
</script>

<button
data-testid="perform-default"
on:click={() => {
default_task.perform();
on:click={async () => {
latest_task_instance = default_task.perform(argument);
return_value(await latest_task_instance);
}}>perform</button
>

Expand All @@ -22,3 +28,12 @@
default_task.cancelAll();
}}>cancel</button
>

<button
data-testid="cancel-default-last"
on:click={() => {
if (latest_task_instance) {
latest_task_instance.cancel();
}
}}>cancel last instance</button
>
77 changes: 70 additions & 7 deletions src/lib/tests/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import type { SvelteConcurrencyUtils } from '$lib/task';
import { render } from '@testing-library/svelte';
import { describe, expect, it, vi } from 'vitest';
import Task from './components/default.svelte';
import Default from './components/default.svelte';

function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

describe('task', () => {
describe('task - default handler', () => {
it('calls the function you pass in', async () => {
const fn = vi.fn();
const { getByTestId } = render(Task, {
const { getByTestId } = render(Default, {
fn,
});
const perform = getByTestId('perform-default');
Expand All @@ -31,7 +31,7 @@ describe('task', () => {
yield;
count++;
}
const { getByTestId } = render(Task, {
const { getByTestId } = render(Default, {
fn,
});
const perform = getByTestId('perform-default');
Expand All @@ -50,13 +50,13 @@ describe('task', () => {
let count = 0;
const wait_time = 2000;
let task_signal: AbortSignal;
async function* fn(_: unknown, { signal }: SvelteConcurrencyUtils) {
async function* fn(_: number, { signal }: SvelteConcurrencyUtils) {
task_signal = signal;
await wait(wait_time);
yield;
count++;
}
const { getByTestId } = render(Task, {
const { getByTestId } = render(Default, {
fn,
});
const perform = getByTestId('perform-default');
Expand All @@ -72,7 +72,7 @@ describe('task', () => {

it('runs multiple time if performed multiple time', async () => {
const fn = vi.fn();
const { getByTestId } = render(Task, {
const { getByTestId } = render(Default, {
fn,
});
const perform = getByTestId('perform-default');
Expand All @@ -83,4 +83,67 @@ describe('task', () => {
expect(fn).toHaveBeenCalledTimes(3);
});
});

it("if awaited returns the value it's returned from the function", async () => {
const returned_value = 42;
const fn = vi.fn(async () => {
return returned_value;
});
const return_value = vi.fn();
const { getByTestId } = render(Default, {
fn,
return_value,
});
const perform = getByTestId('perform-default');
perform.click();
await vi.waitFor(() => {
expect(fn).toHaveBeenCalled();
});
expect(return_value).toHaveBeenCalledWith(returned_value);
});

it('passing a value to perform passes the same value as the first argument of the function', async () => {
let passed_in_value = 0;
const argument = 42;
const fn = vi.fn(async (value) => {
passed_in_value = value;
});
const { getByTestId } = render(Default, {
fn,
argument,
});
const perform = getByTestId('perform-default');
perform.click();
await vi.waitFor(() => {
expect(fn).toHaveBeenCalled();
});
expect(passed_in_value).toBe(argument);
});

it("cancel the last instance if you call cancel on the returned instance, the function is a generator and there's a yield after every await", async () => {
let count = 0;
const wait_time = 2000;
let task_signal: AbortSignal;
async function* fn(_: number, { signal }: SvelteConcurrencyUtils) {
task_signal = signal;
await wait(wait_time);
yield;
count++;
}
const { getByTestId } = render(Default, {
fn,
});
const perform = getByTestId('perform-default');
const cancel = getByTestId('cancel-default-last');
perform.click();
perform.click();
perform.click();
await wait(500);
cancel.click();
await vi.waitFor(() => {
expect(task_signal.aborted).toBeTruthy();
});
await wait(wait_time);
expect(count).toBe(2);
});
});

0 comments on commit 7e757cf

Please sign in to comment.