Exposed flushSync on the test renderer (#12672)

This commit is contained in:
Brian Vaughn
2018-04-23 10:27:39 -07:00
committed by GitHub
parent d3a064df06
commit 7e6edb96b5
2 changed files with 41 additions and 0 deletions
+1
View File
@@ -736,6 +736,7 @@ const ReactTestRendererFiber = {
}
return TestRenderer.getPublicRootInstance(root);
},
unstable_flushSync: TestRenderer.flushSync,
};
Object.defineProperty(
@@ -94,4 +94,44 @@ describe('ReactTestRendererAsync', () => {
expect(renderer.unstable_flushAll()).toEqual(['C:1']);
expect(renderer.toJSON()).toEqual(['A:1', 'B:1', 'C:1']);
});
it('supports high priority interruptions', () => {
function Child(props) {
renderer.unstable_yield(props.children);
return props.children;
}
class Example extends React.Component {
componentDidMount() {
expect(this.props.step).toEqual(2);
}
componentDidUpdate() {
throw Error('Unexpected update');
}
render() {
return (
<React.Fragment>
<Child>{'A:' + this.props.step}</Child>
<Child>{'B:' + this.props.step}</Child>
</React.Fragment>
);
}
}
const renderer = ReactTestRenderer.create(<Example step={1} />, {
unstable_isAsync: true,
});
// Flush the some of the changes, but don't commit
expect(renderer.unstable_flushThrough(['A:1'])).toEqual(['A:1']);
expect(renderer.toJSON()).toEqual(null);
// Interrupt with higher priority properties
renderer.unstable_flushSync(() => {
renderer.update(<Example step={2} />);
});
// Only the higher priority properties have been committed
expect(renderer.toJSON()).toEqual(['A:2', 'B:2']);
});
});