-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseShallowCompareEffect.test.ts
46 lines (37 loc) · 1.51 KB
/
useShallowCompareEffect.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { renderHook } from '@testing-library/react-hooks';
import { useShallowCompareEffect } from '../src';
import { useEffect } from 'react';
let options1 = { max: 10, range: { from: 0, to: 10 } };
const options2 = { max: 10, range: { from: 0, to: 10 } };
const mockEffectNormal = jest.fn();
const mockEffectShallow = jest.fn();
const mockEffectCleanup = jest.fn();
const mockEffectCallback = jest.fn().mockReturnValue(mockEffectCleanup);
it('should shallow compare dependencies', () => {
const { rerender: rerenderNormal } = renderHook(() =>
useEffect(mockEffectNormal, [options1, options2])
);
const { rerender: rerenderShallow } = renderHook(() =>
useShallowCompareEffect(mockEffectShallow, [options1, options2])
);
expect(mockEffectNormal).toHaveBeenCalledTimes(1);
expect(mockEffectShallow).toHaveBeenCalledTimes(1);
options1 = { max: 10, range: options1.range };
rerenderShallow();
rerenderNormal();
expect(mockEffectNormal).toHaveBeenCalledTimes(2);
expect(mockEffectShallow).toHaveBeenCalledTimes(1);
options1 = { max: 10, range: { from: 0, to: 10 } };
rerenderNormal();
rerenderShallow();
expect(mockEffectNormal).toHaveBeenCalledTimes(3);
expect(mockEffectShallow).toHaveBeenCalledTimes(2);
});
it('should run clean-up provided on unmount', () => {
const { unmount } = renderHook(() =>
useShallowCompareEffect(mockEffectCallback, [options1, options2])
);
expect(mockEffectCleanup).not.toHaveBeenCalled();
unmount();
expect(mockEffectCleanup).toHaveBeenCalledTimes(1);
});