Skip to content

Commit

Permalink
test(shared/hooks): 抽取 transition 的 useChildren 为公共 hook useValueWith…
Browse files Browse the repository at this point in the history
…Prev
  • Loading branch information
mengxinssfd committed Sep 16, 2023
1 parent 79d5402 commit 331730d
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/shared/src/hooks/__tests__/useValueWithPrev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { renderHook, act } from '@testing-library/react';
import { useValueWithPrev } from '@pkg/shared';
import { useState } from 'react';

describe('useValueWithPrev', () => {
it('should work', () => {
let values: any[] = [];
const hook = renderHook(() => {
const [child, setChild] = useState<unknown>();
const children = useValueWithPrev(child);
values = children;
return [children, setChild] as const;
});

const [, setChild] = hook.result.current;

expect(hook.result.current[0]).toEqual([undefined, undefined]);
expect(hook.result.current[0]).toEqual(values);

act(() => setChild(true));
expect(hook.result.current[0]).toEqual([true, undefined]);
expect(hook.result.current[0]).toEqual(values);

// 同样的元素不会替换
act(() => setChild(true));
expect(hook.result.current[0]).toEqual([true, undefined]);
expect(hook.result.current[0]).toEqual(values);

act(() => setChild(false));
expect(hook.result.current[0]).toEqual([false, true]);
expect(hook.result.current[0]).toEqual(values);

act(() => setChild(false));
expect(hook.result.current[0]).toEqual([false, true]);
expect(hook.result.current[0]).toEqual(values);

act(() => setChild(false));
expect(hook.result.current[0]).toEqual([false, true]);
expect(hook.result.current[0]).toEqual(values);
});
});

0 comments on commit 331730d

Please sign in to comment.