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>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
bakedCmsData,
|
||||
BUILD_SOURCE,
|
||||
hostCmsData,
|
||||
liveFromData,
|
||||
parseCmsData,
|
||||
rawBanner,
|
||||
rawOfferings,
|
||||
@@ -132,3 +135,64 @@ describe('the baked block', () => {
|
||||
).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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user