Files
admin-web/src/components/RolePreviewSwitcher.test.ts
T
2026-07-30 17:59:43 +08:00

64 lines
2.5 KiB
TypeScript

import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import type { Role } from '@/types/admin'
import RolePreviewSwitcher from './RolePreviewSwitcher.vue'
const roles: Role[] = [
{ id: 'R001', name: '超级管理员', code: 'SUPER_ADMIN', description: '', userCount: 1, dataScope: '全部数据', status: '启用', permissionIds: [] },
{ id: 'R003', name: '操作员', code: 'OPERATOR', description: '', userCount: 2, dataScope: '本部门数据', status: '启用', permissionIds: [] },
]
const stubs = {
'el-dropdown': {
name: 'ElDropdownStub',
emits: ['command'],
template: '<section data-dropdown><slot /><slot name="dropdown" /></section>',
},
'el-dropdown-menu': { template: '<div data-menu><slot /></div>' },
'el-dropdown-item': {
props: ['command', 'disabled'],
template: '<button data-role :data-command="command" :disabled="disabled"><slot /></button>',
},
}
describe('RolePreviewSwitcher', () => {
it('announces the active role and explains the static preview boundary', () => {
const wrapper = mount(RolePreviewSwitcher, {
props: { roles, currentRole: roles[0] },
global: { stubs },
})
const trigger = wrapper.get('button[aria-label]')
expect(trigger.attributes('aria-label')).toBe('权限预览:超级管理员,数据范围未模拟')
expect(trigger.attributes('title')).toBe('权限预览:超级管理员,数据范围未模拟')
expect(wrapper.text()).toContain('权限预览:超级管理员')
expect(wrapper.text()).toContain('仅预览菜单、页面和操作;数据范围未模拟')
expect(wrapper.find('svg.lucide-user-round-cog').exists()).toBe(true)
})
it('disables the current role and emits a string role id for another selection', async () => {
const wrapper = mount(RolePreviewSwitcher, {
props: { roles, currentRole: roles[0] },
global: { stubs },
})
const items = wrapper.findAll('[data-role]')
expect(items[0].attributes('disabled')).toBeDefined()
expect(items[1].attributes('disabled')).toBeUndefined()
wrapper.getComponent({ name: 'ElDropdownStub' }).vm.$emit('command', 3)
expect(wrapper.emitted('select')).toEqual([['3']])
})
it('labels the trigger when no enabled role is available', () => {
const wrapper = mount(RolePreviewSwitcher, {
props: { roles: [], currentRole: null },
global: { stubs },
})
expect(wrapper.get('button[aria-label]').attributes('aria-label'))
.toBe('权限预览:无可用角色,数据范围未模拟')
})
})