@samitouri / QOS-React / commits / 149a34f735

Exposed flushSync on the test renderer (#12672)

Brian Vaughn committed Apr 23, 2018 at 10:27 UTC 149a34f735b47f225e3d26ce2478b3181e4ff8fe
2 files changed +41
packages/react-test-renderer/src/ReactTestRenderer.js
+1
@@ -736,6 +736,7 @@ const ReactTestRendererFiber = {
736 }
737 return TestRenderer.getPublicRootInstance(root);
738 },
739 + unstable_flushSync: TestRenderer.flushSync,
740 };
741
742 Object.defineProperty(
packages/react-test-renderer/src/__tests__/ReactTestRendererAsync-test.js
+40
@@ -94,4 +94,44 @@ describe('ReactTestRendererAsync', () => {
94 expect(renderer.unstable_flushAll()).toEqual(['C:1']);
95 expect(renderer.toJSON()).toEqual(['A:1', 'B:1', 'C:1']);
96 });
97 +
98 + it('supports high priority interruptions', () => {
99 + function Child(props) {
100 + renderer.unstable_yield(props.children);
101 + return props.children;
102 + }
103 +
104 + class Example extends React.Component {
105 + componentDidMount() {
106 + expect(this.props.step).toEqual(2);
107 + }
108 + componentDidUpdate() {
109 + throw Error('Unexpected update');
110 + }
111 + render() {
112 + return (
113 + <React.Fragment>
114 + <Child>{'A:' + this.props.step}</Child>
115 + <Child>{'B:' + this.props.step}</Child>
116 + </React.Fragment>
117 + );
118 + }
119 + }
120 +
121 + const renderer = ReactTestRenderer.create(<Example step={1} />, {
122 + unstable_isAsync: true,
123 + });
124 +
125 + // Flush the some of the changes, but don't commit
126 + expect(renderer.unstable_flushThrough(['A:1'])).toEqual(['A:1']);
127 + expect(renderer.toJSON()).toEqual(null);
128 +
129 + // Interrupt with higher priority properties
130 + renderer.unstable_flushSync(() => {
131 + renderer.update(<Example step={2} />);
132 + });
133 +
134 + // Only the higher priority properties have been committed
135 + expect(renderer.toJSON()).toEqual(['A:2', 'B:2']);
136 + });
137 });