import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import sleepingBaby from '../assets/portfolio/f41a8700.jpg'; import packages from '../content/packages'; import shootPageShared from '../content/shootPageShared'; import loveshootPage from '../content/shootPages/loveshoot'; import newbornPage from '../content/shootPages/newborn'; import shootTypes from '../content/shootTypes'; import siteContent from '../content/siteContent'; import type { ContentImage, RichText } from '../content/types'; import { anyPackage, anySiteContent, contentImage, knownPlaceholders, plainText, shootPageContent, shootPageShared as anyShared, shootType, shootTypeId, } from '../test/arbitraries'; import { shootPackage } from './shootPackage'; import { SHOOT_TYPE_IDS, shootPagePath } from './shootPages'; import { MOSAIC_MIN_PHOTOS, PROCESS_STEP_ORDER, shootPageView, type ShootPageSources, type ShootPageView, } from './shootPageView'; import { visiblePackages } from './visiblePackages'; const NBSP = ' '; const sources: ShootPageSources = { shootTypes, packages, shared: shootPageShared, site: siteContent, portfolioShoots: ['newborn'], }; // Shoot types with unique ids, some with a detail photo const shoots = fc.uniqueArray( fc .tuple(shootType, fc.option(contentImage, { nil: undefined })) .map(([shoot, detailImage]) => detailImage === undefined ? shoot : { ...shoot, detailImage }, ), { selector: (shoot) => shoot.id, maxLength: 5 }, ); // The sources as the CMS could deliver them. The reply time has no braces, // as in the content: it is filled in as a value. const anySources: fc.Arbitrary = fc.record({ shootTypes: shoots, packages: fc.array(anyPackage, { maxLength: 6 }), shared: anyShared, site: fc.tuple(anySiteContent, plainText).map(([site, replyTime]) => ({ ...site, contactForm: { ...site.contactForm, replyTime }, })), portfolioShoots: fc.subarray([...SHOOT_TYPE_IDS]), }); // A shoot id (mostly one of the sources), the sources, and page content // that mostly belongs to that shoot, its photos repeating the shoot's own const viewCase = anySources.chain((given) => { const ids = given.shootTypes.map((shoot) => shoot.id); const id = ids.length > 0 ? fc.oneof( { arbitrary: fc.constantFrom(...ids), weight: 4 }, shootTypeId, ) : shootTypeId; return id.chain((shootId) => { const shoot = given.shootTypes.find((item) => item.id === shootId); const reuse = shoot === undefined ? [] : [ shoot.image, ...(shoot.detailImage ? [shoot.detailImage] : []), ]; return fc.record({ id: fc.constant(shootId), sources: fc.constant(given), page: fc.oneof( { arbitrary: shootPageContent(shootId, reuse), weight: 4 }, shootPageContent(undefined, reuse), ), }); }); }); // A case for which the view exists const resolvedCase = viewCase.filter( ({ id, page, sources: given }) => page.id === id && given.shootTypes.some((shoot) => shoot.id === id), ); const build = (c: { id: Parameters[0]; page: Parameters[1]; sources: ShootPageSources; }) => shootPageView(c.id, c.page, c.sources) as ShootPageView; const plain = (text: RichText) => text.map((piece) => (typeof piece === 'string' ? piece : piece.text)); // Every text in which placeholders are filled in const filledTexts = (view: ShootPageView) => [ ...view.stories.flatMap((story) => [ story.eyebrow, story.title, ...story.paragraphs.flatMap(plain), ]), ...view.process.steps.map((step) => step.text), ...(view.faq?.items ?? []).flatMap((item) => [ item.question, ...plain(item.answer), ]), view.offer.depositNote, ]; // The photos the story blocks and the package block show const usedElsewhere = (view: ShootPageView): ContentImage[] => [ ...view.stories.flatMap((story) => story.image === undefined ? [] : [story.image], ), view.offer.image, ]; describe('shootPageView with the real content', () => { it('builds the newborn page with its package, filled texts and mosaic', () => { const view = shootPageView('newborn', newbornPage, sources)!; expect(view.path).toBe('/soorten-fotoshoots/newborn'); expect(view.breadcrumb).toEqual({ label: 'Kruimelpad', items: [ { label: 'Home', to: '/' }, { label: 'Soorten fotoshoots', to: '/soorten-fotoshoots' }, { label: 'Newborn', to: '/soorten-fotoshoots/newborn' }, ], }); expect(view.offer.pkg?.price).toBe(185); expect(view.offer.bookLink).toEqual({ to: '/contact', search: { pakket: 'newborn' }, }); expect(view.closing.link).toEqual(view.offer.bookLink); expect(view.offer.askLink).toEqual({ to: '/contact' }); expect(view.offer.depositNote).toBe( `Aanbetaling €${NBSP}50, restbedrag na de shoot.`, ); expect(view.offer.image).toBe(shootTypes[0].detailImage); expect(view.process.steps.map((step) => step.id)).toEqual( PROCESS_STEP_ORDER, ); expect(view.process.steps[0].text).toBe( 'Stuur een berichtje. Ik reageer binnen 1-2 werkdagen.', ); expect(view.process.steps[2].text).toBe( `Met een aanbetaling van €${NBSP}50 staat je datum vast.`, ); expect(view.process.steps[3].text).toBe(newbornPage.process.duration); // The sleeping baby is in a story block already (D14) expect(view.photos?.images).toEqual( newbornPage.photos.images.filter( (image) => image.src !== sleepingBaby, ), ); expect(view.photos?.images).toHaveLength(3); expect(view.photos?.portfolioLink).toEqual({ to: '/portfolio', search: { shoot: 'newborn' }, }); expect(view.faq?.items.length).toBe(newbornPage.faq.items.length); }); it('builds the loveshoot page without package and without mosaic', () => { const view = shootPageView('loveshoot', loveshootPage, sources)!; expect(view.offer.pkg).toBeUndefined(); expect(view).not.toHaveProperty('offer.pkg'); expect(view.offer.bookLink).toEqual({ to: '/contact' }); expect(view.closing.link).toEqual({ to: '/contact' }); expect(view.offer.onRequest.price).toBe('Prijs op aanvraag'); expect(view.photos).toBeUndefined(); expect(view.breadcrumb.items.at(-1)).toEqual({ label: 'Loveshoot', to: '/soorten-fotoshoots/loveshoot', }); }); it('gives undefined for a missing shoot type or content of another shoot', () => { expect( shootPageView('newborn', newbornPage, { ...sources, shootTypes: shootTypes.filter( (shoot) => shoot.id !== 'newborn', ), }), ).toBeUndefined(); expect( shootPageView('loveshoot', newbornPage, sources), ).toBeUndefined(); }); it('leaves out the FAQ without questions and the portfolio link when the portfolio does not show the shoot', () => { const view = shootPageView( 'newborn', { ...newbornPage, faq: { ...newbornPage.faq, items: [] } }, { ...sources, portfolioShoots: [] }, )!; expect(view.faq).toBeUndefined(); expect(view.photos).toBeDefined(); expect(view.photos).not.toHaveProperty('portfolioLink'); }); }); describe('shootPageView', () => { it('is undefined exactly when the shoot type is missing or the content is of another shoot (invariant)', () => { fc.assert( fc.property(viewCase, ({ id, page, sources: given }) => { const view = shootPageView(id, page, given); const exists = page.id === id && given.shootTypes.some((shoot) => shoot.id === id); expect(view !== undefined).toBe(exists); if (view !== undefined) { expect(view.id).toBe(id); expect(view.shoot.id).toBe(id); expect(view.path).toBe(shootPagePath(id)); } }), ); }); it('always has the six steps in their fixed order, whatever the content order (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); expect(view.process.steps.map((step) => step.id)).toEqual( PROCESS_STEP_ORDER, ); view.process.steps.forEach((step) => { expect(step.title).toBe( c.sources.shared.process.steps[step.id].title, ); }); }), ); }); it('leaves no known placeholder in any text (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { for (const text of filledTexts(build(c))) { for (const name of knownPlaceholders) { expect(text).not.toContain(`{${name}}`); } } }), ); }); it('shows each remaining page photo once, in order, never one used elsewhere, or no photos below the minimum (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); const used = new Set(usedElsewhere(view).map((i) => i.src)); const remaining = new Set( c.page.photos.images .map((image) => image.src) .filter((src) => !used.has(src)), ); if (remaining.size < MOSAIC_MIN_PHOTOS) { expect(view.photos).toBeUndefined(); return; } const images = view.photos!.images; const srcs = images.map((image) => image.src); expect(new Set(srcs).size).toBe(srcs.length); expect(new Set(srcs)).toEqual(remaining); expect(srcs.some((src) => used.has(src))).toBe(false); // An ordered subset: each photo is a page photo, found // after the one before it let from = 0; for (const image of images) { const at = c.page.photos.images.indexOf(image, from); expect(at).toBeGreaterThanOrEqual(from); from = at + 1; } }), ); }); it('shows the package exactly when shootPackage finds one (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); expect(view.offer.pkg).toBe( shootPackage(view.shoot, c.sources.packages), ); }), ); }); it('only ever preselects a shown package (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); const shown = visiblePackages(c.sources.packages).map( (pkg) => pkg.id, ); for (const link of [view.offer.bookLink, view.closing.link]) { expect(link.to).toBe('/contact'); if (link.search !== undefined) { expect(shown).toContain(link.search.pakket); expect(link.search.pakket).toBe(view.offer.pkg?.id); } } }), ); }); it('ends the breadcrumb with this page, after Home and the overview (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); expect(view.breadcrumb.items.map((item) => item.to)).toEqual([ '/', '/soorten-fotoshoots', view.path, ]); expect(view.breadcrumb.items.at(-1)?.label).toBe( view.shoot.name, ); }), ); }); it('links the photos to the portfolio exactly when the portfolio shows the shoot (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); if (view.photos === undefined) return; expect(view.photos.portfolioLink !== undefined).toBe( c.sources.portfolioShoots.includes(c.id), ); }), ); }); it('has a FAQ exactly when there are questions, all of them in order (invariant)', () => { fc.assert( fc.property(resolvedCase, (c) => { const view = build(c); expect(view.faq !== undefined).toBe( c.page.faq.items.length > 0, ); expect((view.faq?.items ?? []).map((item) => item.id)).toEqual( c.page.faq.items.map((item) => item.id), ); }), ); }); });