Skip to content

Commit e6f2186

Browse files
committed
attempting to trigger the issue @sebmarkbage described
facebook#8751 (comment)
1 parent 41bdb14 commit e6f2186

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ describe('ref swapping', () => {
230230
expect(refHopsAround.refs.divThreeRef).toEqual(thirdDiv);
231231
});
232232

233-
234233
it('always has a value for this.refs', () => {
235234
class Component extends React.Component {
236235
render() {
@@ -392,3 +391,42 @@ describe('string refs between fiber and stack', () => {
392391
}
393392
});
394393
});
394+
395+
describe('root level refs', () => {
396+
it('attaches and detaches root refs in stack', () => {
397+
assertForRenderer(require('ReactDOM'));
398+
});
399+
400+
it('attaches and detaches root refs in fiber', () => {
401+
assertForRenderer(require('ReactDOMFiber'));
402+
});
403+
404+
const assertForRenderer = (DOM) => {
405+
spyOn(console, 'error');
406+
var ref = jest.fn();
407+
var container = document.createElement('div');
408+
var result = DOM.render(<div ref={ref} />, container);
409+
expect(ref).toHaveBeenCalledTimes(1);
410+
expect(ref.mock.calls[0][0]).toBeInstanceOf(HTMLDivElement);
411+
expect(result).toBe(ref.mock.calls[0][0]);
412+
DOM.unmountComponentAtNode(container);
413+
expect(ref).toHaveBeenCalledTimes(2);
414+
expect(ref.mock.calls[1][0]).toBe(null);
415+
416+
class Comp extends React.Component {
417+
render() {
418+
return <div>Comp</div>;
419+
}
420+
}
421+
422+
ref = jest.fn();
423+
result = DOM.render(<Comp ref={ref}/>, container);
424+
425+
expect(ref).toHaveBeenCalledTimes(1);
426+
expect(ref.mock.calls[0][0]).toBeInstanceOf(Comp);
427+
expect(result).toBe(ref.mock.calls[0][0]);
428+
DOM.unmountComponentAtNode(container);
429+
expect(ref).toHaveBeenCalledTimes(2);
430+
expect(ref.mock.calls[1][0]).toBe(null);
431+
};
432+
});

0 commit comments

Comments
 (0)