From a6d4eed534cbbc480577fd52be19d669af679af4 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Fri, 6 Mar 2020 15:03:18 -0800 Subject: [PATCH] Added new failing tests (pending a separate fix to React) --- .../useMutableSource-test.internal.js | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/packages/react-reconciler/src/__tests__/useMutableSource-test.internal.js b/packages/react-reconciler/src/__tests__/useMutableSource-test.internal.js index 3dc1539f7aa5f..3d875ffc89dc4 100644 --- a/packages/react-reconciler/src/__tests__/useMutableSource-test.internal.js +++ b/packages/react-reconciler/src/__tests__/useMutableSource-test.internal.js @@ -809,6 +809,39 @@ describe('useMutableSource', () => { }); }); + it('should recover from a mutation during yield when other work is scheduled', () => { + const source = createSource('one'); + const mutableSource = createMutableSource(source); + + act(() => { + // Start a render that uses the mutable source. + ReactNoop.render( + <> + + + , + ); + expect(Scheduler).toFlushAndYieldThrough(['a:one']); + + // Mutate source + source.value = 'two'; + + // Now render something different. + ReactNoop.render(
); + expect(Scheduler).toFlushAndYield([]); + }); + }); + it('should not throw if the new getSnapshot returns the same snapshot value', () => { const source = createSource('one'); const mutableSource = createMutableSource(source); @@ -1311,6 +1344,65 @@ describe('useMutableSource', () => { ).toThrow('Invalid hook call'); }); }); + + it('should error if multiple renderers of the same type use a mutable source at the same time with mutation between', () => { + const source = createSource('one'); + const mutableSource = createMutableSource(source); + + act(() => { + // Start a render that uses the mutable source. + ReactNoop.render( + <> + + + , + ); + expect(Scheduler).toFlushAndYieldThrough(['a:one']); + + const PrevScheduler = Scheduler; + + // Get a new copy of ReactNoop. + loadModules(); + + spyOnDev(console, 'error'); + + // Mutate before the new render reads from the source. + source.value = 'two'; + + // Use the mutablesource again but with a different renderer. + ReactNoop.render( + , + ); + expect(Scheduler).toFlushAndYieldThrough(['c:two']); + + expect(console.error.calls.argsFor(0)[0]).toContain( + 'Detected multiple renderers concurrently rendering the ' + + 'same mutable source. This is currently unsupported.', + ); + + // TODO (useMutableSource) Act will automatically flush remaining work from render 1, + // but at this point something in the hooks dispatcher has been broken by jest.resetModules() + // Figure out what this is and remove this catch. + expect(() => + PrevScheduler.unstable_flushAllWithoutAsserting(), + ).toThrow('Invalid hook call'); + }); + }); }); } }