Files

66 lines
2.3 KiB
TypeScript

import type { IntlShape } from 'react-intl';
import { describe, expect, it } from 'vitest';
import { createWebStudioMenu } from '#core/WebStudio/config/menu';
// `useMenu` holds no React state — it returns plain path helpers — so the tests call it
// directly. Aliased on import because the `use` prefix otherwise trips the rules-of-hooks lint.
import { useMenu as menuFor } from '#shared/hooks/useMenu';
const intl = { formatMessage: ({ id }: { id: string }) => id } as IntlShape;
const webStudioMenu = [createWebStudioMenu(intl)];
describe('useMenu', () => {
describe('isActive', () => {
it('stays true for a section while a sub-route is open', () => {
const { isActive } = menuFor('/web-studio/pages/-/create');
expect(isActive('/web-studio/pages/-/')).toBe(true);
});
it('is false for a path that only shares a prefix', () => {
const { isActive } = menuFor('/web-studio/pages/listing-archive');
expect(isActive('/web-studio/pages/listing')).toBe(false);
});
});
describe('isSelected', () => {
it('drops the ancestor once a deeper entry matches', () => {
const { isSelected } = menuFor('/web-studio/pages/-/create');
expect(isSelected('/web-studio/pages/-/create', webStudioMenu)).toBe(true);
expect(isSelected('/web-studio/pages/listing', webStudioMenu)).toBe(false);
});
});
describe('getBreadcrumb', () => {
it('resolves the trail for a listing route', () => {
const { getBreadcrumb } = menuFor('/web-studio/pages/listing');
expect(getBreadcrumb(webStudioMenu).map((item) => item.title)).toEqual([
'WEB_STUDIO.MENU.TITLE',
'WEB_STUDIO.PAGES.TITLE'
]);
});
it('reaches the editor entry from a page id route', () => {
const { getBreadcrumb } = menuFor('/web-studio/pages/-/42');
expect(getBreadcrumb(webStudioMenu).map((item) => item.title)).toEqual([
'WEB_STUDIO.MENU.TITLE',
'WEB_STUDIO.PAGES.TITLE',
'WEB_STUDIO.EDITOR.EDIT_TITLE'
]);
});
});
describe('hasActiveChild', () => {
it('is true for the section holding the current route', () => {
const { hasActiveChild } = menuFor('/web-studio/settings/footer');
const settings = createWebStudioMenu(intl).children?.[1];
expect(hasActiveChild(settings?.children)).toBe(true);
});
});
});