diff --git a/web/src/hydration.test.tsx b/web/src/hydration.test.tsx index 4e5303e..7b7a82c 100644 --- a/web/src/hydration.test.tsx +++ b/web/src/hydration.test.tsx @@ -76,16 +76,30 @@ describe('homepage ?pakket= on a direct load', () => { // Start does not recognise the catch-all match of another path (its id holds // the path), so the browser renders that match itself. Its first render must // be the page the HTML shows, or React reports a mismatch (BR-404-05, V12). +// +// Router keeps the pending copy of a route on screen for at least +// pendingMinMs (500 ms by default) and then replaces it with the real page: +// the heading found first is detached a moment later (measured: found after +// 6 to 40 ms, replaced after about 480 ms). On a slow machine that happens +// between finding and asserting. So look the element up again at every +// check, and let the swap happen before the hydration errors are read. +const PENDING_MIN_MS = 500; + +async function settled(find: () => HTMLElement) { + await waitFor(() => expect(find()).toBeInTheDocument()); + // This timer is started after the router's own, so it fires after the swap + await new Promise((resolve) => setTimeout(resolve, PENDING_MIN_MS + 100)); + await waitFor(() => expect(find()).toBeInTheDocument()); +} + describe('404 page served for another URL', () => { it.each(['/onbekend', '/404.html', '/over-mij/x'])( 'hydrates %s without a mismatch', async (url) => { const { errors } = await hydrateAppAsStart('/404', url); - expect( - await screen.findByRole('heading', { - name: 'Pagina niet gevonden', - }), - ).toBeInTheDocument(); + await settled(() => + screen.getByRole('heading', { name: 'Pagina niet gevonden' }), + ); expect(errors).toEqual([]); }, ); @@ -95,9 +109,7 @@ describe('404 page served for another URL', () => { '/404', '/soorten-fotoshoots/onbekend', ); - expect( - await screen.findByTestId('status-page-section-link'), - ).toBeInTheDocument(); + await settled(() => screen.getByTestId('status-page-section-link')); expect(errors).toEqual([]); }); });