// @vitest-environment node
import { ChakraProvider } from '@chakra-ui/react';
import {
createMemoryHistory,
createRouter,
HeadContent,
RouterProvider,
rootRouteId,
} from '@tanstack/react-router';
import type { ReactNode } from 'react';
import { prerender } from 'react-dom/static';
import { describe, expect, it } from 'vitest';
import siteContent from './content/siteContent';
import { emailAddress } from './lib/emailAddress';
import { routerOptions } from './router';
import sitePages from './sitePages';
import system from './theme';
// Fast guard against `window` or `document` in a render path (BR-TST-07):
// every page is rendered in Node, where either one throws, before a full
// build is needed to find out. The build's own prerender stays authoritative.
// The document shell is left out as in renderApp: it renders , and what
// is guarded here is the page inside it.
const TestShell = ({ children }: { children: ReactNode }) => (
<>
{children}
>
);
// Waits for lazy chunks like the real prerender (the form on /contact)
async function prerenderPath(path: string) {
const router = createRouter({
...routerOptions(),
history: createMemoryHistory({ initialEntries: [path] }),
});
const rootOptions = router.routesById[rootRouteId].options as {
shellComponent?: typeof TestShell;
};
rootOptions.shellComponent = TestShell;
await router.load();
const { prelude } = await prerender(
,
);
return new Response(prelude).text();
}
describe('prerender in Node', () => {
it.each([...Object.keys(sitePages), '/onbekend'])(
'renders %s without window or document',
async (path) => {
const html = await prerenderPath(path);
expect(html).not.toBe('');
expect(html).toMatch(/
]/);
},
30_000,
);
it('keeps the assembled address out of the HTML of /contact', async () => {
const address = emailAddress(siteContent.email);
expect(address).toBeTruthy();
const html = await prerenderPath('/contact');
// The form is in the HTML, so the check covers the part that renders
// the address after a failed send (BR-MAIL-06)
expect(html).toContain('data-testid="contact-form"');
expect(html.toLowerCase()).not.toContain(address!.toLowerCase());
});
});