From 331730d3db92b02b66264912bb7a34693cd3875d Mon Sep 17 00:00:00 2001 From: dyh_a Date: Sat, 16 Sep 2023 09:18:31 +0800 Subject: [PATCH] =?UTF-8?q?test(shared/hooks):=20=E6=8A=BD=E5=8F=96=20tran?= =?UTF-8?q?sition=20=E7=9A=84=20useChildren=20=E4=B8=BA=E5=85=AC=E5=85=B1?= =?UTF-8?q?=20hook=20useValueWithPrev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooks/__tests__/useValueWithPrev.test.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 packages/shared/src/hooks/__tests__/useValueWithPrev.test.ts diff --git a/packages/shared/src/hooks/__tests__/useValueWithPrev.test.ts b/packages/shared/src/hooks/__tests__/useValueWithPrev.test.ts new file mode 100644 index 00000000..4df591c1 --- /dev/null +++ b/packages/shared/src/hooks/__tests__/useValueWithPrev.test.ts @@ -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(); + 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); + }); +});