From a966eda77976874d2f585f0703caff67078192e8 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Tue, 19 Apr 2022 16:22:36 -0700 Subject: [PATCH] Add failing test case for #24384 If a components suspends during hydration we expect there to be mismatches with server rendered HTML but we were not always supressing warning messages related to these expected mismatches --- .../src/__tests__/ReactDOMFizzServer-test.js | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js index da9097c51d5e3..d8637417d4033 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js @@ -2872,4 +2872,94 @@ describe('ReactDOMFizzServer', () => { expect(window.__test_outlet).toBe(1); }); }); + + // @gate experimental + it('#24384: Suspending should halt hydration warnings while still allowing siblings to warm up', async () => { + const makeApp = () => { + let resolve, resolved; + const promise = new Promise(r => { + resolve = () => { + resolved = true; + return r(); + }; + }); + function ComponentThatSuspends() { + if (!resolved) { + throw promise; + } + return

A

; + } + + const App = ({text}) => { + return ( +
+ Loading...}> + +

{text}

+
+
+ ); + }; + + return [App, resolve]; + }; + + const [ServerApp, serverResolve] = makeApp(); + await act(async () => { + const {pipe} = ReactDOMFizzServer.renderToPipeableStream( + , + ); + pipe(writable); + }); + await act(() => { + serverResolve(); + }); + + expect(getVisibleChildren(container)).toEqual( +
+

A

+

initial

+
, + ); + + // the client app is rendered with an intentionally incorrect text. The still Suspended component causes + // hydration to fail silently (allowing for cache warming but otherwise skipping this boundary) until it + // resolves. + const [ClientApp, clientResolve] = makeApp(); + ReactDOMClient.hydrateRoot(container, , { + onRecoverableError(error) { + Scheduler.unstable_yieldValue( + 'Logged recoverable error: ' + error.message, + ); + }, + }); + Scheduler.unstable_flushAll(); + + expect(getVisibleChildren(container)).toEqual( +
+

A

+

initial

+
, + ); + + // Now that the boundary resolves to it's children the hydration completes and discovers that there is a mismatch requiring + // client-side rendering. + await clientResolve(); + expect(() => { + expect(Scheduler).toFlushAndYield([ + 'Logged recoverable error: Text content does not match server-rendered HTML.', + 'Logged recoverable error: There was an error while hydrating this Suspense boundary. Switched to client rendering.', + ]); + }).toErrorDev( + 'Warning: Prop `name` did not match. Server: "initial" Client: "replaced"', + ); + expect(getVisibleChildren(container)).toEqual( +
+

A

+

replaced

+
, + ); + + expect(Scheduler).toFlushAndYield([]); + }); });