89 lines
3.3 KiB
TypeScript
89 lines
3.3 KiB
TypeScript
import { createPinia, setActivePinia } from 'pinia'
|
|
import { flushPromises, shallowMount } from '@vue/test-utils'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { listPlatformPermissions, type PlatformPermissionResource } from '@/modules/iam/api/platformIam'
|
|
import { useAuthStore } from '@/core/auth/auth'
|
|
import PermissionsView from './PermissionsView.vue'
|
|
|
|
vi.mock('@/modules/iam/api/platformIam', () => ({
|
|
changePlatformPermissionStatus: vi.fn(),
|
|
createPlatformPermission: vi.fn(),
|
|
deletePlatformPermission: vi.fn(),
|
|
listPlatformPermissions: vi.fn(),
|
|
updatePlatformPermission: vi.fn(),
|
|
}))
|
|
|
|
const permission: PlatformPermissionResource = {
|
|
id: 5100010,
|
|
tenantId: 0,
|
|
appCode: 'PLATFORM',
|
|
permissionType: 'MENU',
|
|
parentId: 5100000,
|
|
permissionCode: 'platform:customer:list',
|
|
permissionName: '企业客户',
|
|
routePath: '/customers',
|
|
httpMethod: null,
|
|
sortNo: 10,
|
|
status: 'ACTIVE',
|
|
}
|
|
|
|
function mountServerView() {
|
|
const pinia = createPinia()
|
|
setActivePinia(pinia)
|
|
const auth = useAuthStore()
|
|
auth.isServerSession = true
|
|
auth.permissions = ['platform:permission:list', 'platform:permission:create']
|
|
return shallowMount(PermissionsView, {
|
|
global: {
|
|
plugins: [pinia],
|
|
directives: { loading: () => undefined },
|
|
stubs: {
|
|
AdminPage: { template: '<main><slot /></main>' },
|
|
ListPageLayout: { template: '<section><slot name="toolbar" /><slot name="state" /><slot /></section>' },
|
|
FormSection: { template: '<section><slot /></section>' },
|
|
AppDrawer: { template: '<section><slot /><slot name="footer" /></section>' },
|
|
LoadErrorState: { props: ['message'], template: '<div role="alert">{{ message }}<slot name="details" /></div>' },
|
|
DataTable: { props: ['data'], template: '<section><slot name="toolbar-actions" /><span data-testid="row-count">{{ data.length }}</span></section>' },
|
|
'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-input-number': true,
|
|
'el-radio-group': true,
|
|
'el-radio-button': true,
|
|
'el-drawer': { template: '<section><slot /><slot name="footer" /></section>' },
|
|
'el-form': { template: '<form><slot /></form>' },
|
|
'el-form-item': { template: '<label><slot /></label>' },
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('PermissionsView IAM mode', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(listPlatformPermissions).mockReset()
|
|
})
|
|
|
|
it('loads the platform permission resource tree from IAM', async () => {
|
|
vi.mocked(listPlatformPermissions).mockResolvedValue([permission])
|
|
|
|
const wrapper = mountServerView()
|
|
await flushPromises()
|
|
|
|
expect(listPlatformPermissions).toHaveBeenCalledTimes(1)
|
|
expect(wrapper.text()).not.toContain('权限资源树')
|
|
})
|
|
|
|
it('shows a recoverable state when the permission list fails', async () => {
|
|
vi.mocked(listPlatformPermissions).mockRejectedValue(new Error('IAM unavailable'))
|
|
|
|
const wrapper = mountServerView()
|
|
await flushPromises()
|
|
|
|
expect(wrapper.findAll('[role="alert"]').map((item) => item.text()).join(' ')).toContain('IAM unavailable')
|
|
})
|
|
})
|