-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathuseIntersectionObserver.test.tsx
42 lines (38 loc) · 1.63 KB
/
useIntersectionObserver.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
import { renderHook, act } from '@testing-library/react-hooks';
import ElementObserver from '../src/hooks/elementObserver';
jest.mock('../src/hooks/elementObserver');
import useIntersectionObserver from '../src/hooks/useIntersectionObserver';
describe('useIntersectionObserver', () => {
it('returns an object on initialization', () => {
const { result } = renderHook(() => useIntersectionObserver());
expect(result.current).toBeDefined();
});
it('registers a callback', () => {
const { result } = renderHook(() => useIntersectionObserver());
const element = document.createElement('li');
const callback = () => {};
act(() => {
result.current.observe(element, callback);
});
const mockElementObserver = (ElementObserver as jest.Mock).mock
.instances[0];
const registerCallback = mockElementObserver.registerCallback;
expect(registerCallback.mock.calls.length).toBe(1);
expect(registerCallback.mock.calls[0].length).toBe(2);
expect(registerCallback.mock.calls[0][0]).toBe(element);
expect(registerCallback.mock.calls[0][1]).toBe(callback);
});
it('unregisters a callback', () => {
const { result } = renderHook(() => useIntersectionObserver());
const element = document.createElement('li');
act(() => {
result.current.unobserve(element);
});
const mockElementObserver = (ElementObserver as jest.Mock).mock
.instances[0];
const removeCallback = mockElementObserver.removeCallback;
expect(removeCallback.mock.calls.length).toBe(1);
expect(removeCallback.mock.calls[0].length).toBe(1);
expect(removeCallback.mock.calls[0][0]).toBe(element);
});
});