import { describe, expect, it } from 'vitest' import type { GlobalMenuItems, NavMenuSection } from '../Navigation.types' import * as currentNavItems from './NavigationMenu.constants' import { getListedMenuId, getMenuGroupLabelByPath } from './NavigationMenu.utils' import { isEnabled, isMenu } from '~/features/IaNav/navTree' import * as option1NavItems from '~/features/IaNav/option1.constants' const MENU: GlobalMenuItems = [ [{ label: 'Start', href: '/guides/getting-started' }], [ { label: 'Products', menuItems: [ [ { label: 'Database', href: '/guides/database/overview' }, { label: 'Auth', href: '/guides/auth' }, ], ], }, ], [{ label: 'Hidden', href: '/guides/hidden', enabled: false }], ] const pageUrls = (entry: Partial): string[] => isEnabled(entry) ? [...(entry.url?.startsWith('/') ? [entry.url] : []), ...(entry.items ?? []).flatMap(pageUrls)] : [] describe('getListedMenuId', () => { // reference pages pass their own menu id, so only guide pages resolve from the tree it('opens every guide of the option-1 trees in a sidebar that lists it', () => { const navItems: Record = option1NavItems const menus = Object.values(navItems).filter(isMenu).filter(isEnabled) const unlistedUrls = menus .flatMap((menu) => menu.items.flatMap(pageUrls)) .filter((pathname) => pathname.startsWith('/guides/')) .filter((pathname) => { const menu = navItems[getListedMenuId({ pathname, navItems })] return !isMenu(menu) || !menu.items.flatMap(pageUrls).includes(pathname) }) expect(unlistedUrls).toEqual([]) }) it('keeps a page that two trees list on the tree of its url prefix', () => { expect( getListedMenuId({ pathname: '/guides/storage/schema/custom-roles', navItems: currentNavItems, }) ).toBe('storage') }) }) describe('getMenuGroupLabelByPath', () => { it('finds the group whose link is the page', () => { expect(getMenuGroupLabelByPath({ menu: MENU, pathname: '/guides/getting-started' })).toBe( 'Start' ) }) it('finds the group of a page under one of its item links', () => { expect(getMenuGroupLabelByPath({ menu: MENU, pathname: '/guides/auth/users' })).toBe('Products') }) it('finds the group of a page that holds one of its item links', () => { expect(getMenuGroupLabelByPath({ menu: MENU, pathname: '/guides/database' })).toBe('Products') }) it('does not match a link that only shares a prefix with the page', () => { expect(getMenuGroupLabelByPath({ menu: MENU, pathname: '/guides/authx' })).toBeUndefined() }) it('skips a disabled group', () => { expect(getMenuGroupLabelByPath({ menu: MENU, pathname: '/guides/hidden' })).toBeUndefined() }) })