|
2 | 2 | import 'intersection-observer';
|
3 | 3 | import React from 'react';
|
4 | 4 | import renderer from 'react-test-renderer';
|
| 5 | +import Observer from '@researchgate/react-intersection-observer'; |
5 | 6 | import Sentinel from '../Sentinel';
|
6 | 7 | import { computeRootMargin } from '../utils';
|
7 | 8 |
|
8 |
| -jest.mock('@researchgate/react-intersection-observer'); |
9 |
| - |
10 | 9 | const defaultProps = {
|
11 | 10 | axis: 'y',
|
12 | 11 | threshold: '100px',
|
@@ -49,32 +48,41 @@ describe('constructor', () => {
|
49 | 48 |
|
50 | 49 | describe('render', () => {
|
51 | 50 | test('first time sets a disabled observer', () => {
|
52 |
| - const spy = require('@researchgate/react-intersection-observer').default; |
53 |
| - createTree(); |
54 |
| - expect(spy.mock.calls[spy.mock.calls.length - 1][0]).toHaveProperty('disabled', true); |
55 |
| - expect(spy.mock.calls[spy.mock.calls.length - 1][0]).toHaveProperty('root', undefined); |
| 51 | + const testRenderer = createTree().root; |
| 52 | + const { props } = testRenderer.findByType(Observer); |
| 53 | + |
| 54 | + expect(props).toHaveProperty('disabled', true); |
| 55 | + expect(props).toHaveProperty('root', undefined); |
56 | 56 | });
|
57 | 57 |
|
58 | 58 | test('re-renders when setRef callback is called', () => {
|
59 | 59 | const instance = createTree().getInstance();
|
60 |
| - const renderSpy = jest.spyOn(instance, 'render'); |
| 60 | + const spy = (instance.setState = jest.fn()); |
61 | 61 | const setRefMock = instance.props.setRef.mock;
|
62 | 62 | const setRefCallback = setRefMock.calls[setRefMock.calls.length - 1][0];
|
63 | 63 | setRefCallback(null);
|
64 |
| - expect(renderSpy).toHaveBeenCalledTimes(1); |
| 64 | + expect(spy).toHaveBeenCalledTimes(1); |
65 | 65 | });
|
66 | 66 |
|
67 |
| - test('avoids re-render if new props are the same', () => { |
| 67 | + test('prevents re-render if root and rootMargin stay the same', () => { |
68 | 68 | const tree = createTree();
|
69 |
| - const renderSpy = jest.spyOn(tree.getInstance(), 'render'); |
70 |
| - const spy = jest.fn(); |
71 |
| - tree.getInstance().element = { |
72 |
| - unobserve: spy, |
73 |
| - observe: spy, |
74 |
| - }; |
| 69 | + const spy = jest.spyOn(tree.getInstance(), 'render'); |
| 70 | + |
75 | 71 | tree.update(<Sentinel {...defaultProps} />);
|
76 |
| - expect(renderSpy).not.toBeCalled(); |
77 |
| - expect(spy).toBeCalled(); |
| 72 | + expect(spy).not.toBeCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('does re-observer if root and rootMargin stay the same', () => { |
| 76 | + const tree = createTree(); |
| 77 | + const instance = tree.getInstance(); |
| 78 | + |
| 79 | + const spy1 = jest.spyOn(instance.observer, 'externalUnobserve'); |
| 80 | + const spy2 = jest.spyOn(instance.observer, 'observe'); |
| 81 | + |
| 82 | + tree.update(<Sentinel {...defaultProps} />); |
| 83 | + |
| 84 | + expect(spy1).toHaveBeenCalledTimes(1); |
| 85 | + expect(spy2).toHaveBeenCalledTimes(1); |
78 | 86 | });
|
79 | 87 | });
|
80 | 88 |
|
|
0 commit comments