Files
summercms/admin/tests/state/useNavigation.test.ts
Jakub Zych 1c2a66df45 test(10-05): bring every SPA module, composable and component under Vitest
- 41 unit and component suites under admin/tests/{app,state,shell,list,form,relation,views,ui}
  covering states and a11y roles; every src module is imported by a test
- typed fixture helper assigns each JSON fixture to its generated OpenAPI type
- fix: iconFor ignores inherited object members such as "constructor"
- fix: field controls import ./control instead of the registry (import cycle
  left a renderer unregistered depending on module load order)
- fix: dropdown shows the placeholder for an unknown stored value next to an emptyOption
- fix: list announces a failed schema load even when the rows arrive after it
- tailwind no longer scans admin/tests; boardwalk/dist rebuilt
2026-09-27 17:53:57 +02:00

81 lines
3.2 KiB
TypeScript

import { beforeEach, describe, expect, it } from 'vitest'
import type { NavigationEntry } from '../../src/api/types'
import {
activeEntry,
firstControllerPath,
homePath,
loadNavigation,
navigation,
railEntries,
setNavigation,
useNavigation,
} from '../../src/state/useNavigation'
import { clone, navigationFixture } from '../fixtures/typed'
import { API, mockApi, requestsTo, resetState } from '../helpers'
function entry(code: string, order: number, controllers: string[], controller = controllers[0] ?? 'x.y.z'): NavigationEntry {
return {
code,
label: code,
icon: 'tags',
order,
controller,
sideMenu: controllers.map((id, index) => ({ code: `${code}-${index}`, label: id, icon: 'tags', order: 0, controller: id, sideMenu: [] })),
}
}
beforeEach(() => {
resetState()
})
describe('loadNavigation', () => {
it('stores the server-filtered entries', async () => {
const calls = mockApi({ [`GET ${API}/navigation`]: { body: navigationFixture } })
expect(await loadNavigation()).toHaveLength(3)
expect(navigation.value.map((item) => item.code)).toEqual(['demo', 'tools', 'hidden'])
expect(requestsTo(calls, 'GET', `${API}/navigation`)).toHaveLength(1)
})
it('empties the navigation when the request fails', async () => {
setNavigation(navigationFixture.data)
mockApi({ [`GET ${API}/navigation`]: { status: 403, body: { error: { code: 'forbidden', message: 'x', details: {} } } } })
expect(await loadNavigation()).toEqual([])
expect(navigation.value).toEqual([])
})
})
describe('rail entries', () => {
it('omits plugins without a permitted side-menu item and orders by order, then position', () => {
setNavigation([entry('b', 20, ['a.b.c']), entry('empty', 1, []), entry('a', 10, ['a.a.c']), entry('c', 20, ['a.c.c'])])
expect(railEntries.value.map((item) => item.code)).toEqual(['a', 'b', 'c'])
})
it('opens the first valid side-menu controller, else the entry controller', () => {
expect(firstControllerPath(entry('p', 0, ['bad id', 'acme.demo.gadgets']))).toBe('/acme/demo/gadgets')
expect(firstControllerPath({ ...entry('p', 0, ['bad id']), controller: 'acme.demo.widgets' })).toBe('/acme/demo/widgets')
expect(firstControllerPath({ ...entry('p', 0, []), controller: 'nope' })).toBeNull()
})
it('finds the active plugin by its own controller or any side-menu controller', () => {
setNavigation(navigationFixture.data)
expect(activeEntry('acme', 'demo')?.code).toBe('demo')
expect(activeEntry('acme', 'tools')?.code).toBe('tools')
expect(activeEntry('acme', 'hidden')).toBeNull()
expect(activeEntry('acme', 'missing')).toBeNull()
const shared = clone(navigationFixture.data)
shared[0]!.controller = 'other.root.index'
setNavigation(shared)
expect(activeEntry('acme', 'demo')?.code).toBe('demo')
})
it('goes home to the first rail plugin, skipping plugins without a valid path', () => {
expect(homePath()).toBeNull()
setNavigation([{ ...entry('broken', 1, ['bad id']), controller: 'bad' }, entry('ok', 2, ['acme.tools.hammers'])])
expect(homePath()).toBe('/acme/tools/hammers')
setNavigation(navigationFixture.data)
expect(homePath()).toBe('/acme/demo/widgets')
expect(useNavigation().homePath).toBe(homePath)
})
})