import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import { knownPlaceholders, placeholderName, plainText, richText, textWithPlaceholders, } from '../test/arbitraries'; import { fillPlaceholders, fillRichText } from './fillPlaceholders'; const values = { reactietermijn: '1-2 werkdagen', aanbetaling: '€ 50', locatie: 'Een newbornshoot is bij jou thuis.', duur: '1-2 uur fotografie', opleverduur: '2–3 weken', }; // A value for every known placeholder; values never contain braces, as in // the content const allValues = fc .tuple(...knownPlaceholders.map(() => plainText)) .map((texts) => Object.fromEntries( knownPlaceholders.map((name, index) => [name, texts[index]]), ), ); // Values for some names, known or not const someValues = fc.dictionary(placeholderName, plainText, { maxKeys: 6 }); // Text in which no {name} appears, stray braces included const withoutPlaceholder = fc .string() .filter((text) => !/\{[^{}]*\}/.test(text)); const hasKnownPlaceholder = (text: string) => knownPlaceholders.some((name) => text.includes(`{${name}}`)); describe('fillPlaceholders', () => { it('fills in each placeholder', () => { expect( fillPlaceholders('Ik reageer binnen {reactietermijn}.', values), ).toBe('Ik reageer binnen 1-2 werkdagen.'); expect(fillPlaceholders('Aanbetaling {aanbetaling}.', values)).toBe( 'Aanbetaling € 50.', ); expect(fillPlaceholders('{locatie}', values)).toBe(values.locatie); expect(fillPlaceholders('{duur}', values)).toBe(values.duur); expect(fillPlaceholders('Binnen {opleverduur}.', values)).toBe( 'Binnen 2–3 weken.', ); }); it('fills in a repeated placeholder every time', () => { expect(fillPlaceholders('{duur} en nog eens {duur}', values)).toBe( '1-2 uur fotografie en nog eens 1-2 uur fotografie', ); }); it('keeps unknown placeholders, stray braces and inherited names', () => { expect(fillPlaceholders('{onbekend}', values)).toBe('{onbekend}'); expect(fillPlaceholders('{Duur} { duur}', values)).toBe( '{Duur} { duur}', ); expect(fillPlaceholders('{ en }', values)).toBe('{ en }'); expect(fillPlaceholders('}{', values)).toBe('}{'); expect(fillPlaceholders('{}', values)).toBe('{}'); expect(fillPlaceholders('{constructor} {toString}', values)).toBe( '{constructor} {toString}', ); expect(fillPlaceholders('{{duur}}', values)).toBe( '{1-2 uur fotografie}', ); }); it('inserts a value literally', () => { expect(fillPlaceholders('{duur}', { duur: '$& $1 $$' })).toBe( '$& $1 $$', ); }); it('returns text without placeholders as it is (invariant)', () => { fc.assert( fc.property( withoutPlaceholder, fc.dictionary(fc.string(), fc.string()), (text, given) => { expect(fillPlaceholders(text, given)).toBe(text); }, ), ); }); it('changes nothing when filled in a second time (idempotency)', () => { fc.assert( fc.property(textWithPlaceholders, someValues, (text, given) => { const once = fillPlaceholders(text, given); expect(fillPlaceholders(once, given)).toBe(once); }), ); }); it('leaves no known placeholder behind (invariant)', () => { fc.assert( fc.property(textWithPlaceholders, allValues, (text, given) => { expect(hasKnownPlaceholder(fillPlaceholders(text, given))).toBe( false, ); }), ); }); }); describe('fillRichText', () => { it('fills in text pieces and link texts, and keeps the link target', () => { expect( fillRichText( [ 'Binnen {opleverduur}. ', { text: 'Vraag het binnen {reactietermijn}', to: '/contact', }, { text: '{duur}', to: '/soorten-fotoshoots', hash: 'newborn', }, ], values, ), ).toEqual([ 'Binnen 2–3 weken. ', { text: 'Vraag het binnen 1-2 werkdagen', to: '/contact' }, { text: '1-2 uur fotografie', to: '/soorten-fotoshoots', hash: 'newborn', }, ]); }); it('fills every piece in its place and keeps links as links with their target (invariant)', () => { fc.assert( fc.property(richText, someValues, (text, given) => { const filled = fillRichText(text, given); expect(filled).toHaveLength(text.length); filled.forEach((piece, index) => { const before = text[index]; if (typeof before === 'string') { expect(piece).toBe(fillPlaceholders(before, given)); } else { expect(piece).toEqual({ ...before, text: fillPlaceholders(before.text, given), }); } }); }), ); }); it('changes nothing when filled in a second time (idempotency)', () => { fc.assert( fc.property(richText, someValues, (text, given) => { const once = fillRichText(text, given); expect(fillRichText(once, given)).toEqual(once); }), ); }); it('leaves no known placeholder behind (invariant)', () => { fc.assert( fc.property(richText, allValues, (text, given) => { fillRichText(text, given).forEach((piece) => { expect( hasKnownPlaceholder( typeof piece === 'string' ? piece : piece.text, ), ).toBe(false); }); }), ); }); });