Skip to content

Commit 0412049

Browse files
authored
fix: ensure thresholds exists, by setting it on the instance (#194)
1 parent 295ae53 commit 0412049

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

src/__tests__/intersection.test.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import { observe, unobserve, destroy } from '../intersection'
2-
3-
global.IntersectionObserver = jest.fn((cb, options) => ({
4-
thresholds: Array.isArray(options.threshold)
5-
? options.threshold
6-
: [options.threshold],
7-
root: options.root,
8-
rootMargin: options.rootMargin,
9-
observe: jest.fn(),
10-
unobserve: jest.fn(),
11-
disconnect: jest.fn(),
12-
}))
2+
import '../test-utils'
133

144
afterEach(() => destroy())
155

@@ -22,6 +12,7 @@ it('should observe', () => {
2212
expect(instance).toMatchObject({
2313
observerId: '0',
2414
inView: false,
15+
thresholds: [0],
2516
observer: {
2617
thresholds: [0],
2718
},
@@ -41,6 +32,7 @@ it('should observe with options', () => {
4132
expect(instance).toMatchObject({
4233
observerId: '0',
4334
inView: false,
35+
thresholds: [0],
4436
observer: {
4537
thresholds: [0],
4638
},
@@ -54,6 +46,7 @@ it('should observe with threshold', () => {
5446
expect(instance).toMatchObject({
5547
observerId: '1',
5648
inView: false,
49+
thresholds: [1],
5750
observer: {
5851
thresholds: [1],
5952
},
@@ -67,6 +60,7 @@ it('should observe with Array threshold', () => {
6760
expect(instance).toMatchObject({
6861
observerId: '0.3,0.6',
6962
inView: false,
63+
thresholds: [0.3, 0.6],
7064
observer: {
7165
thresholds: [0.3, 0.6],
7266
},

src/intersection.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type ObserverInstance = {
1111
inView: boolean
1212
observerId: string
1313
observer: IntersectionObserver
14+
thresholds: number[]
1415
}
1516

1617
const INSTANCE_MAP: Map<Element, ObserverInstance> = new Map()
@@ -78,6 +79,10 @@ export function observe(
7879
inView: false,
7980
observerId,
8081
observer: observerInstance,
82+
// Make sure we have the thresholds value. It's undefined on a browser like Chrome 51.
83+
thresholds:
84+
observerInstance.thresholds ||
85+
(Array.isArray(threshold) ? threshold : [threshold]),
8186
}
8287

8388
INSTANCE_MAP.set(element, instance)
@@ -150,10 +155,8 @@ function onChange(changes: IntersectionObserverEntry[]) {
150155
// Firefox can report a negative intersectionRatio when scrolling.
151156
/* istanbul ignore else */
152157
if (instance && intersectionRatio >= 0) {
153-
const thresholds = instance.observer.thresholds
154-
155158
// If threshold is an array, check if any of them intersects. This just triggers the onChange event multiple times.
156-
let inView = thresholds.some(threshold => {
159+
let inView = instance.thresholds.some(threshold => {
157160
return instance.inView
158161
? intersectionRatio > threshold
159162
: intersectionRatio >= threshold

src/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ beforeAll(() => {
2828

2929
afterEach(() => {
3030
// @ts-ignore
31-
global.IntersectionObserver.mockReset()
31+
global.IntersectionObserver.mockClear()
3232
instanceMap.clear()
3333
observerMap.clear()
3434
})

0 commit comments

Comments
 (0)