62 lines
4.8 KiB
TypeScript
62 lines
4.8 KiB
TypeScript
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||
import TeamFunctionsDashboard from './TeamFunctionsDashboard'
|
||
import type { TeamFunctionPayload } from '@/lib/team-function-contract'
|
||
jest.mock('next-auth/react', () => ({ useSession: () => ({ data: { user: { name: '验收用户', role: 'SYSTEM_ADMIN' } } }), signOut: jest.fn() }))
|
||
const data: TeamFunctionPayload = { canEdit: true, teams: [
|
||
{ organizationUnitId: 'dept-id', name: '花名册部门', path: '公司/品牌中心/花名册部门', lead: { employeeProfileId: 'person-id', name: '花名册负责人' }, leadStatus: 'active', description: '数据库职责', branches: [{ label: '职责', items: ['数据库条目'] }], revision: 2, candidates: [{ employeeProfileId: 'person-id', name: '花名册负责人', jobNumber: '001' }] },
|
||
{ organizationUnitId: 'new-id', name: '新增部门', path: '公司/品牌中心/新增部门', lead: null, leadStatus: 'unset', description: '', branches: [], revision: 0, candidates: [] },
|
||
] }
|
||
beforeEach(() => {
|
||
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: async () => JSON.parse(JSON.stringify(data)) })
|
||
window.confirm = jest.fn().mockReturnValue(true)
|
||
})
|
||
test('通过接口加载当前部门及职责,没有旧硬编码团队或返回按钮', async () => {
|
||
render(<TeamFunctionsDashboard />)
|
||
expect(await screen.findByRole('region', { name: '花名册部门部门介绍' })).toBeInTheDocument()
|
||
expect(screen.getByText('数据库条目')).toBeInTheDocument()
|
||
expect(screen.queryByText('品牌创意A部')).not.toBeInTheDocument()
|
||
expect(screen.queryByRole('link', { name: '返回部门看板' })).not.toBeInTheDocument()
|
||
fireEvent.click(screen.getByRole('button', { name: /新增部门/ }))
|
||
expect(screen.getByText('尚未配置团队职能。')).toBeInTheDocument()
|
||
})
|
||
test('编辑保存使用部门、人员 ID 和修订号;成功显示更新的内容', async () => {
|
||
;(fetch as jest.Mock).mockImplementation(async (_url, options) => ({ ok: true, json: async () => options?.method === 'PUT' ? { revision: 3 } : JSON.parse(JSON.stringify(data)) }))
|
||
render(<TeamFunctionsDashboard />)
|
||
fireEvent.click(await screen.findByRole('button', { name: '编辑' }))
|
||
fireEvent.change(screen.getByLabelText('团队说明'), { target: { value: '新说明' } })
|
||
fireEvent.click(screen.getByRole('button', { name: /^保存$/ }))
|
||
expect(await screen.findByRole('status')).toHaveTextContent('已保存')
|
||
expect(screen.getByText('新说明')).toBeInTheDocument()
|
||
const call = (fetch as jest.Mock).mock.calls.find(([, options]) => options?.method === 'PUT')
|
||
expect(JSON.parse(call[1].body)).toMatchObject({ organizationUnitId: 'dept-id', leadEmployeeProfileId: 'person-id', revision: 2, description: '新说明' })
|
||
})
|
||
test('冲突不丢弃草稿,取消后重新读取当前版本', async () => {
|
||
;(fetch as jest.Mock).mockImplementation(async (_url, options) => ({ ok: options?.method !== 'PUT', json: async () => options?.method === 'PUT' ? { error: '配置已更新' } : JSON.parse(JSON.stringify(data)) }))
|
||
render(<TeamFunctionsDashboard />)
|
||
fireEvent.click(await screen.findByRole('button', { name: '编辑' }))
|
||
fireEvent.change(screen.getByLabelText('团队说明'), { target: { value: '保留的草稿' } })
|
||
fireEvent.click(screen.getByRole('button', { name: /^保存$/ }))
|
||
expect(await screen.findByRole('alert')).toHaveTextContent('配置已更新')
|
||
expect(screen.getByLabelText('团队说明')).toHaveValue('保留的草稿')
|
||
fireEvent.click(screen.getByRole('button', { name: '取消' }))
|
||
expect(await screen.findByText('数据库职责')).toBeInTheDocument()
|
||
})
|
||
test('拒绝丢弃时部门切换保留草稿', async () => {
|
||
;(window.confirm as jest.Mock).mockReturnValue(false)
|
||
render(<TeamFunctionsDashboard />)
|
||
fireEvent.click(await screen.findByRole('button', { name: '编辑' }))
|
||
fireEvent.change(screen.getByLabelText('团队说明'), { target: { value: '未保存' } })
|
||
fireEvent.click(screen.getByRole('button', { name: /新增部门/ }))
|
||
expect(screen.getByLabelText('团队说明')).toHaveValue('未保存')
|
||
expect(screen.getByRole('region', { name: '花名册部门部门介绍' })).toBeInTheDocument()
|
||
})
|
||
test('无维护权限隐藏编辑,API失败显示错误及重试入口', async () => {
|
||
;(fetch as jest.Mock).mockResolvedValue({ ok: false, json: async () => ({ error: '加载失败' }) })
|
||
render(<TeamFunctionsDashboard />)
|
||
expect(await screen.findByRole('alert')).toHaveTextContent('加载失败')
|
||
;(fetch as jest.Mock).mockResolvedValue({ ok: true, json: async () => ({ ...data, canEdit: false }) })
|
||
fireEvent.click(screen.getByRole('button', { name: '重试' }))
|
||
await waitFor(() => expect(screen.getByText('数据库职责')).toBeInTheDocument())
|
||
expect(screen.queryByRole('button', { name: '编辑' })).not.toBeInTheDocument()
|
||
})
|