Skip to content

Commit

Permalink
Merge pull request #88 from mainmatter/add-perform-count
Browse files Browse the repository at this point in the history
feat: add performCount to derived state
  • Loading branch information
paoloricciuti authored May 24, 2024
2 parents 2feb6db + 68cd3c1 commit 228e0a9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/lib/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function _task<TArgs = undefined, TReturn = unknown>(
last_successful: undefined as undefined | TReturn,
error: undefined as undefined | unknown,
results,
performCount: 0,
});

const abort_controllers = new Set<{ controller: AbortController; listener: () => void }>();
Expand Down Expand Up @@ -111,6 +112,7 @@ function _task<TArgs = undefined, TReturn = unknown>(
() => {
result.update((old) => {
old.is_loading = true;
old.performCount++;
return old;
});
queueMicrotask(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tests/components/default.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
export let return_value: (value: unknown) => void = () => {};
export let argument = 0;
const default_task = task.default(fn);
const options_task = task(fn, { kind: 'default' });
export const default_task = task.default(fn);
export const options_task = task(fn, { kind: 'default' });
let latest_task_instance: ReturnType<typeof default_task.perform>;
let latest_options_task_instance: ReturnType<typeof options_task.perform>;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tests/components/drop.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
export let argument = 0;
export let max = 1;
const default_task = task.drop(fn, { max });
const options_task = task(fn, { kind: 'drop', max });
export const default_task = task.drop(fn, { max });
export const options_task = task(fn, { kind: 'drop', max });
let latest_task_instance: ReturnType<typeof default_task.perform>;
let latest_options_task_instance: ReturnType<typeof options_task.perform>;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tests/components/enqueue.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
export let argument = 0;
export let max = 1;
const default_task = task.enqueue(fn, { max });
const options_task = task(fn, { kind: 'enqueue', max });
export const default_task = task.enqueue(fn, { max });
export const options_task = task(fn, { kind: 'enqueue', max });
let latest_task_instance: ReturnType<typeof default_task.perform>;
let latest_options_task_instance: ReturnType<typeof options_task.perform>;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tests/components/keep_latest.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
export let argument = 0;
export let max = 1;
const default_task = task.keepLatest(fn, { max });
const options_task = task(fn, { kind: 'keepLatest', max });
export const default_task = task.keepLatest(fn, { max });
export const options_task = task(fn, { kind: 'keepLatest', max });
let latest_task_instance: ReturnType<typeof default_task.perform>;
let latest_options_task_instance: ReturnType<typeof options_task.perform>;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tests/components/restart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
export let argument = 0;
export let max = 1;
const default_task = task.restart(fn, { max });
const options_task = task(fn, { kind: 'restart', max });
export const default_task = task.restart(fn, { max });
export const options_task = task(fn, { kind: 'restart', max });
let latest_task_instance: ReturnType<typeof default_task.perform>;
let latest_options_task_instance: ReturnType<typeof options_task.perform>;
Expand Down
60 changes: 54 additions & 6 deletions src/lib/tests/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,42 @@ function all_options(fn: (selector: string) => void) {
}

describe.each([
{ component: Default, name: 'default' },
{ component: Enqueue, name: 'enqueue' },
{ component: Drop, name: 'drop' },
{ component: KeepLatest, name: 'keepLatest' },
{ component: Restart, name: 'restart' },
])('task - basic functionality $name', ({ component }) => {
{
component: Default,
name: 'default',
perform_count: {
expected: 5,
},
},
{
component: Enqueue,
name: 'enqueue',
perform_count: {
expected: 5,
},
},
{
component: Drop,
name: 'drop',
perform_count: {
expected: 1,
},
},
{
component: KeepLatest,
name: 'keepLatest',
perform_count: {
expected: 2,
},
},
{
component: Restart,
name: 'restart',
perform_count: {
expected: 5,
},
},
])('task - basic functionality $name', ({ component, perform_count }) => {
all_options((selector) => {
it('calls the function you pass in', async () => {
const fn = vi.fn();
Expand Down Expand Up @@ -118,6 +148,24 @@ describe.each([
});
expect(passed_in_value).toBe(argument);
});

it('has the correct derived state for performCount', async () => {
const fn = vi.fn();
const { getByTestId, component: instance } = render(component, {
fn,
});
const store = instance[`${selector}_task`] as Task;
const perform = getByTestId(`perform-${selector}`);
perform.click();
perform.click();
perform.click();
perform.click();
perform.click();
await vi.waitFor(() => {
expect(fn).toHaveBeenCalledTimes(perform_count.expected);
});
expect(get(store).performCount).toBe(perform_count.expected);
});
});

it('re-throws any error thrown in the perform function and has the error in the error field of the store', async () => {
Expand Down

0 comments on commit 228e0a9

Please sign in to comment.