ray-template/__test__/layout/breadcrumb.spec.tsx
XiaoDaiGua-Ray 911c403434
feat: refine admin workflows for v6.0.3
Improve theme consistency, keyboard navigation, and reusable frontend components.
Harden fullstack initialization and expose scoped lint and Vitest commands.
2026-12-01 11:04:13 +08:00

111 lines
3.2 KiB
TypeScript

import Breadcrumb from '@/layout/components/SiderBar/components/Breadcrumb'
import type { AppMenuOption } from '@/types'
import { mount } from '@vue/test-utils'
import { h, nextTick, ref } from 'vue'
import type { Ref } from 'vue'
const state = vi.hoisted(() => ({
select: vi.fn(),
compact: null as unknown as Ref<boolean>,
icons: null as unknown as Ref<boolean>,
fallback: null as unknown as Ref<boolean>,
options: null as unknown as Ref<AppMenuOption[]>,
}))
vi.mock('@/hooks', () => ({
useDevice: () => ({ isTabletOrSmaller: state.compact }),
}))
vi.mock('@/store', () => ({
useMenuActions: () => ({ changeMenuModelValue: state.select }),
useMenuGetters: () => ({ getBreadcrumbOptions: state.options }),
useSettingGetters: () => ({
getBreadcrumbIcon: state.icons,
getBreadcrumbIconFallback: state.fallback,
}),
}))
const option = (path: string, label: string): AppMenuOption => ({
name: label,
path,
key: path,
fullPath: path,
label,
breadcrumbLabel: label,
meta: {},
})
it('displays menu images and defaults to a fallback for the missing icon', async () => {
state.compact = ref(false)
state.icons = ref(true)
state.fallback = ref(true)
state.options = ref([
{
...option('/reports', 'Reports'),
icon: () => h('img', { src: '/report.png', alt: '' }),
},
option('/reports/monthly', 'Monthly'),
])
const wrapper = mount(Breadcrumb)
try {
const iconSelector = '.layout-breadcrumb__icon'
expect(wrapper.get(`${iconSelector} img`).attributes('src')).toBe(
'/report.png',
)
expect(wrapper.findAll(iconSelector)).toHaveLength(2)
state.fallback.value = false
await nextTick()
expect(wrapper.findAll(iconSelector)).toHaveLength(1)
state.compact.value = true
state.fallback.value = true
await nextTick()
expect(wrapper.get('[aria-current="page"]').text()).toBe('Monthly')
expect(wrapper.findAll(iconSelector)).toHaveLength(1)
state.icons.value = false
await nextTick()
expect(wrapper.findAll(iconSelector)).toHaveLength(0)
} finally {
wrapper.unmount()
}
})
it('opens sibling pages from the keyboard and closes after selection', async () => {
state.compact = ref(false)
state.icons = ref(false)
state.fallback = ref(false)
const monthly = option('/reports/monthly', 'Monthly')
state.options = ref([
{
...option('/reports', 'Reports'),
children: [monthly, option('/reports/yearly', 'Yearly')],
},
monthly,
])
const wrapper = mount(Breadcrumb, { attachTo: document.body })
try {
const trigger = wrapper.get<HTMLButtonElement>(
'button[aria-haspopup="menu"]',
)
trigger.element.focus()
await trigger.trigger('keydown', { key: 'ArrowDown' })
expect(trigger.attributes('aria-expanded')).toBe('true')
await trigger.trigger('keydown', { key: 'ArrowDown' })
await trigger.trigger('keydown', { key: 'Enter' })
expect(state.select).toHaveBeenCalledWith(
'/reports/monthly',
expect.objectContaining({ fullPath: '/reports/monthly' }),
)
expect(trigger.attributes('aria-expanded')).toBe('false')
expect(document.activeElement).toBe(trigger.element)
} finally {
wrapper.unmount()
}
})