-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathis-in-viewport.test.tsx
92 lines (62 loc) · 2.73 KB
/
is-in-viewport.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { runAllIntersectionObservers } from '../../../utils/__tests__/mock-intersection-observer';
jest.useFakeTimers();
function isInViewport(element: Element, callback: (inViewport: boolean) => void) {
// We need to import the function dynamically so that it picks up the mocked IntersectionObserver.
// eslint-disable-next-line @typescript-eslint/no-var-requires
return jest.requireActual('../is-in-viewport').isInViewport(element, callback);
}
beforeEach(() => jest.resetAllMocks());
describe('isInViewport', () => {
it('calls the callback with `true` if the element is visible', () => {
const callback = jest.fn();
const element = document.createElement('div');
isInViewport(element, callback);
runAllIntersectionObservers([{ target: element, isIntersecting: true }]);
jest.runAllTimers();
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(true);
});
it('calls the callback with `false` if the element is not visible', () => {
const callback = jest.fn();
const element = document.createElement('div');
isInViewport(element, callback);
runAllIntersectionObservers([{ target: element, isIntersecting: false }]);
jest.runAllTimers();
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(false);
});
it('calls the callback with `false` if the IntersectionObserver does not fire in reasonable time', () => {
const callback = jest.fn();
const element = document.createElement('div');
isInViewport(element, callback);
jest.runAllTimers();
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(false);
});
it('calls different callbacks for different elements', () => {
const callback1 = jest.fn();
const element1 = document.createElement('div');
const callback2 = jest.fn();
const element2 = document.createElement('div');
isInViewport(element1, callback1);
isInViewport(element2, callback2);
runAllIntersectionObservers([
{ target: element2, isIntersecting: false },
{ target: element1, isIntersecting: true },
]);
jest.runAllTimers();
expect(callback1).toHaveBeenCalledWith(true);
expect(callback2).toHaveBeenCalledWith(false);
});
it('does not call the callback if the cleanup function is run', () => {
const callback = jest.fn();
const element = document.createElement('div');
const cleanup = isInViewport(element, callback);
cleanup();
runAllIntersectionObservers([{ target: element, isIntersecting: false }]);
jest.runAllTimers();
expect(callback).not.toHaveBeenCalled();
});
});