Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed flushSync on the test renderer #12672

Merged
merged 1 commit into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ const ReactTestRendererFiber = {
}
return TestRenderer.getPublicRootInstance(root);
},
unstable_flushSync: TestRenderer.flushSync,
};

Object.defineProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});