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: '