diff --git a/compat/test/browser/hooks.test.js b/compat/test/browser/hooks.test.js index 3255704756..1a4b85a456 100644 --- a/compat/test/browser/hooks.test.js +++ b/compat/test/browser/hooks.test.js @@ -130,6 +130,40 @@ describe('React-18-hooks', () => { expect(scratch.innerHTML).to.equal('

hello new world

'); }); + 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

{value}

; + }; + + act(() => { + render(, scratch); + }); + expect(scratch.innerHTML).to.equal('

1

'); + expect(subscribe).to.be.calledOnce; + expect(getSnapshot).to.be.calledThrice; + + called = true; + flush(); + rerender(); + + expect(scratch.innerHTML).to.equal('

NaN

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