74 lines
4.9 KiB
TypeScript
74 lines
4.9 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react'
|
||
import TeamCapacityDashboard from './TeamCapacityDashboard'
|
||
|
||
jest.mock('next-auth/react', () => ({
|
||
useSession: () => ({ data: { user: { name: '本地超级管理员' } } }),
|
||
signOut: jest.fn(),
|
||
}))
|
||
|
||
const makeWeeks = (department: string, active = false) => Array.from({ length: 8 }, (_, index) => ({
|
||
key: `${department}-${index}`, label: `8月${index + 1}日-8月${index + 7}日`,
|
||
start: '2026-08-01T00:00:00.000Z', end: '2026-08-08T00:00:00.000Z', personnelCount: department === 'art' ? 5 : 3, involvedPersonnelCount: active && index === 7 ? 1 : 0,
|
||
receivedCount: active && index === 7 ? 2 : 0, completedCount: active && index === 7 ? 1 : 0,
|
||
items: active && index === 7 ? [{ id: 'flow-1', businessId: 'A1', title: '美术流程一', status: 'RUNNING', projectType: '美术设计流程', brand: '亿家达', requesterName: '甲', requesterDepartment: '品牌中心', initiatorName: '甲', initiatorDepartment: '品牌中心', assigneeNames: ['甲'], assigneeUserIds: ['u1'], sourceCreatedAt: '2026-09-01T01:00:00.000Z', sourceFinishedAt: null, receivedInWeek: true, completedInWeek: false, isOngoing: true }] : [],
|
||
}))
|
||
|
||
const makeProcess = (department: string, key: 'art' | 'content', active = false) => ({
|
||
key,
|
||
label: key === 'art' ? '美术设计流程' : '内容视频流程',
|
||
weeks: makeWeeks(`${department}-${key}`, active).map(({ personnelCount: _personnelCount, ...week }) => week),
|
||
})
|
||
|
||
const data = {
|
||
summary: {
|
||
weekStart: '2026-07-13T00:00:00.000Z', weekEnd: '2026-09-07T00:00:00.000Z', departmentCount: 5,
|
||
lastSyncedAt: '2026-09-01T01:00:00.000Z', syncStatus: 'ready',
|
||
},
|
||
departments: [
|
||
{ name: '商品企划部', personnelCount: 7, people: [], processes: [makeProcess('excluded', 'art'), makeProcess('excluded', 'content')], weeks: makeWeeks('excluded') },
|
||
{ name: '品牌创意A部', personnelCount: 5, people: [], processes: [makeProcess('art', 'art', true), makeProcess('art', 'content')], weeks: makeWeeks('art', true) },
|
||
{ name: '空间视觉部', personnelCount: 12, people: [], processes: [makeProcess('space', 'art'), makeProcess('space', 'content')], weeks: makeWeeks('space') },
|
||
{ name: '品牌设计部', personnelCount: 1, people: [], processes: [makeProcess('design', 'art'), makeProcess('design', 'content')], weeks: makeWeeks('design') },
|
||
{ name: '品牌创意B部', personnelCount: 8, people: [], processes: [makeProcess('content', 'art'), makeProcess('content', 'content')], weeks: makeWeeks('content') },
|
||
{ name: '综合支持部', personnelCount: 12, people: [], processes: [makeProcess('support', 'art'), makeProcess('support', 'content')], weeks: makeWeeks('support') },
|
||
],
|
||
}
|
||
|
||
describe('TeamCapacityDashboard', () => {
|
||
beforeEach(() => {
|
||
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: async () => ({ success: true, data }) }) as jest.Mock
|
||
})
|
||
|
||
it('renders department navigation, summary, and eight weeks', async () => {
|
||
render(<TeamCapacityDashboard />)
|
||
expect(await screen.findByRole('heading', { level: 1, name: '团队产能' })).toBeInTheDocument()
|
||
await screen.findByRole('button', { name: '品牌创意A部' })
|
||
expect(screen.getByRole('complementary', { name: '团队产能二级部门导航' })).toBeInTheDocument()
|
||
expect(screen.getByRole('button', { name: '品牌创意A部' })).toBeInTheDocument()
|
||
expect(screen.getByRole('button', { name: '品牌创意B部' })).toBeInTheDocument()
|
||
expect(screen.getByRole('button', { name: '综合支持部' })).toBeInTheDocument()
|
||
expect(screen.getByRole('button', { name: '品牌设计部' })).toBeInTheDocument()
|
||
expect(screen.getByRole('button', { name: '空间视觉部' })).toBeInTheDocument()
|
||
expect(screen.queryByRole('button', { name: '商品企划部' })).not.toBeInTheDocument()
|
||
expect(screen.getAllByRole('button', { name: /收到.*完结/ })).toHaveLength(16)
|
||
expect(screen.getByText('当前在职人数')).toBeInTheDocument()
|
||
})
|
||
|
||
it('switches department and selected week detail', async () => {
|
||
render(<TeamCapacityDashboard />)
|
||
await screen.findByRole('button', { name: '品牌创意A部' })
|
||
fireEvent.click(screen.getAllByRole('button', { name: /收到2个,完结1个/ })[0])
|
||
expect(screen.getAllByText('美术流程一').length).toBeGreaterThan(0)
|
||
fireEvent.click(screen.getByRole('button', { name: '品牌创意B部' }))
|
||
expect(await screen.findByRole('heading', { name: '品牌创意B部' })).toBeInTheDocument()
|
||
expect(screen.getAllByText('本周暂无流程明细')).toHaveLength(2)
|
||
})
|
||
|
||
it('renders an error recovery state', async () => {
|
||
;(global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, json: async () => ({ success: false, error: '读取失败' }) })
|
||
render(<TeamCapacityDashboard />)
|
||
expect(await screen.findByRole('alert')).toHaveTextContent('读取失败')
|
||
expect(screen.getByRole('button', { name: '重新读取' })).toBeInTheDocument()
|
||
})
|
||
})
|