Files
admin-web/src/views/RolesView.test.ts
T

106 lines
3.6 KiB
TypeScript
Raw Normal View History

2026-07-30 17:59:43 +08:00
import { createPinia, setActivePinia } from 'pinia'
import { flushPromises, shallowMount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
2026-07-31 13:43:16 +08:00
import { createPlatformRole, listPlatformPermissions, listPlatformRoles, type PlatformRoleResource } from '@/api/platformIam'
2026-07-30 17:59:43 +08:00
import { useAuthStore } from '@/stores/auth'
import RolesView from './RolesView.vue'
vi.mock('@/api/platformIam', () => ({
createPlatformRole: vi.fn(),
2026-07-31 13:43:16 +08:00
listPlatformPermissions: vi.fn(),
2026-07-30 17:59:43 +08:00
listPlatformRoles: vi.fn(),
2026-07-31 13:43:16 +08:00
updatePlatformRole: vi.fn(),
changePlatformRoleStatus: vi.fn(),
deletePlatformRole: vi.fn(),
replacePlatformRolePermissions: vi.fn(),
2026-07-30 17:59:43 +08:00
}))
const platformRole: PlatformRoleResource = {
id: 4000002,
tenantId: 0,
appCode: 'PLATFORM',
roleCode: 'CUSTOMER_REVIEWER',
roleName: '企业审核员',
roleType: 'SYSTEM',
status: 'ACTIVE',
description: '企业资料与入驻审核',
}
function mountView(pinia = createPinia()) {
setActivePinia(pinia)
return shallowMount(RolesView, {
global: {
plugins: [pinia],
directives: { loading: () => undefined },
stubs: {
'el-alert': { props: ['title'], template: '<div role="alert">{{ title }}<slot name="title" /></div>' },
'el-input': true,
'el-option': true,
'el-select': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-radio-group': true,
'el-radio-button': true,
'el-drawer': {
props: ['modelValue'],
template: '<section><slot /><slot name="footer" /></section>',
},
'el-form': { template: '<form><slot /></form>' },
'el-form-item': { template: '<label><slot /></label>' },
'el-tree': {
props: { checkStrictly: Boolean },
template: '<div data-testid="permission-tree" :data-check-strictly="String(checkStrictly)" />',
},
},
},
})
}
describe('RolesView', () => {
beforeEach(() => {
setActivePinia(createPinia())
vi.mocked(listPlatformRoles).mockReset()
2026-07-31 13:43:16 +08:00
vi.mocked(listPlatformPermissions).mockReset()
vi.mocked(listPlatformPermissions).mockResolvedValue([])
2026-07-30 17:59:43 +08:00
vi.mocked(createPlatformRole).mockReset()
})
it('keeps directory, menu, and action permissions independently checkable', () => {
const wrapper = mountView()
expect(wrapper.get('[data-testid="permission-tree"]').attributes('data-check-strictly')).toBe('true')
})
it('loads platform roles from IAM for a server session', async () => {
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
auth.isServerSession = true
auth.permissions = ['platform:role:list', 'platform:role:create']
vi.mocked(listPlatformRoles).mockResolvedValue([platformRole])
const wrapper = mountView(pinia)
await flushPromises()
expect(listPlatformRoles).toHaveBeenCalledTimes(1)
2026-07-31 13:43:16 +08:00
expect(wrapper.text()).toContain('角色查询、新增、编辑、启停、删除及权限分配均使用 IAM 真实接口')
2026-07-30 17:59:43 +08:00
expect(wrapper.find('[data-testid="permission-tree"]').exists()).toBe(false)
})
it('keeps a failed IAM role list recoverable', async () => {
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
auth.isServerSession = true
auth.permissions = ['platform:role:list']
vi.mocked(listPlatformRoles).mockRejectedValue(new Error('IAM unavailable'))
const wrapper = mountView(pinia)
await flushPromises()
expect(wrapper.findAll('[role="alert"]').map((item) => item.text()).join(' ')).toContain('IAM unavailable')
})
})