The pages carry the CMS markers (data block, list regions, JSON-LD price, inline price); PackagesProvider reads the host's data block after hydration and asks the API only when there is none; the site address comes from the block. A stand-in for the host (config/hostInjector.ts, --cms-data in the preview server) and browser tests check the behaviour with different data. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { renderToString } from 'react-dom/server';
|
|
import { screen } from '@testing-library/react';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { renderWithProviders } from '@/test/renderWithProviders';
|
|
import CmsDataIsland from './CmsDataIsland';
|
|
import CmsRegion from './CmsRegion';
|
|
|
|
describe('CmsRegion', () => {
|
|
it('is plain HTML between two markers on the server', () => {
|
|
const html = renderToString(
|
|
<CmsRegion name="OFFERINGS-HTML" html="<ul><li>x</li></ul>">
|
|
<p>gestijld</p>
|
|
</CmsRegion>,
|
|
);
|
|
expect(html).toContain(
|
|
'<!--CMS:OFFERINGS-HTML--><ul><li>x</li></ul><!--/CMS:OFFERINGS-HTML-->',
|
|
);
|
|
expect(html).not.toContain('gestijld');
|
|
});
|
|
|
|
it('shows the styled content once it runs in the browser', async () => {
|
|
await renderWithProviders(
|
|
<CmsRegion name="OFFERINGS-HTML" html="<ul></ul>">
|
|
<p>gestijld</p>
|
|
</CmsRegion>,
|
|
);
|
|
expect(screen.getByText('gestijld')).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe('CmsDataIsland', () => {
|
|
it('writes the marker and the baked block, hidden, with the build as source', () => {
|
|
const html = renderToString(<CmsDataIsland />);
|
|
expect(html).toMatch(
|
|
/<div hidden="" data-cms-island="true"><!--CMS:DATA--><script type="application\/json" id="cms-data">\{/,
|
|
);
|
|
const json = /id="cms-data">(.*?)<\/script>/.exec(html)?.[1] ?? '';
|
|
const data = JSON.parse(json) as {
|
|
source: string;
|
|
siteUrl: string;
|
|
offerings: unknown[];
|
|
banners: object;
|
|
};
|
|
expect(data.source).toBe('build');
|
|
expect(data.siteUrl).toBe('https://andreaslop.nl');
|
|
expect(data.offerings.length).toBeGreaterThan(0);
|
|
expect(Object.keys(data.banners)).toEqual(['top', 'bottom']);
|
|
// Nothing in it can end the script element
|
|
expect(json).not.toMatch(/[<>&]/);
|
|
});
|
|
});
|