Skip to content

Commit 976a6ba

Browse files
Daniel Schmidtthebuilder
authored andcommitted
Add some Jest tests
1 parent 3994ee3 commit 976a6ba

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/intersection.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ export function observe(
3333
if (observerId) OBSERVER_MAP.set(observerId, observerInstance)
3434
}
3535

36-
INSTANCE_MAP.set(element, {
36+
const instance = {
3737
callback,
3838
visible: false,
3939
options,
4040
observerId,
4141
observer: !observerId ? observerInstance : undefined,
42-
})
42+
}
43+
44+
INSTANCE_MAP.set(element, instance)
4345

4446
observerInstance.observe(element)
47+
48+
return instance
4549
}
4650

4751
/**

tests/intersection.test.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,67 @@ const el = { el: 'htmlElement' }
1212

1313
it('should observe', () => {
1414
const cb = jest.fn()
15-
observe(el, cb)
15+
const instance = observe(el, cb)
16+
17+
expect(instance).toMatchObject({
18+
observerId: '0',
19+
visible: false,
20+
options: {
21+
threshold: 0,
22+
},
23+
})
24+
})
25+
26+
it('should observe with options', () => {
27+
const cb = jest.fn()
28+
const instance = observe(el, cb, { threshold: 0 })
29+
30+
expect(instance).toMatchObject({
31+
observerId: '0',
32+
visible: false,
33+
options: {
34+
threshold: 0,
35+
},
36+
})
1637
})
1738

1839
it('should observe with threshold', () => {
1940
const cb = jest.fn()
20-
observe(el, cb, 1)
41+
const instance = observe(el, cb, { threshold: 1 })
42+
43+
expect(instance).toMatchObject({
44+
observerId: '1',
45+
visible: false,
46+
options: {
47+
threshold: 1,
48+
},
49+
})
2150
})
2251

2352
it('should observe with Array threshold', () => {
2453
const cb = jest.fn()
25-
observe(el, cb, [0.3, 0.6])
54+
const instance = observe(el, cb, { threshold: [0.3, 0.6] })
55+
56+
expect(instance).toMatchObject({
57+
observerId: '0.3,0.6',
58+
visible: false,
59+
options: {
60+
threshold: [0.3, 0.6],
61+
},
62+
})
63+
})
64+
65+
it('should observe with rootId', () => {
66+
const cb = jest.fn()
67+
const instance = observe(el, cb, { threshold: 0, root: {} }, 'window')
68+
69+
expect(instance).toMatchObject({
70+
observerId: 'window_0',
71+
visible: false,
72+
options: {
73+
root: {},
74+
},
75+
})
2676
})
2777

2878
it('should unobserve', () => {

0 commit comments

Comments
 (0)