import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import type { NavItem } from '../navigation'; import { knownRoutePath, uniqueShootTypes } from '../test/arbitraries'; import { pathname } from '../test/navArbitraries'; import { isItemCurrent, isSectionCurrent } from './navCurrent'; import { shootNavItems } from './shootNavItems'; // The comparison rule written out once more: lower case, one trailing slash const normalise = (path: string) => { const lower = path.toLowerCase(); return lower.length > 1 && lower.endsWith('/') ? lower.slice(0, -1) : lower; }; const item = fc.record({ to: knownRoutePath, label: fc.string() }); // The overview with a random set of shoot pages as children const parent = uniqueShootTypes.map((shoots): NavItem => ({ to: '/soorten-fotoshoots', label: 'Soorten fotoshoots', children: shootNavItems(shoots), })); describe('isItemCurrent', () => { it('is true exactly when the normalised paths are equal (invariant)', () => { fc.assert( fc.property(item, pathname, (nav, path) => { expect(isItemCurrent(nav, path)).toBe( normalise(nav.to) === normalise(path), ); }), ); }); it('is true for its own path in every spelling (invariant)', () => { fc.assert( fc.property(item, (nav) => { for (const spelling of [ nav.to, `${nav.to}/`, nav.to.toUpperCase(), `${nav.to.toUpperCase()}/`, ]) { expect(isItemCurrent(nav, spelling)).toBe(true); } }), ); }); it('is not changed by letter case or one added trailing slash (idempotency)', () => { fc.assert( fc.property(item, pathname, (nav, path) => { // '' and '/' have no "added slash" twin that means the same fc.pre(path.length > 0 && path !== '/'); const result = isItemCurrent(nav, path); expect(isItemCurrent(nav, path.toUpperCase())).toBe( isItemCurrent(nav, path.toLowerCase()), ); // Adding a slash to a path without one gives the same answer fc.pre(!path.endsWith('/')); expect(isItemCurrent(nav, `${path}/`)).toBe(result); }), ); }); it('is false for a path that normalises differently (invariant)', () => { fc.assert( fc.property(item, pathname, (nav, path) => { fc.pre(normalise(nav.to) !== normalise(path)); expect(isItemCurrent(nav, path)).toBe(false); }), ); }); it('accepts the item path with a trailing slash and other case', () => { const nav: NavItem = { to: '/portfolio', label: 'Portfolio' }; expect(isItemCurrent(nav, '/portfolio')).toBe(true); expect(isItemCurrent(nav, '/portfolio/')).toBe(true); expect(isItemCurrent(nav, '/Portfolio')).toBe(true); expect(isItemCurrent(nav, '/')).toBe(false); }); it('keeps the home page apart from every other path', () => { const home: NavItem = { to: '/', label: 'Home' }; expect(isItemCurrent(home, '/')).toBe(true); expect(isItemCurrent(home, '')).toBe(false); expect(isItemCurrent(home, '/portfolio')).toBe(false); }); }); describe('isSectionCurrent', () => { it('is implied by isItemCurrent (invariant)', () => { fc.assert( fc.property(parent, pathname, (nav, path) => { fc.pre(isItemCurrent(nav, path)); expect(isSectionCurrent(nav, path)).toBe(true); }), ); }); it('is implied by any child being current (invariant)', () => { fc.assert( fc.property(parent, fc.nat(), (nav, index) => { const children = nav.children ?? []; fc.pre(children.length > 0); const child = children[index % children.length]; expect(isSectionCurrent(nav, child.to)).toBe(true); expect(isSectionCurrent(nav, `${child.to}/`)).toBe(true); expect(isSectionCurrent(nav, child.to.toUpperCase())).toBe( true, ); }), ); }); it('is true only for the item or a child (invariant)', () => { fc.assert( fc.property(parent, pathname, (nav, path) => { const expected = isItemCurrent(nav, path) || (nav.children ?? []).some((child) => isItemCurrent(child, path), ); expect(isSectionCurrent(nav, path)).toBe(expected); }), ); }); it('is false for unrelated paths and prefix look-alikes (invariant)', () => { fc.assert( fc.property( parent, fc.constantFrom( '/', '/portfolio', '/contact', '/soorten-fotoshoots-extra', '/soorten-fotoshootsX', '/soorten-fotoshoots/newborn/extra', '/soorten-fotoshoots/onbekend', ), (nav, path) => { expect(isSectionCurrent(nav, path)).toBe(false); }, ), ); }); it('is false on the home page for the overview', () => { const nav: NavItem = { to: '/soorten-fotoshoots', label: 'Soorten fotoshoots', children: shootNavItems([]), }; expect(isSectionCurrent(nav, '/')).toBe(false); }); it('marks the parent as section and the child as item on a shoot page', () => { const child: NavItem = { to: '/soorten-fotoshoots/newborn', label: 'Newborn', }; const nav: NavItem = { to: '/soorten-fotoshoots', label: 'Soorten fotoshoots', children: [child], }; expect(isSectionCurrent(nav, '/soorten-fotoshoots/newborn/')).toBe( true, ); expect(isItemCurrent(child, '/soorten-fotoshoots/newborn/')).toBe(true); expect(isItemCurrent(nav, '/soorten-fotoshoots/newborn/')).toBe(false); }); it('works for an item without children', () => { const nav: NavItem = { to: '/contact', label: 'Contact' }; expect(isSectionCurrent(nav, '/contact')).toBe(true); expect(isSectionCurrent(nav, '/portfolio')).toBe(false); }); }); describe('hostile pathnames', () => { it('always gives a boolean and does not throw (invariant)', () => { fc.assert( fc.property(parent, fc.string(), (nav, path) => { expect(typeof isItemCurrent(nav, path)).toBe('boolean'); expect(typeof isSectionCurrent(nav, path)).toBe('boolean'); }), ); }); it.each(['', '//', ' ', '%2F', '/%2F', 'a'.repeat(100_000)])( 'handles %j without throwing', (path) => { const nav: NavItem = { to: '/', label: 'Home' }; expect(() => isItemCurrent(nav, path)).not.toThrow(); expect(() => isSectionCurrent(nav, path)).not.toThrow(); }, ); });