import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import newbornPage from '@/content/shootPages/newborn'; import loveshootPage from '@/content/shootPages/loveshoot'; import type { StoryBlock } from '@/content/types'; import { imageWithOrientation, storyBlock } from '@/test/arbitraries'; import { LANDSCAPE_RATIO, storyLayout } from './storyLayout'; const story = (width?: number, height?: number): StoryBlock => ({ id: 's', eyebrow: 'e', title: 't', paragraphs: [['p']], ...(width === undefined || height === undefined ? {} : { image: { src: '/a.jpg', alt: 'a', width, height } }), }); describe('storyLayout', () => { it('lays out the four real newborn stories', () => { const layout = storyLayout(newbornPage.stories); expect(layout.map((item) => item.media)).toEqual([ { aspectRatio: '4 / 5', mediaSize: 'medium' }, { aspectRatio: '3 / 2', mediaSize: 'half' }, { aspectRatio: '4 / 5', mediaSize: 'medium' }, { aspectRatio: '4 / 5', mediaSize: 'medium' }, ]); expect(layout.map((item) => item.tone)).toEqual([ 'paper', 'alt', 'paper', 'alt', ]); expect(layout.map((item) => item.reverse)).toEqual([ false, true, false, true, ]); expect(layout.map((item) => item.priority)).toEqual([ true, false, false, false, ]); expect(layout[0].spacing).toBe('flushTop'); expect(layout[1].spacing).toBe('default'); }); it('gives text-only stories no media and no priority', () => { const layout = storyLayout(loveshootPage.stories); expect(layout[0].media).toBeDefined(); for (const item of layout.slice(1)) { expect(item).not.toHaveProperty('media'); expect(item.priority).toBe(false); } expect(storyLayout([story()])[0].priority).toBe(false); }); it('switches to landscape at exactly 1.2', () => { expect(LANDSCAPE_RATIO).toBe(1.2); expect(storyLayout([story(120, 100)])[0].media?.aspectRatio).toBe( '3 / 2', ); expect(storyLayout([story(119, 100)])[0].media?.aspectRatio).toBe( '4 / 5', ); }); it('gives nothing for no stories', () => { expect(storyLayout([])).toEqual([]); }); it('alternates, and decides ratio and width by the photo (invariant)', () => { fc.assert( fc.property( fc.array( fc.tuple( storyBlock, fc.option(imageWithOrientation, { nil: undefined }), ), { maxLength: 8 }, ), (pairs) => { const stories = pairs.map(([block, image]) => ({ ...block, image, })); const layout = storyLayout(stories); expect(layout).toHaveLength(stories.length); layout.forEach((item, index) => { const { image } = stories[index]; expect(item.tone).toBe( index % 2 === 0 ? 'paper' : 'alt', ); expect(item.reverse).toBe(index % 2 === 1); expect(item.priority).toBe( index === 0 && image !== undefined, ); if (image === undefined) { expect(item.media).toBeUndefined(); return; } const landscape = image.width >= LANDSCAPE_RATIO * image.height; expect(item.media).toEqual( landscape ? { aspectRatio: '3 / 2', mediaSize: 'half' } : { aspectRatio: '4 / 5', mediaSize: 'medium', }, ); }); }, ), ); }); });