Skip to content

Commit

Permalink
Add useSyncExternalStore NaN infinite loop test
Browse files Browse the repository at this point in the history
  • Loading branch information
zalishchuk committed Oct 24, 2022
1 parent 528a776 commit 9f7a922
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions compat/test/browser/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,40 @@ describe('React-18-hooks', () => {
expect(scratch.innerHTML).to.equal('<p>hello new world</p>');
});

it('getSnapshot can return NaN without causing infinite loop', () => {
let flush;
const subscribe = sinon.spy(cb => {
flush = cb;
return () => {};
});
let called = false;
const getSnapshot = sinon.spy(() => {
if (called) {
return NaN;
}

return 1;
});

const App = () => {
const value = useSyncExternalStore(subscribe, getSnapshot);
return <p>{value}</p>;
};

act(() => {
render(<App />, scratch);
});
expect(scratch.innerHTML).to.equal('<p>1</p>');
expect(subscribe).to.be.calledOnce;
expect(getSnapshot).to.be.calledThrice;

called = true;
flush();
rerender();

expect(scratch.innerHTML).to.equal('<p>NaN</p>');
});

it('should not call function values on subscription', () => {
let flush;
const subscribe = sinon.spy(cb => {
Expand Down

0 comments on commit 9f7a922

Please sign in to comment.