import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import SectionPanel from '../../src/components/shell/SectionPanel.vue' import { SIDEBAR_KEY, setCollapsed, sidebarCollapsed } from '../../src/state/useSidebar' import { clone, navigationFixture } from '../fixtures/typed' import { resetState, routerAt, signIn } from '../helpers' beforeEach(() => { resetState() setCollapsed(false) }) afterEach(() => { setCollapsed(false) document.body.innerHTML = '' }) enableAutoUnmount(afterEach) async function mountPanel(path: string, entry = navigationFixture.data[0]!) { await signIn() const router = await routerAt(path) const wrapper = mount(SectionPanel, { props: { entry }, global: { plugins: [router] }, attachTo: document.body }) await flushPromises() return wrapper } describe('section panel', () => { it('is a labelled region with the plugin label and its permitted side menu', async () => { const wrapper = await mountPanel('/acme/demo/gadgets/4') expect(wrapper.find('aside').attributes('aria-label')).toBe('Sekcje') expect(wrapper.find('aside span').text()).toBe('Demo') const links = wrapper.findAll('a') expect(links.map((link) => [link.text(), link.attributes('href')])).toEqual([ ['Widgets', '/acme/demo/widgets'], ['Gadgets', '/acme/demo/gadgets'], ]) expect(links[0]!.attributes('aria-current')).toBeUndefined() expect(links[1]!.attributes('aria-current')).toBe('page') }) it('skips side-menu items whose controller id is invalid', async () => { const entry = clone(navigationFixture.data[0]!) entry.sideMenu[0]!.controller = 'not a controller' const wrapper = await mountPanel('/acme/demo/gadgets', entry) expect(wrapper.findAll('a').map((link) => link.text())).toEqual(['Gadgets']) }) it('collapses on "Zwiń menu" and persists the choice', async () => { const wrapper = await mountPanel('/acme/demo/widgets') const button = wrapper.find('[data-sidebar-collapse]') expect(button.attributes('aria-label')).toBe('Zwiń menu') expect(button.attributes('type')).toBe('button') await button.trigger('click') expect(sidebarCollapsed.value).toBe(true) expect(window.localStorage.getItem(SIDEBAR_KEY)).toBe('true') }) })