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>
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
rootRouteId,
|
||||
} from '@tanstack/react-router';
|
||||
import { act } from '@testing-library/react';
|
||||
import { hydrate as hydrateRouter } from '@tanstack/react-router/ssr/client';
|
||||
import { hydrateRoot } from 'react-dom/client';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { afterEach } from 'vitest';
|
||||
@@ -61,7 +62,7 @@ const TestShell = ({ children }: { children: ReactNode }) => (
|
||||
</>
|
||||
);
|
||||
|
||||
async function appRouterAt(path: string) {
|
||||
function createAppRouter(path: string, shell = TestShell) {
|
||||
const router = createRouter({
|
||||
...routerOptions(),
|
||||
history: createMemoryHistory({ initialEntries: [path] }),
|
||||
@@ -69,13 +70,24 @@ async function appRouterAt(path: string) {
|
||||
const rootOptions = router.routesById[rootRouteId].options as {
|
||||
shellComponent?: typeof TestShell;
|
||||
};
|
||||
rootOptions.shellComponent = TestShell;
|
||||
rootOptions.shellComponent = shell;
|
||||
return router;
|
||||
}
|
||||
|
||||
const appFor = (router: ReturnType<typeof createAppRouter>) => (
|
||||
<ChakraProvider value={system}>
|
||||
<RouterProvider router={router} />
|
||||
</ChakraProvider>
|
||||
);
|
||||
|
||||
// Without HeadContent: it needs the real document, and the head is not what
|
||||
// this hydration compares
|
||||
const BareShell = ({ children }: { children: ReactNode }) => <>{children}</>;
|
||||
|
||||
async function appRouterAt(path: string) {
|
||||
const router = createAppRouter(path);
|
||||
await router.load();
|
||||
return (
|
||||
<ChakraProvider value={system}>
|
||||
<RouterProvider router={router} />
|
||||
</ChakraProvider>
|
||||
);
|
||||
return appFor(router);
|
||||
}
|
||||
|
||||
// renderToString cannot wait for a lazy component: it emits the fallback and a
|
||||
@@ -128,3 +140,48 @@ export async function serverHtml(ui: ReactElement, path = '/') {
|
||||
export async function hydratePage(ui: ReactElement, path = '/') {
|
||||
return hydrate(await withProviders(ui, path), await serverHtml(ui, path));
|
||||
}
|
||||
|
||||
// Like hydrateApp, but the way Start hands over in the browser: the client
|
||||
// router is not loaded first. It takes the matches the server dehydrated
|
||||
// (window.$_TSR) and only adopts those whose id equals its own. Then a match
|
||||
// the server made for another path, such as the catch-all at `/404` seen at
|
||||
// `/onbekend`, is rendered by the browser alone, which hydrateApp cannot show
|
||||
// because its client router has already loaded every match.
|
||||
export async function hydrateAppAsStart(
|
||||
serverPath: string,
|
||||
clientPath: string,
|
||||
) {
|
||||
const serverRouter = createAppRouter(serverPath, BareShell);
|
||||
// Router leaves out its root Suspense boundary on the server and in a
|
||||
// hydrated browser router (router.ssr); jsdom is neither, so say it here
|
||||
serverRouter.ssr = { manifest: undefined };
|
||||
await serverRouter.load();
|
||||
// renderToString puts the document tags (preloads, title, meta) in front of
|
||||
// the markup; in the browser they live in <head>, outside the body that is
|
||||
// hydrated, and e2e checks them (V11)
|
||||
const markup = (await serverMarkup(appFor(serverRouter))).replace(
|
||||
/^(?:<(?:link|meta)\b[^>]*>|<title>[^<]*<\/title>)*/,
|
||||
'',
|
||||
);
|
||||
window.$_TSR = {
|
||||
buffer: [],
|
||||
initialized: false,
|
||||
h: () => {},
|
||||
e: () => {},
|
||||
c: () => {},
|
||||
p: (script) => script(),
|
||||
router: {
|
||||
manifest: undefined,
|
||||
// Start inlines ids with '#' for '/' (dehydrateSsrMatchId)
|
||||
matches: serverRouter.state.matches.map((match) => ({
|
||||
i: match.id.replaceAll('/', '#'),
|
||||
s: match.status,
|
||||
ssr: true,
|
||||
u: match.updatedAt,
|
||||
})),
|
||||
},
|
||||
};
|
||||
const clientRouter = createAppRouter(clientPath, BareShell);
|
||||
await hydrateRouter(clientRouter);
|
||||
return hydrate(appFor(clientRouter), markup);
|
||||
}
|
||||
|
||||
+32
-28
@@ -15,32 +15,36 @@ afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
// jsdom kent geen matchMedia (Chakra-breakpoints, reduced motion) en geen
|
||||
// scrollTo. Tests die het gedrag nodig hebben, overschrijven deze stubs.
|
||||
Object.defineProperty(window, 'matchMedia', {
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: (query: string) => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
addListener: () => {},
|
||||
removeListener: () => {},
|
||||
dispatchEvent: () => false,
|
||||
}),
|
||||
});
|
||||
window.scrollTo = () => {};
|
||||
// The router scrolls to a #hash with scrollIntoView, which jsdom lacks too
|
||||
Element.prototype.scrollIntoView = () => {};
|
||||
// Alleen in jsdom: een test met `@vitest-environment node` (prerender.test.tsx)
|
||||
// heeft geen window, en juist daar moet een render die er toch bij komt falen
|
||||
if (typeof window !== 'undefined') {
|
||||
// jsdom kent geen matchMedia (Chakra-breakpoints, reduced motion) en geen
|
||||
// scrollTo. Tests die het gedrag nodig hebben, overschrijven deze stubs.
|
||||
Object.defineProperty(window, 'matchMedia', {
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: (query: string) => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
addListener: () => {},
|
||||
removeListener: () => {},
|
||||
dispatchEvent: () => false,
|
||||
}),
|
||||
});
|
||||
window.scrollTo = () => {};
|
||||
// The router scrolls to a #hash with scrollIntoView, which jsdom lacks too
|
||||
Element.prototype.scrollIntoView = () => {};
|
||||
|
||||
// jsdom has no ResizeObserver either; the carousel (Zag) needs one. Nothing
|
||||
// is measured in jsdom, so a no-op is enough. IntersectionObserver is left
|
||||
// out on purpose: useInView and DeferredImage are tested without it, and
|
||||
// tests that render the carousel stub it themselves.
|
||||
globalThis.ResizeObserver ??= class {
|
||||
observe() {}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
};
|
||||
// jsdom has no ResizeObserver either; the carousel (Zag) needs one. Nothing
|
||||
// is measured in jsdom, so a no-op is enough. IntersectionObserver is left
|
||||
// out on purpose: useInView and DeferredImage are tested without it, and
|
||||
// tests that render the carousel stub it themselves.
|
||||
globalThis.ResizeObserver ??= class {
|
||||
observe() {}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user