|
1 | 1 | import React from 'react' |
2 | 2 | import { mount } from 'enzyme' |
3 | 3 | import Observer from '../src/index.js' |
| 4 | +import intersection from '../src/intersection' |
4 | 5 | import invariant from 'invariant' |
5 | 6 |
|
6 | 7 | jest.mock('../src/intersection') |
7 | 8 | jest.mock('invariant') |
8 | 9 |
|
| 10 | +afterEach(() => {}) |
| 11 | + |
9 | 12 | it('Should render <Observer />', () => { |
10 | 13 | const callback = jest.fn() |
11 | 14 | mount(<Observer>{callback}</Observer>) |
@@ -61,6 +64,99 @@ it('Should render <Observer /> render when in view', () => { |
61 | 64 | expect(wrapper).toMatchSnapshot() |
62 | 65 | }) |
63 | 66 |
|
| 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 | + |
64 | 160 | it('Should throw error when not passing ref', () => { |
65 | 161 | invariant.mockReset() |
66 | 162 |
|
|
0 commit comments