Test showing that a mismatch at the root recovers with client render but shows loading state (#22873)

* rm console.log

* gate test

* move test

* show fallback state
This commit is contained in:
salazarm
2021-12-06 12:36:02 -05:00
committed by GitHub
parent 12bffc78d8
commit c7917fe769
@@ -3014,4 +3014,67 @@ describe('ReactDOMServerPartialHydration', () => {
);
itHydratesWithoutMismatch('an empty string in class component', TestAppClass);
// @gate enableClientRenderFallbackOnHydrationMismatch
it('fallback to client render on hydration mismatch at root', async () => {
let isClient = false;
let suspend = true;
let resolve;
const promise = new Promise((res, rej) => {
resolve = () => {
suspend = false;
res();
};
});
function App() {
return (
<>
<Suspense fallback={<div>Loading</div>}>
<ChildThatSuspends id={1} />
</Suspense>
{isClient ? <span>client</span> : <div>server</div>}
<Suspense fallback={<div>Loading</div>}>
<ChildThatSuspends id={2} />
</Suspense>
</>
);
}
function ChildThatSuspends({id}) {
if (isClient && suspend) {
throw promise;
}
return <div>{id}</div>;
}
const finalHTML = ReactDOMServer.renderToString(<App />);
const container = document.createElement('div');
document.body.appendChild(container);
container.innerHTML = finalHTML;
isClient = true;
expect(() => {
act(() => {
ReactDOM.hydrateRoot(container, <App />);
});
}).toErrorDev(
'Warning: An error occurred during hydration. ' +
'The server HTML was replaced with client content in <div>.',
{withoutStack: true},
);
// We show fallback state when mismatch happens at root
expect(container.innerHTML).toEqual(
'<div>Loading</div><span>client</span><div>Loading</div>',
);
await act(async () => {
resolve();
await promise;
});
expect(container.innerHTML).toEqual(
'<div>1</div><span>client</span><div>2</div>',
);
});
});