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>
150 lines
5.4 KiB
TypeScript
150 lines
5.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { injectCms, type LiveData } from './hostInjector.ts';
|
|
|
|
const AUTHORED = 'https://andreaslop.nl';
|
|
|
|
const page = (inner: string) => `<html><body>${inner}</body></html>`;
|
|
const pakket = (slug: string, price: string, extra = {}) => ({
|
|
slug,
|
|
title: slug.toUpperCase(),
|
|
price,
|
|
features: ['a'],
|
|
displayOrder: 0,
|
|
...extra,
|
|
});
|
|
const live = (
|
|
offerings: unknown,
|
|
banners: LiveData['banners'] = { top: null, bottom: null },
|
|
): LiveData => ({ offerings, banners });
|
|
const run = (html: string, data: LiveData, publicBaseUrl?: string) =>
|
|
injectCms(html, { data, authoredBaseUrl: AUTHORED, publicBaseUrl });
|
|
|
|
describe('injectCms regions', () => {
|
|
const region = (name: string) =>
|
|
`<!--CMS:${name}-->baked<!--/CMS:${name}-->`;
|
|
|
|
it('writes the lists between the markers and keeps the markers', () => {
|
|
const out = run(
|
|
page(
|
|
region('OFFERINGS-HTML') +
|
|
region('BANNER-TOP-HTML') +
|
|
region('BANNER-BOTTOM-HTML'),
|
|
),
|
|
live([pakket('newborn', '€ 185')], {
|
|
top: { text: 'Boven' },
|
|
bottom: { title: 'Onder' },
|
|
}),
|
|
);
|
|
expect(out).toContain(
|
|
'<!--CMS:OFFERINGS-HTML--><ul class="cms-offerings">',
|
|
);
|
|
expect(out).toContain('€ 185');
|
|
expect(out).toContain('<!--CMS:BANNER-TOP-HTML--><aside');
|
|
expect(out).toContain('<span>Boven</span>');
|
|
expect(out).toContain('<strong>Onder</strong>');
|
|
expect(out).toContain('<!--/CMS:BANNER-BOTTOM-HTML-->');
|
|
expect(out).not.toContain('baked');
|
|
});
|
|
|
|
it('empties a region without data', () => {
|
|
const out = run(page(region('OFFERINGS-HTML')), live([]));
|
|
expect(out).toContain(
|
|
'<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('injectCms data block', () => {
|
|
const baked =
|
|
'<!--CMS:DATA--><script type="application/json" id="cms-data">{"source":"build"}</script>';
|
|
|
|
it('replaces the marker and the baked block by the live one, with the address', () => {
|
|
const out = run(
|
|
page(baked),
|
|
live([pakket('x', '€ 5')]),
|
|
'https://t.nl',
|
|
);
|
|
expect(out).not.toContain('"source"');
|
|
const json = /id="cms-data">(.*?)<\/script>/.exec(out)?.[1] ?? '';
|
|
expect(JSON.parse(json)).toMatchObject({
|
|
siteUrl: 'https://t.nl',
|
|
offerings: [{ slug: 'x' }],
|
|
banners: { top: null, bottom: null },
|
|
});
|
|
});
|
|
|
|
it('writes the block in place of a lone marker, and escapes it', () => {
|
|
const out = run(
|
|
page('<!--CMS:DATA-->'),
|
|
live([pakket('x', '€ 5', { title: '</script><b>' })]),
|
|
);
|
|
expect(out).toContain('<script type="application/json" id="cms-data">');
|
|
expect(out.match(/<\/script>/g)).toHaveLength(1);
|
|
expect(out).toContain('\\u003c/script\\u003e');
|
|
});
|
|
});
|
|
|
|
describe('injectCms JSON-LD and prices', () => {
|
|
const ld = (offers: unknown) =>
|
|
`<!--CMS:OFFER-JSON-LD slug="newborn"--><script type="application/ld+json">${JSON.stringify(
|
|
{ '@graph': [{ '@type': 'Service', offers }] },
|
|
)}</script>`;
|
|
|
|
it('sets the price of the package', () => {
|
|
const out = run(
|
|
page(ld({ '@type': 'Offer', price: 1, priceCurrency: 'EUR' })),
|
|
live([pakket('newborn', '€ 1.250')]),
|
|
);
|
|
expect(out).toContain('"price":1250');
|
|
expect(out).toContain('"priceCurrency":"EUR"');
|
|
expect(out).not.toContain('CMS:OFFER-JSON-LD');
|
|
});
|
|
|
|
it('removes offers when the package is gone or has no readable price', () => {
|
|
for (const offerings of [
|
|
[pakket('gezin', '€ 1')],
|
|
[pakket('newborn', 'op aanvraag')],
|
|
]) {
|
|
const out = run(page(ld({ price: 1 })), live(offerings));
|
|
expect(out).not.toContain('offers');
|
|
}
|
|
});
|
|
|
|
it('leaves a block that is not JSON as it is', () => {
|
|
const broken =
|
|
'<!--CMS:OFFER-JSON-LD slug="newborn"--><script type="application/ld+json">{kapot</script>';
|
|
expect(run(page(broken), live([]))).toContain(broken);
|
|
});
|
|
|
|
it('sets the text of an inline price and leaves unknown slugs alone', () => {
|
|
const out = run(
|
|
page(
|
|
'<span data-cms-price="newborn" x>€ 1</span><span data-cms-price="onbekend">€ 9</span>',
|
|
),
|
|
live([pakket('newborn', '€ 185')]),
|
|
);
|
|
expect(out).toContain(
|
|
'<span data-cms-price="newborn" x>€ 185</span>'.replace(
|
|
'€ ',
|
|
'€' + String.fromCharCode(160),
|
|
),
|
|
);
|
|
expect(out).toContain('<span data-cms-price="onbekend">€ 9</span>');
|
|
});
|
|
});
|
|
|
|
describe('injectCms address', () => {
|
|
it('replaces the written address everywhere when another one is set', () => {
|
|
const html = page(`<link href="${AUTHORED}/a"><i>${AUTHORED}/b</i>`);
|
|
const out = run(html, live([]), 'https://t.nl');
|
|
expect(out).not.toContain(AUTHORED);
|
|
expect(out.match(/https:\/\/t\.nl/g)).toHaveLength(2);
|
|
});
|
|
|
|
it('leaves it when none is set or it is the same', () => {
|
|
const html = page(`<i>${AUTHORED}/b</i>`);
|
|
expect(run(html, live([]))).toBe(html);
|
|
expect(run(html, live([]), AUTHORED)).toBe(html);
|
|
});
|
|
});
|