-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseWindowSize.test.tsx
111 lines (85 loc) Β· 2.85 KB
/
useWindowSize.test.tsx
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { act, renderHook } from '@testing-library/react-hooks';
import { replaceRaf } from 'raf-stub';
import useWindowSize from '../src/useWindowSize';
import { isBrowser } from '../src/misc/util';
declare var requestAnimationFrame: {
reset: () => void;
step: (steps?: number, duration?: number) => void;
};
describe('useWindowSize', () => {
beforeAll(() => {
replaceRaf();
});
afterEach(() => {
requestAnimationFrame.reset();
});
it('should be defined', () => {
expect(useWindowSize).toBeDefined();
});
function getHook(options?: any) {
return renderHook(() => useWindowSize(options));
}
function triggerResize(dimension: 'width' | 'height', value: number) {
if (dimension === 'width') {
(window.innerWidth as number) = value;
} else if (dimension === 'height') {
(window.innerHeight as number) = value;
}
window.dispatchEvent(new Event('resize'));
}
it('should return current window dimensions', () => {
const hook = getHook();
expect(typeof hook.result.current).toBe('object');
expect(typeof hook.result.current.height).toBe('number');
expect(typeof hook.result.current.width).toBe('number');
});
it('should use passed parameters as initial values in case of non-browser use', () => {
const hook = getHook({ initialWidth: 1, initialHeight: 1 });
expect(hook.result.current.height).toBe(isBrowser ? window.innerHeight : 1);
expect(hook.result.current.width).toBe(isBrowser ? window.innerWidth : 1);
});
it('should re-render after height change on closest RAF', () => {
const hook = getHook();
act(() => {
triggerResize('height', 360);
requestAnimationFrame.step();
});
expect(hook.result.current.height).toBe(360);
act(() => {
triggerResize('height', 2048);
requestAnimationFrame.step();
});
expect(hook.result.current.height).toBe(2048);
});
it('should re-render after width change on closest RAF', () => {
const hook = getHook();
act(() => {
triggerResize('width', 360);
requestAnimationFrame.step();
});
expect(hook.result.current.width).toBe(360);
act(() => {
triggerResize('width', 2048);
requestAnimationFrame.step();
});
expect(hook.result.current.width).toBe(2048);
});
it('should call onChange callback on window resize', () => {
const onChange = jest.fn();
getHook({ onChange });
act(() => {
triggerResize('width', 720);
triggerResize('height', 480);
requestAnimationFrame.step();
});
expect(onChange).toHaveBeenCalledWith(720, 480);
expect(onChange).toHaveBeenCalledTimes(2);
act(() => {
triggerResize('width', 1920);
triggerResize('height', 1080);
requestAnimationFrame.step();
});
expect(onChange).toHaveBeenCalledWith(1920, 1080);
expect(onChange).toHaveBeenCalledTimes(4);
});
});