-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(shared/hooks): 抽取 transition 的 useChildren 为公共 hook useValueWith…
…Prev
- Loading branch information
1 parent
79d5402
commit 331730d
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/shared/src/hooks/__tests__/useValueWithPrev.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |