Files
admin-web/src/layouts/AppLayout.test.ts
T

60 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-07-30 17:59:43 +08:00
import { createPinia, setActivePinia } from 'pinia'
import { flushPromises, mount } from '@vue/test-utils'
import { createMemoryHistory, createRouter } from 'vue-router'
import { beforeEach, describe, expect, it, vi } from 'vitest'
2026-07-31 15:34:35 +08:00
import { ElMessageBox } from 'element-plus'
2026-07-30 17:59:43 +08:00
import { useAuthStore } from '@/stores/auth'
import AppLayout from './AppLayout.vue'
vi.mock('element-plus', async (importOriginal) => {
const actual = await importOriginal<typeof import('element-plus')>()
2026-07-31 15:34:35 +08:00
return { ...actual, ElMessage: { warning: vi.fn(), error: vi.fn() }, ElMessageBox: { confirm: vi.fn() } }
2026-07-30 17:59:43 +08:00
})
async function mountLayout(path = '/') {
2026-07-30 17:59:43 +08:00
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
2026-07-31 15:34:35 +08:00
auth.token = 'token'
auth.user = { id: '1', name: '平台管理员', account: 'admin', roles: ['PLATFORM_ADMIN'] }
auth.permissions = ['platform:warehouse:list']
2026-07-30 17:59:43 +08:00
const router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<p>page</p>' } },
{ path: '/customers', component: { template: '<p>customers</p>' }, meta: { title: '客户管理', showPageHeading: false } },
{ path: '/login', component: { template: '<p>login</p>' } },
],
2026-07-30 17:59:43 +08:00
})
await router.push(path)
2026-07-30 17:59:43 +08:00
await router.isReady()
const wrapper = mount(AppLayout, {
2026-07-31 15:34:35 +08:00
global: { plugins: [pinia, router], stubs: { AdminSidebarMenu: { template: '<nav />' }, 'el-button': { emits: ['click'], template: '<button @click="$emit(\'click\')"><slot /></button>' } } },
2026-07-30 17:59:43 +08:00
})
2026-07-31 15:34:35 +08:00
return { wrapper, router }
2026-07-30 17:59:43 +08:00
}
2026-07-31 15:34:35 +08:00
describe('AppLayout', () => {
beforeEach(() => vi.mocked(ElMessageBox.confirm).mockReset())
it('shows the authenticated identity without a role preview switcher', async () => {
const { wrapper } = await mountLayout()
expect(wrapper.text()).toContain('登录身份:平台管理员(admin)')
expect(wrapper.text()).not.toContain('权限预览')
2026-07-30 17:59:43 +08:00
})
it('hides the shell heading when the route opts out', async () => {
const { wrapper } = await mountLayout('/customers')
expect(wrapper.find('.page-heading').exists()).toBe(false)
expect(wrapper.text()).toContain('登录身份:平台管理员(admin)')
})
2026-07-31 15:34:35 +08:00
it('logs out after confirmation', async () => {
2026-07-30 17:59:43 +08:00
vi.mocked(ElMessageBox.confirm).mockResolvedValueOnce({} as never)
2026-07-31 15:34:35 +08:00
const { wrapper, router } = await mountLayout()
2026-07-30 17:59:43 +08:00
await wrapper.get('[aria-label="退出登录"]').trigger('click')
await flushPromises()
2026-07-31 13:43:16 +08:00
expect(router.currentRoute.value.path).toBe('/login')
})
2026-07-30 17:59:43 +08:00
})