The router swaps its pending copy for the real page after 500 ms, so a heading found before that can leave the document before the assertion. CI on the Pi caught this. The page itself shows identical content during the swap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
4.5 KiB
TypeScript
116 lines
4.5 KiB
TypeScript
import { screen, waitFor, within } from '@testing-library/react';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver';
|
|
import { hydrateApp, hydrateAppAsStart } from '@/test/hydrate';
|
|
|
|
// A direct load of a page with a search: the HTML was prerendered without it,
|
|
// so the first browser render must ignore it and the page applies it right
|
|
// after hydrating (BR-HYD-10 to 12). React reports a mismatch through
|
|
// onRecoverableError, which hydrate() collects.
|
|
beforeEach(() => {
|
|
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver);
|
|
Element.prototype.scrollTo = () => {};
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
FakeIntersectionObserver.reset();
|
|
// @ts-expect-error -- jsdom has no Element.scrollTo of its own
|
|
delete Element.prototype.scrollTo;
|
|
});
|
|
|
|
describe('portfolio ?shoot= on a direct load', () => {
|
|
it('hydrates without a mismatch and then applies the filter', async () => {
|
|
const { errors } = await hydrateApp(
|
|
'/portfolio',
|
|
'/portfolio?shoot=gezin',
|
|
);
|
|
await waitFor(() =>
|
|
expect(
|
|
screen.getByRole('button', { name: 'Gezin', pressed: true }),
|
|
).toBeInTheDocument(),
|
|
);
|
|
expect(
|
|
within(screen.getByTestId('portfolio-grid')).getAllByRole('link'),
|
|
).toHaveLength(2);
|
|
expect(errors).toEqual([]);
|
|
});
|
|
|
|
it('hydrates a plain /portfolio with the whole list', async () => {
|
|
const { errors } = await hydrateApp('/portfolio', '/portfolio');
|
|
expect(
|
|
await screen.findByRole('button', { name: 'Alles', pressed: true }),
|
|
).toBeInTheDocument();
|
|
expect(errors).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('contact ?pakket= on a direct load', () => {
|
|
it('hydrates without a mismatch and then preselects the package', async () => {
|
|
const { errors } = await hydrateApp(
|
|
'/contact',
|
|
'/contact?pakket=newborn',
|
|
);
|
|
expect(
|
|
await screen.findByTestId('contact-form-shoot', undefined, {
|
|
timeout: 10_000,
|
|
}),
|
|
).toHaveValue('newborn');
|
|
expect(errors).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('homepage ?pakket= on a direct load', () => {
|
|
it('hydrates without a mismatch and then preselects the package', async () => {
|
|
const { errors } = await hydrateApp('/', '/?pakket=newborn');
|
|
expect(
|
|
await screen.findByTestId('contact-form-shoot', undefined, {
|
|
timeout: 10_000,
|
|
}),
|
|
).toHaveValue('newborn');
|
|
expect(errors).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// The prerendered 404 page is made for /404 and served for any unknown URL.
|
|
// 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);
|
|
await settled(() =>
|
|
screen.getByRole('heading', { name: 'Pagina niet gevonden' }),
|
|
);
|
|
expect(errors).toEqual([]);
|
|
},
|
|
);
|
|
|
|
it('shows the section buttons after hydrating under /soorten-fotoshoots/', async () => {
|
|
const { errors } = await hydrateAppAsStart(
|
|
'/404',
|
|
'/soorten-fotoshoots/onbekend',
|
|
);
|
|
await settled(() => screen.getByTestId('status-page-section-link'));
|
|
expect(errors).toEqual([]);
|
|
});
|
|
});
|