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

84 lines
3.6 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'
2026-08-05 15:54:59 +08:00
import { defineComponent, ref } from 'vue'
2026-07-30 17:59:43 +08:00
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-08-03 18:49:18 +08:00
import { useAuthStore } from '@/core/auth/auth'
2026-07-30 17:59:43 +08:00
import AppLayout from './AppLayout.vue'
2026-08-05 15:54:59 +08:00
const CachedPage = defineComponent({
setup() {
const count = ref(0)
return { count }
},
template: '<button class="cached-count" @click="count += 1">{{ count }}</button>',
})
2026-07-30 17:59:43 +08:00
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: [
2026-08-05 15:54:59 +08:00
{ path: '/', name: 'home', component: { template: '<p>page</p>' }, meta: { requiresAuth: true, workspaceTab: false, keepAlive: false } },
{ path: '/customers', name: 'customers', component: CachedPage, meta: { title: '客户管理', showPageHeading: false, requiresAuth: true } },
{ path: '/warehouses', name: 'warehouses', component: { template: '<p>warehouses</p>' }, meta: { title: '仓库管理', requiresAuth: true } },
{ 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-08-05 15:54:59 +08:00
global: { plugins: [pinia, router], stubs: { AdminSidebarMenu: { template: '<nav />' }, WorkspaceTabs: { template: '<div class="workspace-tabs-stub" />' }, '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-08-05 15:54:59 +08:00
it('opens business routes as workspace tabs and keeps route component state while switching', async () => {
const { wrapper, router } = await mountLayout('/customers')
expect(wrapper.find('.workspace-tabs-stub').exists()).toBe(true)
await wrapper.get('.cached-count').trigger('click')
expect(wrapper.get('.cached-count').text()).toBe('1')
await router.push('/warehouses')
await flushPromises()
await router.push('/customers')
await flushPromises()
expect(wrapper.get('.cached-count').text()).toBe('1')
})
2026-07-30 17:59:43 +08:00
})