Reads the shoot packages and an optional banner from the CMS at build time
The build asks GET /api/v1/offerings and bakes the packages into the pages, with a committed snapshot as fallback so a CMS outage never breaks the build or shows an empty price. The form choices and ?pakket= use the same list. An offering with layout=banner becomes an optional band under the price cards. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { 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',
|
||||
});
|
||||
expect(result?.banner).toBeNull();
|
||||
});
|
||||
|
||||
it('houdt de banner buiten de pakketten en neemt de eerste bruikbare', () => {
|
||||
const result = mapOfferings([
|
||||
card(),
|
||||
card({
|
||||
slug: 'actie',
|
||||
title: 'Kerstactie',
|
||||
price: '€ 99 i.p.v. € 150',
|
||||
layout: 'banner',
|
||||
displayOrder: 9,
|
||||
}),
|
||||
card({
|
||||
slug: 'actie2',
|
||||
title: 'Tweede',
|
||||
layout: 'banner',
|
||||
displayOrder: 10,
|
||||
}),
|
||||
]);
|
||||
expect(result?.packages.map((p) => p.id)).toEqual(['newborn']);
|
||||
expect(result?.banner).toMatchObject({
|
||||
title: 'Kerstactie',
|
||||
priceText: '€ 99 i.p.v. € 150',
|
||||
});
|
||||
});
|
||||
|
||||
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([card({ layout: 'banner' })])).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);
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user