cms-client: hero uit het datablok #cms-data (parseHero, doelcontrole, titelregels en afgeleide kop, useCmsHero) met tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 16:48:55 +01:00
co-authored by Claude Sonnet 5.5
parent 02f3d9e931
commit 72dd381cf9
11 changed files with 364 additions and 0 deletions
@@ -0,0 +1,66 @@
import { describe, expect, it } from 'vitest';
import { parseHero } from '../parseHero';
// The seed of the SLP Software hero in the CMS (scripts/hero-sls.json of the CMS repository).
const seed = {
display: 'single',
title: 'Websites,\napps\n& meer.',
tagline: null,
lead: 'Bij SLP Software kun je terecht.',
buttons: [
{ label: 'Bekijk pakketten', target: '#pakketten' },
{ label: 'Plan een gesprek', target: '#contact' },
],
slides: [],
};
describe('parseHero', () => {
it('reads the seed hero of the CMS', () => {
expect(parseHero(seed)).toEqual({
title: 'Websites,\napps\n& meer.',
lead: 'Bij SLP Software kun je terecht.',
buttons: seed.buttons,
});
});
it('returns null (baked hero) without a usable hero', () => {
for (const value of [null, undefined, 'x', 5, [], {}, { title: '' }, { title: ' \n ' }, { title: 7 }]) {
expect(parseHero(value)).toBeNull();
}
});
it('ignores tagline and slides: a text-only site', () => {
const hero = parseHero({ ...seed, tagline: 'Label', slides: [{ id: 'x' }] });
expect(hero).not.toBeNull();
expect(hero).not.toHaveProperty('tagline');
expect(hero).not.toHaveProperty('slides');
});
it('turns an empty lead into null and trims texts', () => {
expect(parseHero({ ...seed, lead: ' ' })?.lead).toBeNull();
expect(parseHero({ ...seed, lead: null })?.lead).toBeNull();
expect(parseHero({ ...seed, lead: ' Hallo ' })?.lead).toBe('Hallo');
});
it('skips buttons with an unsafe target or an empty label, never repairs them', () => {
const hero = parseHero({
...seed,
buttons: [
{ label: 'Slecht', target: 'javascript:alert(1)' },
{ label: ' ', target: '#contact' },
{ label: 'Goed', target: '/over' },
null,
'x',
],
});
expect(hero?.buttons).toEqual([{ label: 'Goed', target: '/over' }]);
});
it('keeps at most two buttons and accepts zero or a missing list', () => {
const three = [1, 2, 3].map((n) => ({ label: `B${n}`, target: `#b${n}` }));
expect(parseHero({ ...seed, buttons: three })?.buttons).toHaveLength(2);
expect(parseHero({ ...seed, buttons: [] })?.buttons).toEqual([]);
expect(parseHero({ title: 'x' })?.buttons).toEqual([]);
expect(parseHero({ title: 'x', buttons: 'nope' })?.buttons).toEqual([]);
});
});