Skip to content

Commit f6d9dc2

Browse files
author
Luis Merino
committed
fix(reobserve): using externalUnobserver to operate on the right target
1 parent 1ba2769 commit f6d9dc2

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/Sentinel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Sentinel extends Component {
4242
// its threshold prematurely. In this case we don't get any update from the Observer instance.
4343
// We need to guarantee an update, and re-observing is a cheap way to accomplish this.
4444
if (currentRootMargin === rootMargin && currentRootElement === rootElement) {
45-
this.element.unobserve();
46-
this.element.observe();
45+
this.observer.externalUnobserve();
46+
this.observer.observe();
4747
return false;
4848
}
4949
return true;
@@ -59,8 +59,8 @@ class Sentinel extends Component {
5959

6060
return (
6161
<Observer
62-
ref={node => {
63-
this.element = node;
62+
ref={instance => {
63+
this.observer = instance;
6464
}}
6565
disabled={typeof rootElement === 'undefined'}
6666
root={rootElement}

src/__tests__/Sentinel.spec.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import 'intersection-observer';
33
import React from 'react';
44
import renderer from 'react-test-renderer';
5+
import Observer from '@researchgate/react-intersection-observer';
56
import Sentinel from '../Sentinel';
67
import { computeRootMargin } from '../utils';
78

8-
jest.mock('@researchgate/react-intersection-observer');
9-
109
const defaultProps = {
1110
axis: 'y',
1211
threshold: '100px',
@@ -49,32 +48,41 @@ describe('constructor', () => {
4948

5049
describe('render', () => {
5150
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);
5656
});
5757

5858
test('re-renders when setRef callback is called', () => {
5959
const instance = createTree().getInstance();
60-
const renderSpy = jest.spyOn(instance, 'render');
60+
const spy = (instance.setState = jest.fn());
6161
const setRefMock = instance.props.setRef.mock;
6262
const setRefCallback = setRefMock.calls[setRefMock.calls.length - 1][0];
6363
setRefCallback(null);
64-
expect(renderSpy).toHaveBeenCalledTimes(1);
64+
expect(spy).toHaveBeenCalledTimes(1);
6565
});
6666

67-
test('avoids re-render if new props are the same', () => {
67+
test('prevents re-render if root and rootMargin stay the same', () => {
6868
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+
7571
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);
7886
});
7987
});
8088

0 commit comments

Comments
 (0)