-
Notifications
You must be signed in to change notification settings - Fork 37
/
elementObserver.test.tsx
39 lines (33 loc) · 1.03 KB
/
elementObserver.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
// TODO move this
const observe = jest.fn();
const unobserve = jest.fn();
const disconnect = jest.fn();
jest.spyOn(globalThis, 'IntersectionObserver').mockImplementation(() => {
return {
observe,
unobserve,
disconnect,
root: null,
thresholds: [],
rootMargin: '',
takeRecords: () => [],
};
});
import ElementObserver from '../src/hooks/elementObserver';
describe('elementObserver', () => {
it('registers a callback and observes the element', () => {
const observer = new ElementObserver();
const element = document.createElement('li');
observer.registerCallback(element, ()=>{});
expect(observe).toHaveBeenCalled();
expect(observe).toHaveBeenCalledWith(element);
});
it('unregisters a callback and unobserves the element', () => {
const observer = new ElementObserver();
const element = document.createElement('li');
observer.removeCallback(element);
expect(unobserve).toHaveBeenCalled();
expect(unobserve).toHaveBeenCalledWith(element);
});
});
jest.clearAllMocks();