-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathInView.test.tsx
More file actions
158 lines (134 loc) · 4.43 KB
/
InView.test.tsx
File metadata and controls
158 lines (134 loc) · 4.43 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
import { render, screen } from "@testing-library/react";
import { userEvent } from "@vitest/browser/context";
import React from "react";
import { InView } from "../InView";
import { intersectionMockInstance, mockAllIsIntersecting } from "../test-utils";
test("Should render <InView /> intersecting", () => {
const callback = vi.fn();
render(
<InView onChange={callback}>
{({ inView, ref }) => <div ref={ref}>{inView.toString()}</div>}
</InView>,
);
mockAllIsIntersecting(false);
expect(callback).toHaveBeenLastCalledWith(
false,
expect.objectContaining({ isIntersecting: false }),
);
mockAllIsIntersecting(true);
expect(callback).toHaveBeenLastCalledWith(
true,
expect.objectContaining({ isIntersecting: true }),
);
});
test("should render plain children", () => {
render(<InView>inner</InView>);
screen.getByText("inner");
});
test("should render as element", () => {
const { container } = render(<InView as="span">inner</InView>);
const tagName = container.children[0].tagName.toLowerCase();
expect(tagName).toBe("span");
});
test("should render with className", () => {
const { container } = render(<InView className="inner-class">inner</InView>);
expect(container.children[0].className).toBe("inner-class");
});
test("Should respect skip", () => {
const cb = vi.fn();
render(
<InView skip onChange={cb}>
inner
</InView>,
);
mockAllIsIntersecting(true);
expect(cb).not.toHaveBeenCalled();
});
test("Should handle initialInView", () => {
const cb = vi.fn();
render(
<InView initialInView onChange={cb}>
{({ inView }) => <span>InView: {inView.toString()}</span>}
</InView>,
);
screen.getByText("InView: true");
});
test("Should unobserve old node", () => {
const { rerender } = render(
<InView>
{({ inView, ref }) => (
<div key="1" ref={ref}>
Inview: {inView.toString()}
</div>
)}
</InView>,
);
rerender(
<InView>
{({ inView, ref }) => (
<div key="2" ref={ref}>
Inview: {inView.toString()}
</div>
)}
</InView>,
);
mockAllIsIntersecting(true);
});
test("Should ensure node exists before observing and unobserving", () => {
const { unmount } = render(<InView>{() => null}</InView>);
unmount();
});
test("Should recreate observer when threshold change", () => {
const { container, rerender } = render(<InView>Inner</InView>);
mockAllIsIntersecting(true);
const instance = intersectionMockInstance(container.children[0]);
vi.spyOn(instance, "unobserve");
rerender(<InView threshold={0.5}>Inner</InView>);
expect(instance.unobserve).toHaveBeenCalled();
});
test("Should recreate observer when root change", () => {
const { container, rerender } = render(<InView>Inner</InView>);
mockAllIsIntersecting(true);
const instance = intersectionMockInstance(container.children[0]);
vi.spyOn(instance, "unobserve");
const root = document.createElement("div");
rerender(<InView root={root}>Inner</InView>);
expect(instance.unobserve).toHaveBeenCalled();
});
test("Should recreate observer when rootMargin change", () => {
const { container, rerender } = render(<InView>Inner</InView>);
mockAllIsIntersecting(true);
const instance = intersectionMockInstance(container.children[0]);
vi.spyOn(instance, "unobserve");
rerender(<InView rootMargin="10px">Inner</InView>);
expect(instance.unobserve).toHaveBeenCalled();
});
test("Should unobserve when triggerOnce comes into view", () => {
const { container } = render(<InView triggerOnce>Inner</InView>);
mockAllIsIntersecting(false);
const instance = intersectionMockInstance(container.children[0]);
vi.spyOn(instance, "unobserve");
mockAllIsIntersecting(true);
expect(instance.unobserve).toHaveBeenCalled();
});
test("Should unobserve when unmounted", () => {
const { container, unmount } = render(<InView triggerOnce>Inner</InView>);
const instance = intersectionMockInstance(container.children[0]);
vi.spyOn(instance, "unobserve");
unmount();
expect(instance.unobserve).toHaveBeenCalled();
});
test("plain children should not catch bubbling onChange event", async () => {
const onChange = vi.fn();
const { getByLabelText } = render(
<InView onChange={onChange}>
<label>
<input name="field" />
input
</label>
</InView>,
);
const input = getByLabelText("input");
await userEvent.type(input, "changed value");
expect(onChange).not.toHaveBeenCalled();
});