Skip to content

Commit 087f1f6

Browse files
committed
Stack test renderer: cache nodeMock to not call on unmount
1 parent 8354df1 commit 087f1f6

3 files changed

Lines changed: 9 additions & 24 deletions

File tree

src/renderers/shared/shared/__tests__/refs-test.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -290,27 +290,6 @@ describe('ref swapping', () => {
290290
expect(a.refs[1].nodeName).toBe('DIV');
291291
});
292292

293-
it('supports updates with unmounts correctly when using refs', () => {
294-
const ReactDOM = require('ReactDOM');
295-
const container = document.createElement('div');
296-
const log = [];
297-
const ref = element => {
298-
const result = element == null ? null : element.nodeName.toLowerCase();
299-
log.push(result);
300-
return result;
301-
};
302-
class Foo extends React.Component {
303-
render() {
304-
return this.props.useDiv
305-
? <div ref={ref}/>
306-
: <span ref={ref} />;
307-
}
308-
}
309-
ReactDOM.render(<Foo useDiv={true} />, container);
310-
ReactDOM.render(<Foo useDiv={false} />, container);
311-
expect(log).toEqual(['div', null, 'span']);
312-
});
313-
314293
});
315294

316295
describe('string refs between fiber and stack', () => {

src/renderers/testing/__tests__/ReactTestRenderer-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ describe('ReactTestRenderer', () => {
392392
{createNodeMock}
393393
);
394394
inst.update(<Foo useDiv={false} />);
395-
// It's called with 'div' twice (mounting and unmounting)
396-
expect(log).toEqual(['div', 'div', 'span']);
395+
expect(log).toEqual(['div', 'span']);
397396
});
398397

399398
it('supports error boundaries', () => {

src/renderers/testing/stack/ReactTestRendererStack.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ function getRenderedHostOrTextFromComponent(component) {
5050
return component;
5151
}
5252

53+
var UNSET = {};
54+
5355
class ReactTestComponent {
5456
_currentElement: ReactElement;
5557
_renderedChildren: null | Object;
5658
_topLevelWrapper: null | ReactInstance;
5759
_hostContainerInfo: null | Object;
60+
_nodeMock: Object;
5861

5962
constructor(element: ReactElement) {
6063
this._currentElement = element;
6164
this._renderedChildren = null;
6265
this._topLevelWrapper = null;
6366
this._hostContainerInfo = null;
67+
this._nodeMock = UNSET;
6468
}
6569

6670
mountComponent(
@@ -93,7 +97,10 @@ class ReactTestComponent {
9397
'hostContainerInfo should be populated before ' +
9498
'getPublicInstance is called.'
9599
);
96-
return hostContainerInfo.createNodeMock(element);
100+
if (this._nodeMock === UNSET) {
101+
this._nodeMock = hostContainerInfo.createNodeMock(element);
102+
}
103+
return this._nodeMock;
97104
}
98105

99106
toJSON(): ReactTestRendererJSON {

0 commit comments

Comments
 (0)