import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import testimonials from '../content/testimonials'; import type { Testimonial } from '../content/types'; import { anyTestimonial, imagePositionAny, uniqueTestimonials, } from '../test/arbitraries'; import { isValidTestimonial, visibleTestimonials } from './visibleTestimonials'; const valid = testimonials[0]; describe('visibleTestimonials', () => { it('keeps the live reviews as they are', () => { expect(visibleTestimonials(testimonials)).toEqual(testimonials); }); it('skips reviews that break an invariant', () => { const broken = [ { ...valid, id: ' ' }, { ...valid, id: 'zonder-shoot', shoot: '' }, { ...valid, id: 'zonder-tekst', quote: ' ' }, { ...valid, id: 'zonder-naam', author: '' }, { ...valid, id: 'zonder-foto', image: { ...valid.image, src: '' } }, { ...valid, id: 'nul-breed', image: { ...valid.image, width: 0 } }, { ...valid, id: 'nul-hoog', image: { ...valid.image, height: 0 } }, ]; expect(visibleTestimonials([...broken, valid])).toEqual([valid]); }); it('keeps the first of two reviews with the same id', () => { expect( visibleTestimonials([valid, { ...valid, author: 'Dubbel' }]), ).toEqual([valid]); }); it('only returns valid reviews with unique ids (invariant)', () => { fc.assert( fc.property(fc.array(anyTestimonial), (input) => { const result = visibleTestimonials(input); expect(result.every(isValidTestimonial)).toBe(true); const ids = result.map((item) => item.id); expect(new Set(ids).size).toBe(ids.length); }), ); }); it('is a subsequence of the input in the same order (invariant)', () => { fc.assert( fc.property(fc.array(anyTestimonial), (input) => { const result = visibleTestimonials(input); let i = 0; for (const item of input) { if (item === result[i]) i++; } return i === result.length; }), ); }); it('is idempotent', () => { fc.assert( fc.property(fc.array(anyTestimonial), (input) => { const once = visibleTestimonials(input); expect(visibleTestimonials(once)).toEqual(once); }), ); }); it('does not let the crop position decide what is shown (invariant)', () => { const ids = (items: Testimonial[]) => visibleTestimonials(items).map((item) => item.id); fc.assert( fc.property( fc.array(anyTestimonial), fc.array(imagePositionAny, { minLength: 1, maxLength: 5 }), (input, positions) => { const withOther = input.map((item, index) => ({ ...item, imagePosition: positions[index % positions.length] as string | undefined, })); const withNone = input.map((item) => { const copy = { ...item }; delete copy.imagePosition; return copy; }); expect(ids(withOther)).toEqual(ids(input)); expect(ids(withNone)).toEqual(ids(input)); }, ), ); }); it('returns valid, unique reviews unchanged (oracle)', () => { fc.assert( fc.property(uniqueTestimonials, (input) => { expect(visibleTestimonials(input)).toEqual(input); }), ); }); });