Files
AndreaSlopFotografie/web/src/lib/cmsData.test.ts
T
SluijsensandClaude Sonnet 5.5 ad0b114828 Hero from the CMS: markers HERO-HTML and HERO-PRELOAD, hero key in the data block, picture per ratio, baked fallback
The prerendered hero is plain HTML between the markers (first photo, h1, buttons), the slider takes over after hydration with the hero of the host block, the API or the build. The head preload is added to the finished HTML. Hero texts move into nl.json. The stand-in host and the e2e tests cover the hero.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 16:48:00 +01:00

206 lines
6.4 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('passes the hero answer on unchecked', () => {
expect(
parseCmsData(JSON.stringify({ hero: { title: 'T' } }))?.hero,
).toEqual({ title: 'T' });
expect(parseCmsData('{}')?.hero).toBeUndefined();
});
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,
});
});
});