Skip to content

Commit

Permalink
chore: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticsoul committed Nov 25, 2023
1 parent 0c4449a commit 57ea46a
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/helux/__tests__/hooks/useActionLoading.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import '@testing-library/jest-dom';
import { act, renderHook } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import { atom, atomActionAsync, useActionLoading } from '../helux';
import { delay } from '../util';

describe('useDerivedAtom', () => {
async function testLoading(asyncAction, useActionLoading) {
const action1 = asyncAction(async ({ setState }) => {
await delay(100);
setState((draft) => {
draft.val = 100;
});
}, 'action1');

let runCount = 0;
const { result } = renderHook(() => {
runCount += 1;
const [ld] = useActionLoading();
return ld['action1'].loading;
});

expect(runCount).toBe(1);
expect(result.current).toBe(false);
act(() => {
action1();
});
expect(runCount).toBe(2);
expect(result.current).toBe(true); // loading
await delay(120);
expect(runCount).toBe(3);
expect(result.current).toBe(false); // loading canceled
}

test('ctx.asyncAction, ctx.useActionLoading', async () => {
const [, , ctx] = atom(1);
testLoading(ctx.actionAsync, ctx.useActionLoading);
});

test('topApi.asyncAction, ctx.useActionLoading', async () => {
const [numAtom, , ctx] = atom(1);
testLoading(atomActionAsync(numAtom), ctx.useActionLoading);
});

test('ctx.asyncAction, topApi.useActionLoading', async () => {
const [numAtom, , ctx] = atom(1);
testLoading(ctx.actionAsync, () => useActionLoading(numAtom));
});

test('topApi.asyncAction, topApi.useActionLoading', async () => {
const [numAtom] = atom(1);
testLoading(atomActionAsync(numAtom), () => useActionLoading(numAtom));
});
});
77 changes: 77 additions & 0 deletions packages/helux/__tests__/hooks/useDerivedAtom.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import '@testing-library/jest-dom';
import { act, renderHook } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import { atom, deriveAtom, useDerivedAtom } from '../helux';
import { delay } from '../util';

describe('useDerivedAtom', () => {
test('read and change', async () => {
const [numAtom, setAtom] = atom(1);
const plus1Result = deriveAtom(() => numAtom.val + 1);

const { result } = renderHook(() => {
const [plus1] = useDerivedAtom(plus1Result);
return plus1;
});

expect(result.current).toBe(2);
act(() => {
setAtom(2);
});
expect(result.current).toBe(3);
});

test('read async and change', async () => {
const [numAtom, setAtom] = atom(1);
const plus1Result = deriveAtom({
fn: () => numAtom.val + 1,
task: async () => {
await delay(100);
return numAtom.val + 100;
},
});

let runCount = 0;
const { result } = renderHook(() => {
runCount += 1;
const [plus1] = useDerivedAtom(plus1Result);
return plus1;
});

expect(result.current).toBe(2);
expect(runCount).toBe(1);
act(() => {
setAtom(2);
});
await delay(120);
expect(result.current).toBe(102); // returned by task
expect(runCount).toBe(3); // loading wil trigger one time render
});

test('read async, set showLoading=false', async () => {
const [numAtom, setAtom] = atom(1);
const plus1Result = deriveAtom({
fn: () => numAtom.val + 1,
task: async () => {
await delay(100);
return numAtom.val + 100;
},
});

let runCount = 0;
const { result } = renderHook(() => {
runCount += 1;
const [plus1] = useDerivedAtom(plus1Result, { showLoading: false });
return plus1;
});

expect(result.current).toBe(2);
expect(runCount).toBe(1);
act(() => {
setAtom(2);
});
await delay(120);
expect(result.current).toBe(102); // returned by task
expect(runCount).toBe(2); // now only render 2 times
});
});
41 changes: 41 additions & 0 deletions packages/helux/__tests__/hooks/useService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import '@testing-library/jest-dom';
import { act, renderHook } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import { atom, useAtom, useService } from '../helux';

describe('useService', () => {
test('read and change', async () => {
const [numAtom, setAtom] = atom(1);

let renderCount = 0;
const { result } = renderHook(() => {
renderCount += 1;
useAtom(numAtom);
const srv = useService({
a() {
return 'stable fn q';
},
b() {
return 'stable fn b';
},
});
// srv and its sub methods is stable
return srv;
});

expect(renderCount).toBe(1);
const prevSrv = result.current;
const prevFnA = prevSrv.a;
const prevFnB = prevSrv.b;
act(() => {
setAtom(Date.now());
});
expect(renderCount).toBe(2);
const currSrv = result.current;
const currFnA = currSrv.a;
const currFnB = currSrv.b;
expect(prevSrv).toBe(currSrv);
expect(prevFnA).toBe(currFnA);
expect(prevFnB).toBe(currFnB);
});
});

0 comments on commit 57ea46a

Please sign in to comment.