import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import { shootTypeId, slugInput } from '../test/arbitraries'; import { SHOOT_TYPE_IDS, SHOOT_TYPES_PATH, shootIdFromPath, shootIdFromSlug, shootPagePath, } from './shootPages'; // Paths as they could reach the app: a slug below the overview, with or // without slashes after it, the overview itself, or anything else const pathInput = fc.oneof( fc .tuple( slugInput.filter((slug) => typeof slug === 'string'), fc.constantFrom('', '/', '//'), ) .map(([slug, end]) => `${SHOOT_TYPES_PATH}/${slug as string}${end}`), fc.constantFrom(SHOOT_TYPES_PATH, `${SHOOT_TYPES_PATH}/`, '/', ''), fc.string(), ); describe('shootPagePath and SHOOT_TYPE_IDS', () => { it('lists the five shoot types, each with a page below the overview', () => { expect(SHOOT_TYPE_IDS).toEqual([ 'newborn', 'zwangerschap', 'gezin', 'loveshoot', 'portret', ]); expect(SHOOT_TYPE_IDS.map(shootPagePath)).toEqual([ '/soorten-fotoshoots/newborn', '/soorten-fotoshoots/zwangerschap', '/soorten-fotoshoots/gezin', '/soorten-fotoshoots/loveshoot', '/soorten-fotoshoots/portret', ]); }); }); describe('shootIdFromSlug', () => { it('knows every shoot type by its exact id', () => { for (const id of SHOOT_TYPE_IDS) { expect(shootIdFromSlug(id)).toBe(id); } }); it('ignores other cases, padding, slashes and non-text values', () => { expect(shootIdFromSlug('Newborn')).toBeUndefined(); expect(shootIdFromSlug(' newborn')).toBeUndefined(); expect(shootIdFromSlug('newborn/')).toBeUndefined(); expect(shootIdFromSlug('onzin')).toBeUndefined(); expect(shootIdFromSlug('constructor')).toBeUndefined(); expect(shootIdFromSlug(['newborn'])).toBeUndefined(); expect(shootIdFromSlug(undefined)).toBeUndefined(); }); it('gives back the id it was given (round trip)', () => { fc.assert( fc.property(shootTypeId, (id) => { expect(shootIdFromSlug(id)).toBe(id); }), ); }); it('gives a known id for exactly that slug, or undefined, and never throws (invariant)', () => { fc.assert( fc.property(slugInput, (slug) => { const result = shootIdFromSlug(slug); if (result !== undefined) { expect(SHOOT_TYPE_IDS).toContain(result); expect(result).toBe(slug); } }), ); }); }); describe('shootIdFromPath', () => { it('finds the shoot type of its page, with one trailing slash tolerated', () => { expect(shootIdFromPath('/soorten-fotoshoots/newborn')).toBe('newborn'); expect(shootIdFromPath('/soorten-fotoshoots/portret')).toBe('portret'); expect(shootIdFromPath('/soorten-fotoshoots/newborn/')).toBe('newborn'); }); it('gives undefined for any other path', () => { expect(shootIdFromPath('/soorten-fotoshoots/onzin')).toBeUndefined(); expect(shootIdFromPath('/soorten-fotoshoots')).toBeUndefined(); expect(shootIdFromPath('/soorten-fotoshoots/')).toBeUndefined(); expect(shootIdFromPath('/soorten-fotoshoots/Newborn')).toBeUndefined(); expect( shootIdFromPath('/soorten-fotoshoots/newborn/extra'), ).toBeUndefined(); expect( shootIdFromPath('/soorten-fotoshoots/newborn//'), ).toBeUndefined(); expect( shootIdFromPath('/soorten-fotoshoots%2Fnewborn'), ).toBeUndefined(); expect( shootIdFromPath('/soorten-fotoshoots/newborn%2Fextra'), ).toBeUndefined(); expect(shootIdFromPath('/newborn')).toBeUndefined(); expect(shootIdFromPath('')).toBeUndefined(); }); it('gives back the id of every shoot page path (round trip)', () => { fc.assert( fc.property(shootTypeId, (id) => { expect(shootIdFromPath(shootPagePath(id))).toBe(id); }), ); }); it('gives a known id only for its exact page path, or undefined, and never throws (invariant)', () => { fc.assert( fc.property(pathInput, (path) => { const result = shootIdFromPath(path); if (result !== undefined) { expect(SHOOT_TYPE_IDS).toContain(result); expect(shootPagePath(result)).toBe( path.endsWith('/') ? path.slice(0, -1) : path, ); } }), ); }); });