Skip to content

Commit

Permalink
test: check the lifecycle module test case (alibaba#1129)
Browse files Browse the repository at this point in the history
Co-authored-by: leihao <leihao@xiaoduotech.com>
  • Loading branch information
rayhomie and leihao authored Sep 8, 2021
1 parent 690142e commit e0ca12a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 31 deletions.
11 changes: 9 additions & 2 deletions packages/hooks/src/useDebounceEffect/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,17 @@ describe('useDebounceEffect', () => {
expect(mockCleanUp.mock.calls.length).toEqual(0);

await sleep(300);
hook.unmount();

expect(mockEffect.mock.calls.length).toEqual(1);
expect(mockCleanUp.mock.calls.length).toEqual(0);

hook.rerender(2);
await sleep(300);
expect(mockEffect.mock.calls.length).toEqual(2);
expect(mockCleanUp.mock.calls.length).toEqual(1);

hook.unmount();
expect(mockEffect.mock.calls.length).toEqual(2);
expect(mockCleanUp.mock.calls.length).toEqual(2);
});
});
});
3 changes: 3 additions & 0 deletions packages/hooks/src/useMount/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ describe('useMount', () => {
expect(fn).toBeCalledTimes(1);
hook.unmount();
expect(fn).toBeCalledTimes(1);

renderHook(() => useMount(fn)).unmount();
expect(fn).toBeCalledTimes(2);
});
});
44 changes: 23 additions & 21 deletions packages/hooks/src/useThrottleEffect/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,42 @@ describe('useThrottleEffect', () => {
});

it('useThrottleEffect should work', async () => {
let mountedState = 1;
const mockEffect = jest.fn(() => {});
const mockCleanUp = jest.fn(() => {});
act(() => {
hook = renderHook(() =>
useThrottleEffect(
() => {
mockEffect();
return () => {
mockCleanUp();
};
},
[mountedState],
{ wait: 200 },
),
hook = renderHook(
({ value, wait }) =>
useThrottleEffect(
() => {
mockEffect();
return () => {
mockCleanUp();
};
},
[value],
{ wait },
),
{ initialProps: { value: 1, wait: 200 } },
);
});

await act(async () => {
expect(mockEffect.mock.calls.length).toEqual(1);
expect(mockCleanUp.mock.calls.length).toEqual(0);

mountedState = 2;
hook.rerender();
await sleep(300);
expect(mockEffect.mock.calls.length).toEqual(2);
expect(mockCleanUp.mock.calls.length).toEqual(1);

mountedState = 3;
hook.rerender();
hook.rerender({ value: 2, wait: 200 });
await sleep(100);
expect(mockEffect.mock.calls.length).toEqual(1);
expect(mockCleanUp.mock.calls.length).toEqual(0);
await sleep(150);
expect(mockEffect.mock.calls.length).toEqual(2);
expect(mockCleanUp.mock.calls.length).toEqual(1);

await sleep(300);
hook.rerender({ value: 3, wait: 100 });
await sleep(50);
expect(mockEffect.mock.calls.length).toEqual(3);
expect(mockCleanUp.mock.calls.length).toEqual(2);
await sleep(100);
expect(mockEffect.mock.calls.length).toEqual(3);
expect(mockCleanUp.mock.calls.length).toEqual(2);
});
Expand Down
7 changes: 3 additions & 4 deletions packages/hooks/src/useUnmountedRef/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ describe('useMountStatus', () => {
it('should be defined', () => {
expect(useUnmountedRef).toBeDefined();
});
it('test mount', async () => {
it('should work', async () => {
const hook = renderHook(() => useUnmountedRef());
expect(hook.result.current.current).toBe(false);
});
it('test unmounted', async () => {
const hook = renderHook(() => useUnmountedRef());
hook.rerender();
expect(hook.result.current.current).toBe(false);
hook.unmount();
expect(hook.result.current.current).toBe(true);
});
Expand Down
10 changes: 6 additions & 4 deletions packages/hooks/src/useUpdate/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { useRef } from 'react';
import { renderHook, act } from '@testing-library/react-hooks';
import useUpdate from '..';
import useMemoizedFn from '../../useMemoizedFn';

describe('useUpdate', () => {
it('should be defined', () => {
expect(useUpdate).toBeDefined();
});
it('should update', () => {
let count = 0;
const hooks = renderHook(() => {
const ref = useRef(0);
const update = useUpdate();
return {
update,
count: ref.current,
count,
onChange: useMemoizedFn(() => {
ref.current = ref.current + 1;
count++;
update();
}),
};
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/useUpdateEffect/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('useUpdateEffect', () => {
}, [mountedState]),
);
expect(mountedState).toEqual(1);
hook.rerender();
expect(mountedState).toEqual(1);
mountedState = 2;
hook.rerender();
expect(mountedState).toEqual(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('useUpdateLayoutEffect', () => {
}, [mountedState]),
);
expect(mountedState).toEqual(1);
hook.rerender();
expect(mountedState).toEqual(1);
mountedState = 2;
hook.rerender();
expect(mountedState).toEqual(3);
Expand Down

0 comments on commit e0ca12a

Please sign in to comment.