A featured card gets a heidegroen top rule, border and a small "Uitgelicht" tab on its top edge; the tab sits on the border so the row keeps its layout. Tests and docs state that the site does not depend on description, price note or button label. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
231 lines
7.4 KiB
TypeScript
231 lines
7.4 KiB
TypeScript
import fc from 'fast-check';
|
|
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
isSafeBannerUrl,
|
|
mapBanner,
|
|
mapTopBanner,
|
|
mapOfferings,
|
|
parsePriceText,
|
|
} from './offerings';
|
|
|
|
const card = (over: Record<string, unknown> = {}) => ({
|
|
id: 'x',
|
|
slug: 'newborn',
|
|
title: 'Newborn',
|
|
description: 'Mooie shoot',
|
|
price: '€ 185',
|
|
priceNote: 'incl. galerij',
|
|
features: ['1-2 uur fotografie', 'Online galerij'],
|
|
ctaLabel: 'Boek',
|
|
featured: false,
|
|
displayOrder: 2,
|
|
categoryId: null,
|
|
category: null,
|
|
layout: 'card',
|
|
...over,
|
|
});
|
|
|
|
describe('parsePriceText', () => {
|
|
it.each([
|
|
['€ 175', 175],
|
|
['€ 1.250', 1250],
|
|
['175,-', 175],
|
|
['€ 175,00', 175],
|
|
['€\u00a0185', 185],
|
|
])('leest %s als %i', (text, euros) => {
|
|
expect(parsePriceText(text)).toBe(euros);
|
|
});
|
|
|
|
it.each(['', 'op aanvraag', '€ 12,50', '€ 0', '175 of 185', null, 12])(
|
|
'geeft geen prijs voor %j',
|
|
(text) => {
|
|
expect(parsePriceText(text)).toBeNull();
|
|
},
|
|
);
|
|
|
|
it('geeft terug wat formatteren als hele euros oplevert', () => {
|
|
fc.assert(
|
|
fc.property(fc.integer({ min: 1, max: 99_999 }), (euros) => {
|
|
const grouped = euros.toLocaleString('nl-NL');
|
|
expect(parsePriceText(`€ ${grouped}`)).toBe(euros);
|
|
expect(parsePriceText(`€ ${euros}`)).toBe(euros);
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('mapOfferings', () => {
|
|
it('maakt pakketten in volgorde van displayOrder', () => {
|
|
const result = mapOfferings([
|
|
card({ slug: 'gezin', title: 'Gezin', displayOrder: 3 }),
|
|
card({
|
|
slug: 'zwangerschap',
|
|
title: 'Zwangerschap',
|
|
displayOrder: 1,
|
|
}),
|
|
card(),
|
|
]);
|
|
expect(result?.packages.map((p) => p.id)).toEqual([
|
|
'zwangerschap',
|
|
'newborn',
|
|
'gezin',
|
|
]);
|
|
expect(result?.packages[1]).toMatchObject({
|
|
name: 'Newborn',
|
|
price: 185,
|
|
priceNote: 'incl. galerij',
|
|
ctaLabel: 'Boek',
|
|
});
|
|
});
|
|
|
|
it('neemt featured over alleen als het true is', () => {
|
|
const result = mapOfferings([
|
|
card({ slug: 'a', featured: true }),
|
|
card({ slug: 'b', featured: 'ja' }),
|
|
]);
|
|
expect(result?.packages.map((p) => p.featured)).toEqual([
|
|
true,
|
|
undefined,
|
|
]);
|
|
});
|
|
|
|
it('slaat ongeldige kaarten over en behoudt de rest', () => {
|
|
const result = mapOfferings([
|
|
card({ slug: 'Slecht Id' }),
|
|
card({ slug: 'a', price: 'op aanvraag' }),
|
|
card({ slug: 'b', title: ' ' }),
|
|
card({ slug: 'c', features: [] }),
|
|
card({ slug: 'd', features: ['ok', ''] }),
|
|
card({ slug: 'newborn' }),
|
|
card({ slug: 'newborn', title: 'Dubbel' }),
|
|
]);
|
|
expect(result?.packages.map((p) => p.id)).toEqual(['newborn']);
|
|
expect(result?.packages[0].name).toBe('Newborn');
|
|
});
|
|
|
|
it('verwerpt een antwoord zonder geldig pakket', () => {
|
|
expect(mapOfferings([])).toBeNull();
|
|
expect(mapOfferings([card({ price: '' })])).toBeNull();
|
|
expect(mapOfferings({ items: [] })).toBeNull();
|
|
expect(mapOfferings(null)).toBeNull();
|
|
expect(mapOfferings('fout')).toBeNull();
|
|
});
|
|
|
|
it('geeft nooit een pakket met lege of ongeldige prijs terug, wat er ook komt', () => {
|
|
fc.assert(
|
|
fc.property(fc.array(fc.jsonValue()), (answer) => {
|
|
const result = mapOfferings(answer);
|
|
for (const pkg of result?.packages ?? []) {
|
|
expect(Number.isInteger(pkg.price)).toBe(true);
|
|
expect(pkg.price).toBeGreaterThan(0);
|
|
expect(pkg.features.length).toBeGreaterThan(0);
|
|
}
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('mapOfferings without description, price note and button label', () => {
|
|
const bare = { slug: 'x', title: 'X', price: '€ 100', features: ['a'] };
|
|
|
|
it.each([
|
|
['absent', {}],
|
|
['null', { description: null, priceNote: null, ctaLabel: null }],
|
|
['empty', { description: '', priceNote: ' ', ctaLabel: '' }],
|
|
])('still gives the card when they are %s', (_name, extra) => {
|
|
const mapped = mapOfferings([{ ...bare, ...extra }]);
|
|
expect(mapped?.packages).toHaveLength(1);
|
|
expect(mapped?.packages[0]).toMatchObject({ id: 'x', price: 100 });
|
|
expect(mapped?.packages[0].description).toBeUndefined();
|
|
expect(mapped?.packages[0].priceNote).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('mapBanner', () => {
|
|
it('neemt titel, tekst en een volledige link', () => {
|
|
expect(
|
|
mapBanner({
|
|
title: 'Kerstactie',
|
|
text: 'Alleen in december',
|
|
linkLabel: 'Boek nu',
|
|
linkUrl: '/contact',
|
|
}),
|
|
).toEqual({
|
|
title: 'Kerstactie',
|
|
text: 'Alleen in december',
|
|
link: { label: 'Boek nu', url: '/contact' },
|
|
});
|
|
});
|
|
|
|
it('laat lege velden en een halve of onveilige link weg', () => {
|
|
expect(
|
|
mapBanner({ title: 'T', text: '', linkLabel: null, linkUrl: null }),
|
|
).toEqual({ title: 'T', text: undefined, link: undefined });
|
|
expect(
|
|
mapBanner({ title: 'T', linkLabel: 'Hier', linkUrl: null })?.link,
|
|
).toBeUndefined();
|
|
expect(
|
|
mapBanner({
|
|
title: 'T',
|
|
linkLabel: 'Hier',
|
|
linkUrl: 'javascript:x',
|
|
})?.link,
|
|
).toBeUndefined();
|
|
});
|
|
|
|
it('geeft null zonder titel of zonder object', () => {
|
|
expect(mapBanner({ title: ' ' })).toBeNull();
|
|
expect(mapBanner(null)).toBeNull();
|
|
expect(mapBanner('x')).toBeNull();
|
|
expect(mapBanner([{ title: 'T' }])).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('mapTopBanner', () => {
|
|
it('accepts a title, a text or both, and a complete safe link', () => {
|
|
expect(mapTopBanner({ title: 'T', text: 'x' })).toMatchObject({
|
|
title: 'T',
|
|
text: 'x',
|
|
});
|
|
expect(mapTopBanner({ title: '', text: 'x' })?.title).toBeUndefined();
|
|
expect(mapTopBanner({ title: 'T', text: null })?.text).toBeUndefined();
|
|
expect(
|
|
mapTopBanner({
|
|
text: 'x',
|
|
linkLabel: 'Hier',
|
|
linkUrl: '/contact',
|
|
})?.link,
|
|
).toEqual({ label: 'Hier', url: '/contact' });
|
|
expect(
|
|
mapTopBanner({
|
|
text: 'x',
|
|
linkLabel: 'Hier',
|
|
linkUrl: 'http://a.nl',
|
|
})?.link,
|
|
).toBeUndefined();
|
|
});
|
|
|
|
it('rejects an answer with neither title nor text', () => {
|
|
expect(mapTopBanner({ title: ' ', text: '' })).toBeNull();
|
|
expect(mapTopBanner(null)).toBeNull();
|
|
expect(mapTopBanner([{ title: 'T' }])).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('isSafeBannerUrl', () => {
|
|
it.each(['https://example.nl/x', '/contact', '/#contact', '/a?b=1'])(
|
|
'staat %s toe',
|
|
(url) => expect(isSafeBannerUrl(url)).toBe(true),
|
|
);
|
|
it.each([
|
|
'http://example.nl',
|
|
'//evil.nl',
|
|
'javascript:alert(1)',
|
|
'mailto:a@b.nl',
|
|
'contact',
|
|
`/${String.fromCharCode(92)}evil`,
|
|
'https://',
|
|
'',
|
|
])('weigert %s', (url) => expect(isSafeBannerUrl(url)).toBe(false));
|
|
});
|