110 lines
3.8 KiB
TypeScript
110 lines
3.8 KiB
TypeScript
import { render, screen, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import PermissionGroupsDashboard from './PermissionGroupsDashboard'
|
|
|
|
jest.mock('next-auth/react', () => ({
|
|
useSession: () => ({
|
|
status: 'authenticated',
|
|
data: { user: { isSuperAdmin: true, name: '本地超级管理员' } },
|
|
}),
|
|
}))
|
|
|
|
jest.mock('./DepartmentShell', () => ({
|
|
__esModule: true,
|
|
default: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
|
}))
|
|
|
|
describe('PermissionGroupsDashboard', () => {
|
|
beforeEach(() => {
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({
|
|
materialTypes: ['3D场景'],
|
|
tree: [
|
|
{
|
|
id: 'root',
|
|
name: '江苏简尚品牌管理有限公司',
|
|
path: '江苏简尚品牌管理有限公司',
|
|
level: 1,
|
|
parentId: null,
|
|
children: [
|
|
{
|
|
id: 'ops',
|
|
name: '运营中心A',
|
|
path: '江苏简尚品牌管理有限公司/运营中心A',
|
|
level: 2,
|
|
parentId: 'root',
|
|
children: [],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
groups: [
|
|
{
|
|
slug: 'operations',
|
|
name: '运营',
|
|
permissions: {
|
|
'3D场景': { canView: true, canDownload: true, canDownloadSource: false, canShare: true },
|
|
},
|
|
departments: [
|
|
{ path: '江苏简尚品牌管理有限公司/运营中心A', name: '运营中心A' },
|
|
],
|
|
members: [],
|
|
},
|
|
{
|
|
slug: 'visual',
|
|
name: '视觉',
|
|
permissions: {
|
|
'3D场景': { canView: true, canDownload: true, canDownloadSource: false, canShare: true },
|
|
},
|
|
departments: [],
|
|
members: [],
|
|
},
|
|
],
|
|
}),
|
|
}) as jest.Mock
|
|
})
|
|
|
|
it('在治理与设置里展示权限组和部门树', async () => {
|
|
render(<PermissionGroupsDashboard />)
|
|
|
|
expect(await screen.findByRole('heading', { level: 1, name: '权限组' })).toBeInTheDocument()
|
|
expect(await screen.findByRole('navigation', { name: '花名册组织树' })).toBeInTheDocument()
|
|
expect(within(screen.getByRole('navigation', { name: '权限组' })).getByRole('button', { name: /运营/ })).toBeInTheDocument()
|
|
expect(screen.getByText('已在本组')).toBeInTheDocument()
|
|
expect(screen.getByRole('columnheader', { name: '可见' })).toBeInTheDocument()
|
|
expect(screen.queryByRole('columnheader', { name: '分享' })).not.toBeInTheDocument()
|
|
expect(screen.getByRole('link', { name: '按人看权限' })).toHaveAttribute(
|
|
'href',
|
|
'/workspaces/governance/user-permissions',
|
|
)
|
|
})
|
|
|
|
it('已在别组的部门会标明占用,不能再加入当前组', async () => {
|
|
render(<PermissionGroupsDashboard />)
|
|
await screen.findByRole('navigation', { name: '花名册组织树' })
|
|
await userEvent.click(
|
|
within(screen.getByRole('navigation', { name: '权限组' })).getByRole('button', { name: /视觉/ }),
|
|
)
|
|
expect(await screen.findByText('已在「运营」')).toBeInTheDocument()
|
|
expect(screen.getByText('占用')).toBeInTheDocument()
|
|
})
|
|
|
|
it('可以把部门从组里拿掉', async () => {
|
|
render(<PermissionGroupsDashboard />)
|
|
const remove = await screen.findByRole('button', { name: '拿掉' })
|
|
await userEvent.click(remove)
|
|
expect(global.fetch).toHaveBeenCalledWith(
|
|
'/api/organization/permission-groups',
|
|
expect.objectContaining({
|
|
method: 'PUT',
|
|
body: JSON.stringify({
|
|
action: 'remove-department',
|
|
slug: 'operations',
|
|
path: '江苏简尚品牌管理有限公司/运营中心A',
|
|
}),
|
|
}),
|
|
)
|
|
})
|
|
})
|