Adds the neutral HTML, the data block helpers and the golden files of the host injection
cmsHtml.ts writes the plain list and banner HTML that sits between the CMS markers; contract/host-injection holds the inputs, the expected HTML and the rules the CMS host's C# template must follow. cmsData.ts reads, validates and writes the cms-data block. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
bakedCmsData,
|
||||
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']);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user