forked from Rapiders/react-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFocus.test.tsx
More file actions
57 lines (49 loc) · 1.32 KB
/
useFocus.test.tsx
File metadata and controls
57 lines (49 loc) · 1.32 KB
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
import { fireEvent, render } from '@testing-library/react';
import useFocus from './useFocus';
import React from 'react';
interface Entrie {
target: Element;
isIntersecting: boolean;
}
const mockIntersectionObserver = class {
entries: Entrie[];
constructor(callback) {
this.entries = [];
window.addEventListener('scroll', () => {
if (window.scrollY > 50) {
this.entries.map((entry) => {
entry.isIntersecting = this.isInViewPort();
});
}
callback(this.entries, this);
});
}
isInViewPort() {
return true;
}
observe(target: Element) {
this.entries.push({ isIntersecting: false, target });
}
unobserve(target) {
this.entries = this.entries.filter((ob) => ob.target !== target);
}
disconnect() {
this.entries = [];
}
};
describe('useFocus 기능 테스트', () => {
beforeEach(() => {
// eslint-disable-next-line
global.IntersectionObserver = mockIntersectionObserver as any;
});
it('focus되었을때 onFocusCallback을 실행시킬 수 있다.', async () => {
const mock = jest.fn();
const Test = () => {
const ref = useFocus<HTMLDivElement>(mock);
return <div ref={ref}>Test</div>;
};
render(<Test />);
fireEvent.scroll(window, { target: { scrollY: 100 } });
expect(mock).toHaveBeenCalled();
});
});