Skip to content

Commit 826b4ee

Browse files
committed
test: improve test coverage of index.js
1 parent 1f03482 commit 826b4ee

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

tests/Observer.test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import React from 'react'
22
import { mount } from 'enzyme'
33
import Observer from '../src/index.js'
4+
import intersection from '../src/intersection'
45
import invariant from 'invariant'
56

67
jest.mock('../src/intersection')
78
jest.mock('invariant')
89

10+
afterEach(() => {})
11+
912
it('Should render <Observer />', () => {
1013
const callback = jest.fn()
1114
mount(<Observer>{callback}</Observer>)
@@ -61,6 +64,99 @@ it('Should render <Observer /> render when in view', () => {
6164
expect(wrapper).toMatchSnapshot()
6265
})
6366

67+
it('Should unobserve old node', () => {
68+
const wrapper = mount(<Observer>Content</Observer>)
69+
const instance = wrapper.instance()
70+
jest.spyOn(instance, 'observeNode')
71+
const node = wrapper.getDOMNode()
72+
instance.handleNode(<div />)
73+
expect(intersection.unobserve).toHaveBeenCalledWith(node)
74+
expect(instance.observeNode).toHaveBeenCalled()
75+
expect(intersection.observe).toHaveBeenCalled()
76+
})
77+
78+
it('Should ensure node exists before observering', () => {
79+
const wrapper = mount(<Observer>Content</Observer>)
80+
const instance = wrapper.instance()
81+
intersection.observe.mockReset()
82+
instance.handleNode(null)
83+
expect(intersection.observe).not.toHaveBeenCalled()
84+
})
85+
86+
it('Should ensure node exists before unmounting', () => {
87+
const wrapper = mount(<Observer>Content</Observer>)
88+
const instance = wrapper.instance()
89+
instance.handleNode(null)
90+
91+
intersection.unobserve.mockReset()
92+
instance.componentWillUnmount()
93+
expect(intersection.unobserve).not.toHaveBeenCalled()
94+
})
95+
96+
it('Should update state onChange', () => {
97+
const wrapper = mount(<Observer>Content</Observer>)
98+
wrapper.instance().handleChange(true)
99+
expect(wrapper.state().inView).toBe(true)
100+
wrapper.instance().handleChange(false)
101+
expect(wrapper.state().inView).toBe(false)
102+
})
103+
104+
it('Should recreate observer when threshold change', () => {
105+
const wrapper = mount(<Observer>Content</Observer>)
106+
const instance = wrapper.instance()
107+
jest.spyOn(instance, 'observeNode')
108+
109+
// Changing threshold should cause the instance to be observed once more
110+
wrapper.setProps({ threshold: 0.5 })
111+
expect(intersection.unobserve).toHaveBeenCalledWith(wrapper.getDOMNode())
112+
expect(instance.observeNode).toHaveBeenCalled()
113+
})
114+
115+
it('Should recreate observer when root change', () => {
116+
const wrapper = mount(<Observer>Content</Observer>)
117+
const instance = wrapper.instance()
118+
jest.spyOn(instance, 'observeNode')
119+
120+
// Changing threshold should cause the instance to be observed once more
121+
wrapper.setProps({ root: {} })
122+
expect(intersection.unobserve).toHaveBeenCalledWith(wrapper.getDOMNode())
123+
expect(instance.observeNode).toHaveBeenCalled()
124+
})
125+
126+
it('Should recreate observer when root change', () => {
127+
const wrapper = mount(<Observer>Content</Observer>)
128+
const instance = wrapper.instance()
129+
jest.spyOn(instance, 'observeNode')
130+
131+
// Changing threshold should cause the instance to be observed once more
132+
wrapper.setProps({ rootMargin: '10px' })
133+
expect(intersection.unobserve).toHaveBeenCalledWith(wrapper.getDOMNode())
134+
expect(instance.observeNode).toHaveBeenCalled()
135+
})
136+
137+
it('Should trigger onChange callback', () => {
138+
const onChange = jest.fn()
139+
const wrapper = mount(<Observer onChange={onChange}>Content</Observer>)
140+
wrapper.instance().handleChange(true)
141+
expect(onChange).toHaveBeenLastCalledWith(true)
142+
wrapper.instance().handleChange(false)
143+
expect(onChange).toHaveBeenLastCalledWith(false)
144+
})
145+
146+
it('Should unobserve when triggerOnce comes into view', () => {
147+
const wrapper = mount(<Observer triggerOnce>Content</Observer>)
148+
wrapper.setState({ inView: true })
149+
const node = wrapper.getDOMNode()
150+
expect(intersection.unobserve).toHaveBeenCalledWith(node)
151+
})
152+
153+
it('Should unobserve when unounted', () => {
154+
const wrapper = mount(<Observer>Content</Observer>)
155+
const node = wrapper.getDOMNode()
156+
wrapper.instance().componentWillUnmount()
157+
expect(intersection.unobserve).toHaveBeenCalledWith(node)
158+
})
159+
64160
it('Should throw error when not passing ref', () => {
65161
invariant.mockReset()
66162

0 commit comments

Comments
 (0)