ray-template/__test__/hooks/useAppNavigation.spec.ts
XiaoDaiGua-Ray 15b1da763f
fix: address system-wide UI and data review findings
Restore keyboard navigation and fix page refresh, icons, and table interactions.

Guard concurrent edits and align Mock persistence with the API contracts.
2026-11-18 10:25:41 +08:00

79 lines
2.4 KiB
TypeScript

import { createMemoryHistory, createRouter } from 'vue-router'
import { useAppNavigation } from '../../apps/admin/src/hooks/template/useAppNavigation'
import type { AppMenuOption } from '../../apps/admin/src/types'
const { state, menus } = vi.hoisted(() => ({
state: { router: void 0 as ReturnType<typeof createRouter> | undefined },
menus: [{ fullPath: '/parent', children: [{ fullPath: '/target' }] }],
}))
vi.mock('../../apps/admin/src/hooks/template/useVueRouter', () => ({
useVueRouter: () => ({ router: state.router }),
}))
vi.mock('../../apps/admin/src/store', () => ({
useMenuGetters: () => ({ getMenuOptions: { value: menus } }),
}))
describe('useAppNavigation', () => {
beforeEach(() => {
state.router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/target', component: {} },
{ path: '/items/:id', component: {} },
{ path: '/:catchAll(.*)', component: {} },
],
})
})
afterEach(() => vi.restoreAllMocks())
it('returns the navigation promise and supports indices and menu objects', async () => {
const { navigationTo } = useAppNavigation()
const pending = navigationTo(0)
expect(pending).toBeInstanceOf(Promise)
await pending
expect(state.router?.currentRoute.value.path).toBe('/target')
await navigationTo(
{ fullPath: '/items/4?q=one#details' } as AppMenuOption,
{
query: { page: '2' },
},
)
expect(state.router?.currentRoute.value.fullPath).toBe(
'/items/4?q=one&page=2#details',
)
})
it('resolves a dynamic path and preserves URL query and hash when replacing', async () => {
const replace = vi.spyOn(state.router!, 'replace')
await useAppNavigation().navigationTo('/items/8?source=url&keep=1#header', {
isPush: false,
query: { source: 'option' },
})
expect(replace).toHaveBeenCalledOnce()
expect(state.router?.currentRoute.value).toMatchObject({
path: '/items/8',
params: { id: '8' },
query: { source: 'option', keep: '1' },
hash: '#header',
})
})
it.each([-1, NaN, Infinity, 0.5, 1, '/unknown'])(
'ignores invalid target %s',
(target) => {
const warning = vi.spyOn(console, 'warn').mockImplementation(() => {})
const push = vi.spyOn(state.router!, 'push')
expect(useAppNavigation().navigationTo(target)).toBeUndefined()
expect(push).not.toHaveBeenCalled()
expect(warning).toHaveBeenCalledOnce()
},
)
})