-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseScrollbarWidth.test.ts
45 lines (37 loc) · 1.21 KB
/
useScrollbarWidth.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
import { act, renderHook } from '@testing-library/react-hooks';
import { scrollbarWidth } from '@xobotyi/scrollbar-width';
import { useScrollbarWidth } from '../src';
import { replaceRaf } from 'raf-stub';
declare var requestAnimationFrame: {
add: (cb: Function) => number;
remove: (id: number) => void;
flush: (duration?: number) => void;
reset: () => void;
step: (steps?: number, duration?: number) => void;
};
describe('useScrollbarWidth', () => {
beforeAll(() => {
replaceRaf();
});
afterEach(() => {
requestAnimationFrame.reset();
});
it('should be defined', () => {
expect(useScrollbarWidth).toBeDefined();
});
it('should return value of scrollbarWidth result', () => {
scrollbarWidth.__cache = 21;
const { result } = renderHook(() => useScrollbarWidth());
expect(result.current).toBe(21);
});
it('should re-call scrollbar width in RAF in case `scrollbarWidth()` returned undefined', () => {
scrollbarWidth.__cache = undefined;
const { result } = renderHook(() => useScrollbarWidth());
expect(result.current).toBe(undefined);
scrollbarWidth.__cache = 34;
act(() => {
requestAnimationFrame.step();
});
expect(result.current).toBe(34);
});
});