Skip to content

Commit

Permalink
Add regression test for facebook#14188 (facebook#14197)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored and jetoneza committed Jan 23, 2019
1 parent 8a68c0c commit 6ee3804
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
ReactCache = require('react-cache');
Suspense = React.Suspense;
container = document.createElement('div');
document.body.appendChild(container);

TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
return new Promise((resolve, reject) =>
Expand All @@ -38,6 +39,10 @@ describe('ReactDOMSuspensePlaceholder', () => {
}, ([text, ms]) => text);
});

afterEach(() => {
document.body.removeChild(container);
});

function advanceTimers(ms) {
// Note: This advances Jest's virtual time but not React's. Use
// ReactNoop.expire for that.
Expand Down Expand Up @@ -157,4 +162,64 @@ describe('ReactDOMSuspensePlaceholder', () => {
);
},
);

// Regression test for https://github.com/facebook/react/issues/14188
it('can call findDOMNode() in a suspended component commit phase', async () => {
const log = [];
const Lazy = React.lazy(
() =>
new Promise(resolve =>
resolve({
default() {
return 'lazy';
},
}),
),
);

class Child extends React.Component {
componentDidMount() {
log.push('cDM ' + this.props.id);
ReactDOM.findDOMNode(this);
}
componentDidUpdate() {
log.push('cDU ' + this.props.id);
ReactDOM.findDOMNode(this);
}
render() {
return 'child';
}
}

const buttonRef = React.createRef();
class App extends React.Component {
state = {
suspend: false,
};
handleClick = () => {
this.setState({suspend: true});
};
render() {
return (
<React.Suspense fallback="Loading">
<Child id="first" />
<button ref={buttonRef} onClick={this.handleClick}>
Suspend
</button>
<Child id="second" />
{this.state.suspend && <Lazy />}
</React.Suspense>
);
}
}

ReactDOM.render(<App />, container);

expect(log).toEqual(['cDM first', 'cDM second']);
log.length = 0;

buttonRef.current.dispatchEvent(new MouseEvent('click', {bubbles: true}));
await Lazy;
expect(log).toEqual(['cDU first', 'cDU second']);
});
});

0 comments on commit 6ee3804

Please sign in to comment.