-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathhooks.test.tsx
More file actions
349 lines (296 loc) · 9.59 KB
/
hooks.test.tsx
File metadata and controls
349 lines (296 loc) · 9.59 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { render, screen } from "@testing-library/react";
import React, { useCallback } from "react";
import type { IntersectionOptions } from "../index";
import {
destroyIntersectionMocking,
intersectionMockInstance,
mockAllIsIntersecting,
mockIsIntersecting,
} from "../test-utils";
import { useInView } from "../useInView";
const HookComponent = ({
options,
unmount,
}: {
options?: IntersectionOptions;
unmount?: boolean;
}) => {
const [ref, inView] = useInView(options);
return (
<div data-testid="wrapper" ref={!unmount ? ref : undefined}>
{inView.toString()}
</div>
);
};
const LazyHookComponent = ({ options }: { options?: IntersectionOptions }) => {
const [isLoading, setIsLoading] = React.useState(true);
React.useEffect(() => {
setIsLoading(false);
}, []);
const [ref, inView] = useInView(options);
if (isLoading) return <div>Loading</div>;
return (
<div data-testid="wrapper" ref={ref}>
{inView.toString()}
</div>
);
};
test("should create a hook", () => {
const { getByTestId } = render(<HookComponent />);
const wrapper = getByTestId("wrapper");
const instance = intersectionMockInstance(wrapper);
expect(instance.observe).toHaveBeenCalledWith(wrapper);
});
test("should create a hook with array threshold", () => {
const { getByTestId } = render(
<HookComponent options={{ threshold: [0.1, 1] }} />,
);
const wrapper = getByTestId("wrapper");
const instance = intersectionMockInstance(wrapper);
expect(instance.observe).toHaveBeenCalledWith(wrapper);
});
test("should create a lazy hook", () => {
const { getByTestId } = render(<LazyHookComponent />);
const wrapper = getByTestId("wrapper");
const instance = intersectionMockInstance(wrapper);
expect(instance.observe).toHaveBeenCalledWith(wrapper);
});
test("should create a hook inView", () => {
const { getByText } = render(<HookComponent />);
mockAllIsIntersecting(true);
getByText("true");
});
test("should mock thresholds", () => {
render(<HookComponent options={{ threshold: [0.5, 1] }} />);
mockAllIsIntersecting(0.2);
screen.getByText("false");
mockAllIsIntersecting(0.5);
screen.getByText("true");
mockAllIsIntersecting(1);
screen.getByText("true");
});
test("should create a hook with initialInView", () => {
const { getByText } = render(
<HookComponent options={{ initialInView: true }} />,
);
getByText("true");
mockAllIsIntersecting(false);
getByText("false");
});
test("should trigger a hook leaving view", () => {
const { getByText } = render(<HookComponent />);
mockAllIsIntersecting(true);
mockAllIsIntersecting(false);
getByText("false");
});
test("should respect trigger once", () => {
const { getByText } = render(
<HookComponent options={{ triggerOnce: true }} />,
);
mockAllIsIntersecting(true);
mockAllIsIntersecting(false);
getByText("true");
});
test("should trigger onChange", () => {
const onChange = vi.fn();
render(<HookComponent options={{ onChange }} />);
mockAllIsIntersecting(true);
expect(onChange).toHaveBeenLastCalledWith(
true,
expect.objectContaining({ intersectionRatio: 1, isIntersecting: true }),
);
mockAllIsIntersecting(false);
expect(onChange).toHaveBeenLastCalledWith(
false,
expect.objectContaining({ intersectionRatio: 0, isIntersecting: false }),
);
});
test("should respect skip", () => {
const { getByText, rerender } = render(
<HookComponent options={{ skip: true }} />,
);
mockAllIsIntersecting(false);
getByText("false");
rerender(<HookComponent options={{ skip: false }} />);
mockAllIsIntersecting(true);
getByText("true");
});
test("should not reset current state if changing skip", () => {
const { getByText, rerender } = render(
<HookComponent options={{ skip: false }} />,
);
mockAllIsIntersecting(true);
rerender(<HookComponent options={{ skip: true }} />);
getByText("true");
});
test("should unmount the hook", () => {
const { unmount, getByTestId } = render(<HookComponent />);
const wrapper = getByTestId("wrapper");
const instance = intersectionMockInstance(wrapper);
unmount();
expect(instance.unobserve).toHaveBeenCalledWith(wrapper);
});
test("inView should be false when component is unmounted", () => {
const { rerender, getByText } = render(<HookComponent />);
mockAllIsIntersecting(true);
getByText("true");
rerender(<HookComponent unmount />);
getByText("false");
});
test("should handle trackVisibility", () => {
render(<HookComponent options={{ trackVisibility: true, delay: 100 }} />);
mockAllIsIntersecting(true);
});
test("should handle trackVisibility when unsupported", () => {
render(<HookComponent options={{ trackVisibility: true, delay: 100 }} />);
});
const SwitchHookComponent = ({
options,
toggle,
unmount,
}: {
options?: IntersectionOptions;
toggle?: boolean;
unmount?: boolean;
}) => {
const [ref, inView] = useInView(options);
return (
<>
<div
data-testid="item-1"
data-inview={!toggle && inView}
ref={!toggle && !unmount ? ref : undefined}
/>
<div
data-testid="item-2"
data-inview={!!toggle && inView}
ref={toggle && !unmount ? ref : undefined}
/>
</>
);
};
/**
* This is a test for the case where people move the ref around (please don't)
*/
test("should handle ref removed", () => {
const { rerender, getByTestId } = render(<SwitchHookComponent />);
mockAllIsIntersecting(true);
const item1 = getByTestId("item-1");
const item2 = getByTestId("item-2");
// Item1 should be inView
expect(item1.getAttribute("data-inview")).toBe("true");
expect(item2.getAttribute("data-inview")).toBe("false");
rerender(<SwitchHookComponent toggle />);
mockAllIsIntersecting(true);
// Item2 should be inView
expect(item1.getAttribute("data-inview")).toBe("false");
expect(item2.getAttribute("data-inview")).toBe("true");
rerender(<SwitchHookComponent unmount />);
// Nothing should be inView
expect(item1.getAttribute("data-inview")).toBe("false");
expect(item2.getAttribute("data-inview")).toBe("false");
// Add the ref back
rerender(<SwitchHookComponent />);
mockAllIsIntersecting(true);
expect(item1.getAttribute("data-inview")).toBe("true");
expect(item2.getAttribute("data-inview")).toBe("false");
});
const MergeRefsComponent = ({ options }: { options?: IntersectionOptions }) => {
const [inViewRef, inView] = useInView(options);
const setRef = useCallback(
(node: Element | null) => inViewRef(node),
[inViewRef],
);
return <div data-testid="inview" data-inview={inView} ref={setRef} />;
};
test("should handle ref merged", () => {
const { rerender, getByTestId } = render(<MergeRefsComponent />);
mockAllIsIntersecting(true);
rerender(<MergeRefsComponent />);
expect(getByTestId("inview").getAttribute("data-inview")).toBe("true");
});
const MultipleHookComponent = ({
options,
}: {
options?: IntersectionOptions;
}) => {
const [ref1, inView1] = useInView(options);
const [ref2, inView2] = useInView(options);
const [ref3, inView3] = useInView();
const mergedRefs = useCallback(
(node: Element | null) => {
const cleanup = [ref1(node), ref2(node), ref3(node)];
return () => cleanup.forEach((fn) => fn());
},
[ref1, ref2, ref3],
);
return (
<div ref={mergedRefs}>
<div data-testid="item-1" data-inview={inView1}>
{inView1}
</div>
<div data-testid="item-2" data-inview={inView2}>
{inView2}
</div>
<div data-testid="item-3" data-inview={inView3}>
{inView3}
</div>
</div>
);
};
test("should handle multiple hooks on the same element", () => {
const { getByTestId } = render(
<MultipleHookComponent options={{ threshold: 0.1 }} />,
);
mockAllIsIntersecting(true);
expect(getByTestId("item-1").getAttribute("data-inview")).toBe("true");
expect(getByTestId("item-2").getAttribute("data-inview")).toBe("true");
expect(getByTestId("item-3").getAttribute("data-inview")).toBe("true");
});
test("should handle thresholds missing on observer instance", () => {
render(<HookComponent options={{ threshold: [0.1, 1] }} />);
const wrapper = screen.getByTestId("wrapper");
const instance = intersectionMockInstance(wrapper);
// @ts-ignore
instance.thresholds = undefined;
mockAllIsIntersecting(true);
screen.getByText("true");
});
test("should handle thresholds missing on observer instance with no threshold set", () => {
render(<HookComponent />);
const wrapper = screen.getByTestId("wrapper");
const instance = intersectionMockInstance(wrapper);
// @ts-ignore
instance.thresholds = undefined;
mockAllIsIntersecting(true);
screen.getByText("true");
});
const HookComponentWithEntry = ({
options,
unmount,
}: {
options?: IntersectionOptions;
unmount?: boolean;
}) => {
const { ref, entry } = useInView(options);
return (
<div data-testid="wrapper" ref={!unmount ? ref : undefined}>
{entry && Object.entries(entry).map(([key, value]) => `${key}: ${value}`)}
</div>
);
};
test("should set intersection ratio as the largest threshold smaller than trigger", () => {
render(
<HookComponentWithEntry options={{ threshold: [0, 0.25, 0.5, 0.75, 1] }} />,
);
const wrapper = screen.getByTestId("wrapper");
mockIsIntersecting(wrapper, 0.5);
screen.getByText(/intersectionRatio: 0.5/);
});
test("should restore the browser IntersectionObserver", () => {
expect(vi.isMockFunction(window.IntersectionObserver)).toBe(true);
destroyIntersectionMocking();
// This should restore the original IntersectionObserver
expect(window.IntersectionObserver).toBeDefined();
expect(vi.isMockFunction(window.IntersectionObserver)).toBe(false);
});