import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import aboutContent from './content/aboutContent'; import packages from './content/packages'; import portfolioContent from './content/portfolioContent'; 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 { emailAddress } from './lib/emailAddress'; import { richTextToPlain } from './lib/richTextToPlain'; import { shootPagePath } from './lib/shootPages'; import { shootPageView, type ShootPageView } from './lib/shootPageView'; import { visibleSocials } from './lib/visibleSocials'; import type { RoutePath } from './navigation'; import { aboutPage, absoluteUrl, areaServed, breadcrumbList, businessId, canonicalUrl, contactPage, defaultOgImage, faqPage, galleryImages, jsonLdScript, pageHead, person, personId, preloadImage, professionalService, shootPageGraph, shootService, shootServiceId, shootServices, siteHead, siteUrl, structuredDataScript, webPage, type WebPageType, } from './seo'; import sitePages from './sitePages'; import { anySiteContent, contentImage, faqItems, knownRoutePath, knowsAboutEntries, plainText, portfolioPhotos, pricingPackage, routePath, shootPageContent, shootPageShared as anyShared, shootType, shootTypeId, uniqueShootTypes, } from './test/arbitraries'; const meta = (head: ReturnType, key: string) => head.meta.find( (m) => ('name' in m && m.name === key) || ('property' in m && m.property === key), ); describe('canonicalUrl', () => { it('maakt een absolute URL zonder slash, query of hash', () => { expect(canonicalUrl('/')).toBe(`${siteUrl}/`); expect(canonicalUrl('')).toBe(`${siteUrl}/`); expect(canonicalUrl('/portfolio/')).toBe(`${siteUrl}/portfolio`); expect(canonicalUrl('/contact?x=1#top')).toBe(`${siteUrl}/contact`); expect(canonicalUrl('portfolio')).toBe(`${siteUrl}/portfolio`); }); it('is idempotent', () => { fc.assert( fc.property( routePath, fc.constantFrom('', '/', '?a=1', '#x'), (path, suffix) => { const once = canonicalUrl(path + suffix); expect(canonicalUrl(once.slice(siteUrl.length))).toBe(once); }, ), ); }); it('begint met siteUrl en eindigt alleen bij de root op een slash (invariant)', () => { fc.assert( fc.property(fc.string(), (path) => { const url = canonicalUrl(path); const rest = url.slice(siteUrl.length); return ( url.startsWith(siteUrl + '/') && !/[?#]/.test(rest) && (rest === '/' || !rest.endsWith('/')) ); }), ); }); }); describe('pageHead', () => { it('zet titel, canonical en preview voor de homepage, zonder robots-tag', () => { const head = pageHead('/'); expect(head.meta[0]).toEqual({ title: 'Lifestyle fotograaf in Huizen | Andrea Slop Fotografie', }); expect(head.links).toEqual([{ rel: 'canonical', href: `${siteUrl}/` }]); expect(meta(head, 'og:url')).toMatchObject({ content: `${siteUrl}/` }); expect(meta(head, 'og:image')).toMatchObject({ content: `${siteUrl}/og-image.jpg`, }); expect(meta(head, 'robots')).toBeUndefined(); }); it('zet noindex op een placeholder', () => { const head = pageHead('/privacyverklaring'); expect(meta(head, 'robots')).toMatchObject({ content: 'noindex' }); expect(head.links[0].href).toBe(`${siteUrl}/privacyverklaring`); }); }); describe('jsonLdScript', () => { it('geeft na parsen dezelfde data terug (round-trip)', () => { fc.assert( fc.property(fc.jsonValue(), (value) => { expect(JSON.parse(jsonLdScript(value))).toEqual( JSON.parse(JSON.stringify(value)), ); }), ); }); it('bevat nooit < of >, dus nooit een sluitende script-tag (invariant)', () => { fc.assert( fc.property(fc.jsonValue(), fc.string(), (value, text) => { const out = jsonLdScript({ value, text: text + '' }); return !/[<>]/.test(out); }), ); }); }); describe('professionalService en siteHead', () => { it('beschrijft de fotograaf zonder e-mailadres', () => { const data = professionalService(siteContent); expect(data['@type']).toBe('ProfessionalService'); expect(data.address.addressLocality).toBe('Huizen'); expect(data.sameAs).toEqual(siteContent.socials.map((s) => s.href)); expect(JSON.stringify(data)).not.toContain('@andreaslop'); }); it('serves Huizen and Gooi en Vechtstreek', () => { expect(professionalService(siteContent).areaServed).toEqual([ { '@type': 'City', name: 'Huizen' }, { '@type': 'AdministrativeArea', name: 'Gooi en Vechtstreek' }, ]); }); it('laat ongeldige socials weg uit sameAs', () => { const data = professionalService({ ...siteContent, socials: [{ platform: 'facebook', href: 'http://onveilig.nl' }], }); expect(data.sameAs).toEqual([]); }); it('zet de gestructureerde data als ld+json-script', () => { const head = siteHead(siteContent); expect(head.scripts[0].type).toBe('application/ld+json'); expect(JSON.parse(head.scripts[0].children)['@type']).toBe( 'ProfessionalService', ); }); }); describe('areaServed', () => { it('keeps every named area, trimmed and in order (invariant)', () => { fc.assert( fc.property(anySiteContent, (site) => { const named = site.serviceArea.filter( (area) => area.name.trim() !== '', ); expect(areaServed(site.serviceArea)).toEqual( named.map((area) => ({ '@type': area.type, name: area.name.trim(), })), ); }), ); }); it('only produces towns and regions (invariant)', () => { fc.assert( fc.property(anySiteContent, (site) => areaServed(site.serviceArea).every((place) => ['City', 'AdministrativeArea'].includes(place['@type']), ), ), ); }); }); describe('pageHead for the about page', () => { it('is indexed, with its own title, description and preview image', () => { const head = pageHead('/over-mij'); expect(head.meta[0]).toEqual({ title: 'Over mij – fotograaf in Huizen | Andrea Slop Fotografie', }); expect(meta(head, 'robots')).toBeUndefined(); expect(meta(head, 'description')).toMatchObject({ content: sitePages['/over-mij'].description, }); expect(meta(head, 'og:image')).toMatchObject({ content: `${siteUrl}/og-over-mij.jpg`, }); expect(head.links[0].href).toBe(`${siteUrl}/over-mij`); }); it('keeps title and description within what search engines show', () => { const page = sitePages['/over-mij']; expect( `${page.title} | Andrea Slop Fotografie`.length, ).toBeLessThanOrEqual(60); expect(page.description.length).toBeLessThanOrEqual(155); }); }); describe('structured data about Andrea', () => { const image = `${siteUrl}/og-over-mij.jpg`; it('describes Andrea from the page content and the site socials', () => { expect(person(aboutContent.person, siteContent, image)).toEqual({ '@type': 'Person', '@id': personId, name: 'Andrea Slop', jobTitle: 'Lifestyle fotograaf', image, address: { '@type': 'PostalAddress', addressLocality: 'Huizen', addressCountry: 'NL', }, url: `${siteUrl}/over-mij`, worksFor: { '@id': businessId }, knowsAbout: [ 'Newbornfotografie', 'Zwangerschapsfotografie', 'Gezinsfotografie', ], sameAs: siteContent.socials.map((social) => social.href), }); }); it('leaves out empty lists instead of writing them', () => { const data = person( { ...aboutContent.person, knowsAbout: [' ', ''] }, { ...siteContent, socials: [] }, image, ); expect(data).not.toHaveProperty('knowsAbout'); expect(data).not.toHaveProperty('sameAs'); }); it('links Andrea and the business both ways (invariant)', () => { fc.assert( fc.property(anySiteContent, (site) => { const business = professionalService(site); const andrea = person(aboutContent.person, site, image); expect(business['@id']).toBe(businessId); expect(business.founder['@id']).toBe(andrea['@id']); expect(andrea.worksFor['@id']).toBe(business['@id']); }), ); }); it('lists exactly the valid https socials in sameAs (invariant)', () => { fc.assert( fc.property(anySiteContent, (site) => { const valid = visibleSocials(site.socials).map((s) => s.href); const andrea = person(aboutContent.person, site, image); expect(andrea.sameAs ?? []).toEqual(valid); }), ); }); it('lists the cleaned specialisations in knowsAbout (invariant)', () => { fc.assert( fc.property(knowsAboutEntries, (entries) => { const andrea = person( { ...aboutContent.person, knowsAbout: entries }, siteContent, image, ); const list = andrea.knowsAbout ?? []; expect( list.every( (entry) => entry === entry.trim() && entry !== '', ), ).toBe(true); expect(new Set(list).size).toBe(list.length); }), ); }); it('never contains the e-mail address, on any page (invariant)', () => { fc.assert( fc.property(anySiteContent, (site) => { const address = emailAddress(site.email); const out = jsonLdScript( aboutPage('/over-mij', aboutContent.person, site), ) + jsonLdScript(professionalService(site)); return address === null || !out.includes(address); }), ); }); it('matches the page registry and canonical URL (invariant)', () => { fc.assert( fc.property(anySiteContent, (site) => { const page = aboutPage('/over-mij', aboutContent.person, site); expect(page['@id']).toBe(canonicalUrl('/over-mij')); expect(page.url).toBe(canonicalUrl('/over-mij')); expect(page.name).toBe(sitePages['/over-mij'].title); expect(page.description).toBe( sitePages['/over-mij'].description, ); expect(page.mainEntity.image).toBe(image); }), ); }); it('falls back to the site preview image for a page without its own', () => { const page = aboutPage( '/privacyverklaring', aboutContent.person, siteContent, ); expect(page.mainEntity.image).toBe(`${siteUrl}/og-image.jpg`); }); it('wraps data in an escaped ld+json script tag', () => { const script = structuredDataScript({ name: '' }); expect(script.type).toBe('application/ld+json'); expect(script.children).not.toMatch(/[<>]/); expect(JSON.parse(script.children)).toEqual({ name: '' }); }); }); describe('webPage', () => { const paths = Object.keys(sitePages) as RoutePath[]; const anyPath = fc.constantFrom(...paths); const anyType = fc.constantFrom( 'WebPage', 'AboutPage', 'ContactPage', 'ImageGallery', 'CollectionPage', ); const baseKeys = [ '@context', '@type', '@id', 'url', 'name', 'description', 'inLanguage', ]; // Page-specific fields: any JSON, under keys the base does not use const anyExtra = fc.dictionary( fc.string().filter((key) => !baseKeys.includes(key)), fc.jsonValue(), ); it('describes a page from the registry', () => { expect( webPage('ContactPage', '/contact', { about: { '@id': businessId }, }), ).toEqual({ '@context': 'https://schema.org', '@type': 'ContactPage', '@id': `${siteUrl}/contact`, url: `${siteUrl}/contact`, name: sitePages['/contact'].title, description: sitePages['/contact'].description, inLanguage: 'nl-NL', about: { '@id': businessId }, }); }); it('needs no extra fields', () => { expect(webPage('WebPage', '/')).not.toHaveProperty('mainEntity'); expect(webPage('WebPage', '/')['@id']).toBe(`${siteUrl}/`); }); it('takes name, description and URL from the registry (invariant)', () => { fc.assert( fc.property(anyType, anyPath, (type, path) => { const page = webPage(type, path); expect(page['@context']).toBe('https://schema.org'); expect(page['@type']).toBe(type); expect(page['@id']).toBe(canonicalUrl(path)); expect(page.url).toBe(canonicalUrl(path)); expect(page.name).toBe(sitePages[path].title); expect(page.description).toBe(sitePages[path].description); expect(page.inLanguage).toBe('nl-NL'); }), ); }); it('keeps every extra field as given (invariant)', () => { fc.assert( fc.property(anyType, anyPath, anyExtra, (type, path, extra) => { const page: Record = webPage( type, path, extra, ); for (const [key, value] of Object.entries(extra)) { expect(page[key]).toEqual(value); } }), ); }); it('survives the ld+json script unchanged (round trip)', () => { fc.assert( fc.property(anyType, anyPath, anyExtra, (type, path, extra) => { const page = webPage(type, path, extra); expect(JSON.parse(jsonLdScript(page))).toEqual( JSON.parse(JSON.stringify(page)), ); }), ); }); // aboutPage as it was before it used webPage, kept as the oracle for the // rebuilt version function previousAboutPage( path: RoutePath, content: Parameters[1], site: Parameters[2], ) { const page = sitePages[path]; const image = siteUrl + (page.ogImage ?? defaultOgImage).path; return { '@context': 'https://schema.org', '@type': 'AboutPage', '@id': canonicalUrl(path), url: canonicalUrl(path), name: page.title, description: page.description, inLanguage: 'nl-NL', mainEntity: person(content, site, image), }; } it('leaves the about page data exactly as it was (oracle)', () => { fc.assert( fc.property( anyPath, anySiteContent, knowsAboutEntries, (path, site, knowsAbout) => { const content = { ...aboutContent.person, knowsAbout }; expect(aboutPage(path, content, site)).toEqual( previousAboutPage(path, content, site), ); }, ), ); }); }); describe('absoluteUrl', () => { it('makes a bundled image path absolute and keeps absolute URLs', () => { expect(absoluteUrl('/assets/newborn-abc.jpg')).toBe( `${siteUrl}/assets/newborn-abc.jpg`, ); expect(absoluteUrl('assets/x.jpg')).toBe(`${siteUrl}/assets/x.jpg`); expect(absoluteUrl('https://cdn.example.nl/x.jpg')).toBe( 'https://cdn.example.nl/x.jpg', ); }); it('always gives an https URL, and applying it twice changes nothing (invariant, idempotency)', () => { fc.assert( fc.property(fc.oneof(fc.webPath(), fc.webUrl()), (src) => { const once = absoluteUrl(src); expect(once).toMatch(/^https?:\/\//); expect(absoluteUrl(once)).toBe(once); }), ); }); }); describe('shootServices', () => { const page = `${siteUrl}/soorten-fotoshoots`; it('lists the five shoots as services of the business, without prices', () => { const list = shootServices(shootTypes, siteContent); expect(list.numberOfItems).toBe(5); const services = list.itemListElement.map((element) => element.item); expect(services.map((service) => service['@id'])).toEqual([ `${page}/newborn#service`, `${page}/zwangerschap#service`, `${page}/gezin#service`, `${page}/loveshoot#service`, `${page}/portret#service`, ]); expect(services.map((service) => service.url)).toEqual([ `${page}/newborn`, `${page}/zwangerschap`, `${page}/gezin`, `${page}/loveshoot`, `${page}/portret`, ]); expect(services[0]).toMatchObject({ '@type': 'Service', name: 'Newbornshoot', serviceType: 'Newbornshoot', provider: { '@id': businessId }, areaServed: [ { '@type': 'City', name: 'Huizen' }, { '@type': 'AdministrativeArea', name: 'Gooi en Vechtstreek' }, ], }); // Only the first paragraph: the newborn invitation is not a description expect(services[0].description).toBe(shootTypes[0].paragraphs[0]); expect(services[0].image).toMatch(/^https:\/\/andreaslop\.nl\//); expect(JSON.stringify(list)).not.toMatch(/offers|price/i); }); it('fits in the page data built with webPage', () => { const data = webPage('WebPage', '/soorten-fotoshoots', { mainEntity: shootServices(shootTypes, siteContent), }); expect(data['@id']).toBe(page); expect(data.mainEntity.itemListElement).toHaveLength(5); }); it('has one item per shoot, numbered in order (invariant)', () => { fc.assert( fc.property(uniqueShootTypes, anySiteContent, (shoots, site) => { const list = shootServices(shoots, site); expect(list.numberOfItems).toBe(shoots.length); expect( list.itemListElement.map((element) => element.position), ).toEqual(shoots.map((_, index) => index + 1)); expect( list.itemListElement.map((element) => element.item.name), ).toEqual(shoots.map((shoot) => shoot.shootLabel)); }), ); }); it('gives every service the id and URL of its own shoot page (invariant)', () => { fc.assert( fc.property(uniqueShootTypes, anySiteContent, (shoots, site) => { const services = shootServices( shoots, site, ).itemListElement.map((element) => element.item); const ids = services.map((service) => service['@id']); expect(new Set(ids).size).toBe(ids.length); services.forEach((service, index) => { expect(service['@id']).toBe( `${page}/${shoots[index].id}#service`, ); expect(service.url).toBe(`${page}/${shoots[index].id}`); }); }), ); }); it('is always provided by the business in its service area, described by the first paragraph (invariant)', () => { fc.assert( fc.property(uniqueShootTypes, anySiteContent, (shoots, site) => { shootServices(shoots, site).itemListElement.forEach( ({ item }, index) => { expect(item.provider).toEqual({ '@id': businessId }); expect(item.areaServed).toEqual( areaServed(site.serviceArea), ); expect(item.description).toBe( shoots[index].paragraphs[0], ); }, ); }), ); }); it('never contains the e-mail address or a price (invariant)', () => { fc.assert( fc.property(uniqueShootTypes, anySiteContent, (shoots, site) => { const address = emailAddress(site.email); const out = jsonLdScript(shootServices(shoots, site)); return ( (address === null || !out.includes(address)) && !/"(offers|price)"/.test(out) ); }), ); }); }); // A shoot type with hostile text where the structured data shows it const hostileShoot = fc .tuple(shootType, plainText, plainText) .map(([shoot, label, description]) => ({ ...shoot, shootLabel: label, paragraphs: [description, ...shoot.paragraphs], })); // Breadcrumb items as a page could have them: known routes, hostile labels const breadcrumbItems = fc.array( fc.record({ label: plainText, to: knownRoutePath }), { maxLength: 5 }, ); // The view of a shoot page from the real shoot types and packages, with // any shared texts, site content and page content const anyView = fc .tuple(shootTypeId, anyShared, anySiteContent) .chain(([id, shared, site]) => shootPageContent(id).map( (page) => shootPageView(id, page, { shootTypes, packages, shared, site, portfolioShoots: [], }) as ShootPageView, ), ); const realView = (id: 'newborn' | 'loveshoot') => shootPageView(id, id === 'newborn' ? newbornPage : loveshootPage, { shootTypes, packages, shared: shootPageShared, site: siteContent, portfolioShoots: [], })!; // Survives the script tag unchanged, and never writes a markup character // or the e-mail address const expectSafeScript = (data: unknown, site: typeof siteContent) => { const script = jsonLdScript(data); expect(JSON.parse(script)).toEqual(data); expect(/[<>&]/.test(script)).toBe(false); const address = emailAddress(site.email); if (address !== null) expect(script).not.toContain(address); }; describe('shootService', () => { it('describes the newborn shoot with the offer of its package', () => { const newbornPackage = packages.find((pkg) => pkg.id === 'newborn'); expect( shootService(shootTypes[0], siteContent, newbornPackage), ).toMatchObject({ '@type': 'Service', '@id': `${siteUrl}/soorten-fotoshoots/newborn#service`, url: `${siteUrl}/soorten-fotoshoots/newborn`, name: 'Newbornshoot', offers: { '@type': 'Offer', price: 185, priceCurrency: 'EUR' }, }); expect(shootServiceId('portret')).toBe( `${siteUrl}/soorten-fotoshoots/portret#service`, ); }); it('is the same service on the overview and on the shoot page, with an offer only for a package (invariant)', () => { fc.assert( fc.property( uniqueShootTypes, anySiteContent, fc.option(pricingPackage, { nil: undefined }), (shoots, site, pkg) => { const overview = shootServices( shoots, site, ).itemListElement; shoots.forEach((shoot, index) => { const service = shootService(shoot, site, pkg); expect(overview[index].item['@id']).toBe( service['@id'], ); expect(service['@id']).toBe(shootServiceId(shoot.id)); expect(service.url).toBe( canonicalUrl(shootPagePath(shoot.id)), ); expect('offers' in service).toBe(pkg !== undefined); if (pkg !== undefined) { expect(service.offers).toEqual({ '@type': 'Offer', price: pkg.price, priceCurrency: 'EUR', }); } }); }, ), ); }); it('survives the script tag, without markup characters or e-mail address (round trip, invariant)', () => { fc.assert( fc.property( hostileShoot, anySiteContent, fc.option(pricingPackage, { nil: undefined }), (shoot, site, pkg) => { expectSafeScript(shootService(shoot, site, pkg), site); expectSafeScript(shootServices([shoot], site), site); }, ), ); }); }); describe('breadcrumbList', () => { it('numbers the items from 1, with absolute URLs and the labels in order (invariant)', () => { fc.assert( fc.property(breadcrumbItems, (items) => { const list = breadcrumbList(items); expect(list['@type']).toBe('BreadcrumbList'); expect( list.itemListElement.map((element) => element.position), ).toEqual(items.map((_, index) => index + 1)); expect( list.itemListElement.map((element) => element.name), ).toEqual(items.map((item) => item.label)); list.itemListElement.forEach((element, index) => { expect(element.item.startsWith(`${siteUrl}/`)).toBe(true); expect(element.item).toBe(canonicalUrl(items[index].to)); }); }), ); }); it('survives the script tag, without markup characters (round trip, invariant)', () => { fc.assert( fc.property(breadcrumbItems, (items) => { expectSafeScript(breadcrumbList(items), siteContent); }), ); }); }); describe('faqPage', () => { it('is left out without questions', () => { expect(faqPage('/contact', [])).toBeUndefined(); }); it('has one question per item in order, answered by its visible text (invariant)', () => { fc.assert( fc.property(knownRoutePath, faqItems, (path, items) => { const data = faqPage(path, items); if (data === undefined) { expect(items).toHaveLength(0); return; } expect(data.isPartOf).toEqual({ '@id': canonicalUrl(path) }); expect(data.mainEntity).toEqual( items.map((item) => ({ '@type': 'Question', name: item.question, acceptedAnswer: { '@type': 'Answer', text: richTextToPlain(item.answer), }, })), ); }), ); }); it('survives the script tag, without markup characters (round trip, invariant)', () => { fc.assert( fc.property(knownRoutePath, faqItems, (path, items) => { expectSafeScript(faqPage(path, items) ?? null, siteContent); }), ); }); }); describe('shootPageGraph', () => { it('describes the newborn page: its service with the package price, the breadcrumb and the FAQ', () => { const [page, faq] = shootPageGraph(realView('newborn'), siteContent); expect(page).toMatchObject({ '@type': 'WebPage', '@id': `${siteUrl}/soorten-fotoshoots/newborn`, mainEntity: { '@type': 'Service', '@id': `${siteUrl}/soorten-fotoshoots/newborn#service`, url: `${siteUrl}/soorten-fotoshoots/newborn`, offers: { '@type': 'Offer', price: 185, priceCurrency: 'EUR' }, }, breadcrumb: { '@type': 'BreadcrumbList', itemListElement: [ { position: 1, name: 'Home', item: `${siteUrl}/` }, { position: 2, name: 'Soorten fotoshoots', item: `${siteUrl}/soorten-fotoshoots`, }, { position: 3, name: 'Newborn', item: `${siteUrl}/soorten-fotoshoots/newborn`, }, ], }, }); expect(faq).toMatchObject({ '@type': 'FAQPage', isPartOf: { '@id': `${siteUrl}/soorten-fotoshoots/newborn` }, }); expect(faq).toHaveProperty( 'mainEntity.length', newbornPage.faq.items.length, ); }); it('describes the loveshoot page without an offer', () => { const graph = shootPageGraph(realView('loveshoot'), siteContent); expect(graph.map((data) => data['@type'])).toEqual([ 'WebPage', 'FAQPage', ]); expect(graph[0]).not.toHaveProperty('mainEntity.offers'); expect(JSON.stringify(graph)).not.toMatch(/"(offers|price)"/); }); it('has the page, and the FAQPage exactly when the page has questions (invariant)', () => { fc.assert( fc.property(anyView, anySiteContent, (view, site) => { const [page, ...rest] = shootPageGraph(view, site); expect(rest).toHaveLength(view.faq === undefined ? 0 : 1); expect(page).toHaveProperty(['@id'], canonicalUrl(view.path)); expect(page).toHaveProperty( ['mainEntity', '@id'], shootServiceId(view.id), ); expect(page).toHaveProperty( 'breadcrumb', breadcrumbList(view.breadcrumb.items), ); if (view.offer.pkg === undefined) { expect(page).not.toHaveProperty('mainEntity.offers'); } else { expect(page).toHaveProperty( 'mainEntity.offers.price', view.offer.pkg.price, ); } }), ); }); it('survives the script tag, without markup characters or e-mail address (round trip, invariant)', () => { fc.assert( fc.property(anyView, anySiteContent, (view, site) => { for (const data of shootPageGraph(view, site)) { expectSafeScript(data, site); } }), ); }); }); describe('the contact page', () => { it('is indexed, with its own title, description and preview image', () => { const head = pageHead('/contact'); expect(head.meta[0]).toEqual({ title: 'Contact – fotoshoot boeken in Huizen | Andrea Slop Fotografie', }); expect(meta(head, 'robots')).toBeUndefined(); expect(meta(head, 'og:image')).toMatchObject({ content: `${siteUrl}/og-contact.jpg`, }); expect(head.links[0].href).toBe(`${siteUrl}/contact`); }); it('keeps title and description within what search engines show', () => { const page = sitePages['/contact']; // 61: one over the guideline, accepted to keep "Contact" first expect( `${page.title} | Andrea Slop Fotografie`.length, ).toBeLessThanOrEqual(61); expect(page.description.length).toBeLessThanOrEqual(155); }); it('describes the page as a ContactPage about the business', () => { expect(contactPage('/contact')).toEqual({ '@context': 'https://schema.org', '@type': 'ContactPage', '@id': `${siteUrl}/contact`, url: `${siteUrl}/contact`, name: sitePages['/contact'].title, description: sitePages['/contact'].description, inLanguage: 'nl-NL', about: { '@id': businessId }, }); }); it('never contains an e-mail address and survives the script tag (invariant, round trip)', () => { fc.assert( fc.property( fc.constantFrom(...(Object.keys(sitePages) as RoutePath[])), (path) => { const data = contactPage(path); const script = jsonLdScript(data); expect(script).not.toMatch(/@[a-z0-9-]+\.[a-z]/i); expect(JSON.parse(script)).toEqual(data); expect(data['@id']).toBe(canonicalUrl(path)); expect(data.about).toEqual({ '@id': businessId }); }, ), ); }); }); describe('galleryImages', () => { it('describes each portfolio photo as an image by Andrea, owned by the business', () => { const images = galleryImages(portfolioContent.photos); expect(images).toHaveLength(10); const first = portfolioContent.photos[0]; expect(images[0]).toEqual({ '@type': 'ImageObject', contentUrl: absoluteUrl(first.full.src), thumbnailUrl: absoluteUrl(first.thumb.src), description: first.thumb.alt, width: 1067, height: 1600, creator: { '@id': personId }, copyrightHolder: { '@id': businessId }, }); }); it('gives one image per photo in order, with absolute URLs and the alt text (invariant, oracle)', () => { fc.assert( fc.property(portfolioPhotos, (photos) => { const images = galleryImages(photos); expect(images).toHaveLength(photos.length); images.forEach((image, i) => { expect(image.contentUrl).toMatch(/^https:\/\//); expect(image.thumbnailUrl).toMatch(/^https:\/\//); expect(image.description).toBe(photos[i].thumb.alt); }); }), ); }); it('never puts an e-mail address in the gallery script (invariant)', () => { const script = jsonLdScript( webPage('ImageGallery', '/portfolio', { associatedMedia: galleryImages(portfolioContent.photos), }), ); expect(script).not.toMatch(/@[a-z0-9-]+\.[a-z]/i); }); }); describe('preloadImage', () => { const photo = { src: '/assets/contact-abc.jpg', alt: 'Een foto', width: 800, height: 1200, }; it('preloads the photo at high priority for every width', () => { expect(preloadImage(photo)).toEqual({ rel: 'preload', as: 'image', href: '/assets/contact-abc.jpg', fetchPriority: 'high', }); }); it('binds the preload to a media query when given one', () => { expect(preloadImage(photo, { media: '(min-width: 64em)' })).toEqual({ rel: 'preload', as: 'image', href: '/assets/contact-abc.jpg', fetchPriority: 'high', media: '(min-width: 64em)', }); }); it('leaves out an empty options object and an empty media', () => { expect(preloadImage(photo, {})).toEqual(preloadImage(photo)); expect(preloadImage(photo, { media: '' })).toEqual(preloadImage(photo)); }); it('points at the image with the fixed link fields, and adds media only when given (invariant)', () => { fc.assert( fc.property( contentImage, fc.option(fc.stringMatching(/^\(min-width: \d{1,3}em\)$/), { nil: undefined, }), (image, media) => { const link = preloadImage(image, { media }); expect(link.href).toBe(image.src); expect(link.rel).toBe('preload'); expect(link.as).toBe('image'); expect(link.fetchPriority).toBe('high'); expect(link.media).toBe(media); expect(Object.keys(link).sort()).toEqual( media ? ['as', 'fetchPriority', 'href', 'media', 'rel'] : ['as', 'fetchPriority', 'href', 'rel'], ); }, ), ); }); });