Skip to content

Commit 989a8eb

Browse files
committed
test: improve test coverage of intersection.js
1 parent 826b4ee commit 989a8eb

File tree

3 files changed

+160
-2
lines changed

3 files changed

+160
-2
lines changed

src/intersection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function onChange(changes) {
120120
const { isIntersecting, intersectionRatio, target } = intersection
121121
const instance = INSTANCE_MAP.get(target)
122122

123-
// Firefox can report a negative intersectionRatio when scrolling. Ignore this, and
123+
// Firefox can report a negative intersectionRatio when scrolling.
124124
if (instance && intersectionRatio >= 0) {
125125
const options = instance.options
126126

tests/Observer.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/no-named-as-default-member */
12
import React from 'react'
23
import { mount } from 'enzyme'
34
import Observer from '../src/index.js'
@@ -123,7 +124,7 @@ it('Should recreate observer when root change', () => {
123124
expect(instance.observeNode).toHaveBeenCalled()
124125
})
125126

126-
it('Should recreate observer when root change', () => {
127+
it('Should recreate observer when rootMargin change', () => {
127128
const wrapper = mount(<Observer>Content</Observer>)
128129
const instance = wrapper.instance()
129130
jest.spyOn(instance, 'observeNode')

tests/intersection.test.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,165 @@ it('should observe with rootId', () => {
7474
},
7575
})
7676
})
77+
it('should observe without rootId', () => {
78+
const cb = jest.fn()
79+
const instance = observe(el, cb, { threshold: 0, root: {} })
80+
81+
expect(instance).toMatchObject({
82+
visible: false,
83+
options: {
84+
root: {},
85+
},
86+
})
87+
})
7788

7889
it('should unobserve', () => {
7990
observe(el, jest.fn())
8091
unobserve(el)
8192
})
93+
94+
it('should keep observer when unobserve with multiple elements', () => {
95+
observe(el, jest.fn())
96+
observe({ el: 'htmlElement2' }, jest.fn())
97+
unobserve(el)
98+
})
99+
100+
it('should trigger onChange with ratio 0', () => {
101+
const cb = jest.fn()
102+
const instance = observe(el, cb)
103+
const calls = global.IntersectionObserver.mock.calls
104+
const [onChange] = calls[calls.length - 1]
105+
106+
// We now have the onChange method
107+
onChange([
108+
{
109+
target: el,
110+
intersectionRatio: 0,
111+
},
112+
])
113+
114+
expect(cb).toHaveBeenCalledWith(true)
115+
expect(instance.visible).toBe(true)
116+
})
117+
118+
it('should trigger onChange with multiple thresholds ', () => {
119+
const cb = jest.fn()
120+
const instance = observe(el, cb, { threshold: [0, 0.5] })
121+
const calls = global.IntersectionObserver.mock.calls
122+
const [onChange] = calls[calls.length - 1]
123+
124+
// We now have the onChange method
125+
onChange([
126+
{
127+
target: el,
128+
intersectionRatio: 0,
129+
},
130+
])
131+
132+
expect(cb).toHaveBeenCalledWith(true)
133+
expect(instance.visible).toBe(true)
134+
})
135+
136+
it('should trigger onChange with isIntersection', () => {
137+
const cb = jest.fn()
138+
const instance = observe(el, cb)
139+
const calls = global.IntersectionObserver.mock.calls
140+
const [onChange] = calls[calls.length - 1]
141+
142+
// We now have the onChange method
143+
onChange([
144+
{
145+
target: el,
146+
intersectionRatio: 0,
147+
isIntersecting: true,
148+
},
149+
])
150+
151+
expect(cb).toHaveBeenCalledWith(true)
152+
expect(instance.visible).toBe(true)
153+
})
154+
155+
it('should not trigger if threshold is undefined', () => {
156+
const cb = jest.fn()
157+
const instance = observe(el, cb, { threshold: undefined })
158+
const calls = global.IntersectionObserver.mock.calls
159+
const [onChange] = calls[calls.length - 1]
160+
161+
// We now have the onChange method
162+
onChange([
163+
{
164+
target: el,
165+
intersectionRatio: 0,
166+
},
167+
])
168+
169+
expect(cb).toHaveBeenCalledWith(false)
170+
expect(instance.visible).toBe(false)
171+
})
172+
173+
it('should trigger onChange with isIntersection false', () => {
174+
const cb = jest.fn()
175+
const instance = observe(el, cb)
176+
const calls = global.IntersectionObserver.mock.calls
177+
const [onChange] = calls[calls.length - 1]
178+
179+
// We now have the onChange method
180+
onChange([
181+
{
182+
target: el,
183+
intersectionRatio: 0,
184+
isIntersecting: false,
185+
},
186+
])
187+
188+
expect(cb).toHaveBeenCalledWith(false)
189+
expect(instance.visible).toBe(false)
190+
})
191+
192+
it('should trigger clear visible when going back to 0', () => {
193+
const cb = jest.fn()
194+
const instance = observe(el, cb)
195+
const calls = global.IntersectionObserver.mock.calls
196+
const [onChange] = calls[calls.length - 1]
197+
198+
onChange([
199+
{
200+
target: el,
201+
intersectionRatio: 0.1,
202+
},
203+
])
204+
205+
expect(instance.visible).toBe(true)
206+
onChange([
207+
{
208+
target: el,
209+
intersectionRatio: 0,
210+
},
211+
])
212+
213+
expect(instance.visible).toBe(false)
214+
})
215+
216+
it('should trigger clear visible when going back to 0 with array threshold', () => {
217+
const cb = jest.fn()
218+
const instance = observe(el, cb, { threshold: [0, 0.5] })
219+
const calls = global.IntersectionObserver.mock.calls
220+
const [onChange] = calls[calls.length - 1]
221+
222+
onChange([
223+
{
224+
target: el,
225+
intersectionRatio: 0.1,
226+
},
227+
])
228+
229+
expect(instance.visible).toBe(true)
230+
onChange([
231+
{
232+
target: el,
233+
intersectionRatio: 0,
234+
},
235+
])
236+
237+
expect(instance.visible).toBe(false)
238+
})

0 commit comments

Comments
 (0)