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>
189 lines
6.7 KiB
TypeScript
189 lines
6.7 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 hero', () => {
|
|
const hero = {
|
|
title: 'Titel',
|
|
slides: [
|
|
{
|
|
id: 'a',
|
|
alt: 'Alt',
|
|
decorative: false,
|
|
image: { width: 10, height: 8, src: '/m/a.webp' },
|
|
},
|
|
],
|
|
};
|
|
const regions =
|
|
'<!--CMS:HERO-PRELOAD-->old<!--/CMS:HERO-PRELOAD--><!--CMS:HERO-HTML-->old<!--/CMS:HERO-HTML-->';
|
|
|
|
it('writes the hero and its preload and puts the hero in the data block', () => {
|
|
const out = run(
|
|
page(
|
|
regions +
|
|
'<!--CMS:DATA--><script type="application/json" id="cms-data">{}</script>',
|
|
),
|
|
{ ...live([]), hero },
|
|
);
|
|
expect(out).toContain('<h1 class="cms-hero-title">');
|
|
expect(out).toContain(
|
|
'<link rel="preload" as="image" href="/m/a.webp"',
|
|
);
|
|
expect(out).not.toContain('old');
|
|
expect(out).toContain('"hero":{"title":"Titel"');
|
|
});
|
|
|
|
it('keeps the baked hero when there is no usable one', () => {
|
|
for (const data of [live([]), { ...live([]), hero: { title: 'x' } }]) {
|
|
const out = run(page(regions), data);
|
|
expect(out).toBe(page(regions));
|
|
}
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|