Files
AndreaSlopFotografie/web/src/lib/cmsData.test.ts
T
SluijsensandClaude Sonnet 5.5 bf21b6f690 Builds the markers, data block and stand-in host for the CMS host injection
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>
2026-11-15 16:21:08 +01:00

199 lines
6.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
bakedCmsData,
BUILD_SOURCE,
hostCmsData,
liveFromData,
parseCmsData,
rawBanner,
rawOfferings,
readCmsData,
scriptJson,
} from './cmsData';
import { mapBanner, mapOfferings, mapTopBanner } from './offerings';
import { PRODUCTION_SITE_URL } from './siteUrl';
const packages = [
{
id: 'newborn',
name: 'Newborn',
price: 185,
features: ['a', 'b'],
featured: true,
description: 'd',
priceNote: 'n',
ctaLabel: 'c',
},
{ id: 'gezin', name: 'Gezin', price: 1250, features: ['x'] },
];
describe('parseCmsData', () => {
it('reads the three parts and checks the address', () => {
const data = parseCmsData(
JSON.stringify({
siteUrl: 'https://test.andreaslop.nl/',
offerings: [{ slug: 'x' }],
banners: { top: null, bottom: { title: 'T' } },
}),
);
expect(data).toEqual({
siteUrl: 'https://test.andreaslop.nl',
offerings: [{ slug: 'x' }],
banners: { top: null, bottom: { title: 'T' } },
});
});
it('falls back to production for an address that is not an origin', () => {
expect(
parseCmsData(JSON.stringify({ siteUrl: 'ftp://x' }))?.siteUrl,
).toBe(PRODUCTION_SITE_URL);
});
it('leaves out what is missing or of the wrong kind', () => {
expect(
parseCmsData(JSON.stringify({ offerings: 'x', siteUrl: '' })),
).toEqual({
siteUrl: undefined,
offerings: undefined,
banners: undefined,
});
});
it.each([null, undefined, '', 'niet json', '[1]', '"x"', 'null'])(
'gives null for %s',
(text) => expect(parseCmsData(text)).toBeNull(),
);
});
describe('readCmsData', () => {
it('reads the block of the page', () => {
const doc = {
getElementById: (id: string) =>
id === 'cms-data' ? { textContent: '{"offerings":[]}' } : null,
} as unknown as Document;
expect(readCmsData(doc)?.offerings).toEqual([]);
});
it('gives null without a block', () => {
const doc = { getElementById: () => null } as unknown as Document;
expect(readCmsData(doc)).toBeNull();
});
});
describe('scriptJson', () => {
it('cannot end the script element or contain a line separator', () => {
const text = scriptJson({
a: '</script><!-- & \u2028 \u2029',
});
expect(text).not.toMatch(/[<>&\u2028\u2029]/);
expect(JSON.parse(text)).toEqual({
a: '</script><!-- & \u2028 \u2029',
});
});
});
describe('the baked block', () => {
it('maps back to the same packages and banners', () => {
const data = bakedCmsData(
'https://andreaslop.nl',
packages,
{ title: 'Onder', text: 't', link: { label: 'L', url: '/x' } },
{ text: 'Boven' },
);
expect(data.siteUrl).toBe('https://andreaslop.nl');
const mapped = mapOfferings(data.offerings);
expect(mapped?.packages).toEqual(
packages.map((pkg) => ({
priceNote: undefined,
description: undefined,
ctaLabel: undefined,
featured: undefined,
...pkg,
})),
);
expect(mapBanner(data.banners?.bottom)).toEqual({
title: 'Onder',
text: 't',
link: { label: 'L', url: '/x' },
});
expect(mapTopBanner(data.banners?.top)).toMatchObject({
text: 'Boven',
});
});
it('gives null for no banner and keeps the order', () => {
expect(rawBanner(null, 'top')).toBeNull();
expect(rawBanner({ title: 'Alleen' }, 'bottom')).toEqual({
title: 'Alleen',
text: null,
linkLabel: null,
linkUrl: null,
position: 'bottom',
});
expect(
rawOfferings(packages).map((raw) => (raw as { slug: string }).slug),
).toEqual(['newborn', 'gezin']);
});
});
const docWith = (json: string) =>
({
getElementById: () => ({ textContent: json }),
}) as unknown as Document;
describe('hostCmsData', () => {
it('is the block the host wrote', () => {
expect(hostCmsData(docWith('{"offerings":[]}'))?.offerings).toEqual([]);
});
it('is null for the block the build baked, and for no block', () => {
const baked = JSON.stringify(
bakedCmsData('https://andreaslop.nl', packages, null, null),
);
expect(parseCmsData(baked)?.source).toBe(BUILD_SOURCE);
expect(hostCmsData(docWith(baked))).toBeNull();
expect(
hostCmsData({ getElementById: () => null } as unknown as Document),
).toBeNull();
});
});
describe('liveFromData', () => {
it('gives nothing for no data', () => {
expect(liveFromData(null)).toBeNull();
});
it('takes only what the block has', () => {
expect(liveFromData({ siteUrl: 'https://andreaslop.nl' })).toEqual({});
});
it('trusts the host: an empty or unusable list is no packages', () => {
expect(liveFromData({ offerings: [] })).toEqual({ packages: [] });
expect(liveFromData({ offerings: [{ slug: 'Bad Slug' }] })).toEqual({
packages: [],
});
expect(
liveFromData({
offerings: [
{ slug: 'x', title: 'X', price: '€ 5', features: ['a'] },
],
})?.packages?.map((pkg) => pkg.id),
).toEqual(['x']);
});
it('maps both banners, null meaning none and an unusable answer too', () => {
expect(liveFromData({ banners: { top: null, bottom: null } })).toEqual({
topBanner: null,
banner: null,
});
expect(
liveFromData({
banners: { top: { text: 'Boven' }, bottom: { title: '' } },
}),
).toEqual({
topBanner: { title: undefined, text: 'Boven', link: undefined },
banner: null,
});
});
});