Files
AndreaSlopFotografie/web/src/routes/hydration.test.tsx
T
SluijsensandClaude Opus 5.5 8bf0000531 Adds browser tests and fixes hydration of the 404 page
The prerendered 404 page is served for other URLs, so its dehydrated match id
differs and the router renders the catch-all's pending component first. Without
one that render was empty, which gave React error 418 and duplicate head tags.
The tests run the built output in a real browser under the production CSP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 19:03:07 +01:00

104 lines
3.8 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).
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();
expect(errors).toEqual([]);
},
);
it('shows the section buttons after hydrating under /soorten-fotoshoots/', async () => {
const { errors } = await hydrateAppAsStart(
'/404',
'/soorten-fotoshoots/onbekend',
);
expect(
await screen.findByTestId('status-page-section-link'),
).toBeInTheDocument();
expect(errors).toEqual([]);
});
});