58 lines
3.4 KiB
TypeScript
58 lines
3.4 KiB
TypeScript
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react'
|
||
import BusinessTrendsDashboard from './BusinessTrendsDashboard'
|
||
|
||
jest.mock('next-auth/react', () => ({
|
||
useSession: () => ({ data: { user: { name: '本地超级管理员' } } }),
|
||
signOut: jest.fn(),
|
||
}))
|
||
|
||
const week = (key: string, startedCount = 0, completedCount = 0) => ({
|
||
key, label: '8月31日-9月6日', start: '2026-08-30T16:00:00.000Z', end: '2026-09-06T16:00:00.000Z', startedCount, completedCount,
|
||
items: startedCount || completedCount ? [{
|
||
id: `${key}-1`, businessId: 'A1', title: '亿家达美术流程', status: 'COMPLETED', processCode: 'ART', projectType: '美术设计流程', brand: '亿家达', requesterName: '甲', requesterDepartment: '品牌中心', initiatorName: '甲', initiatorDepartment: '品牌中心', sourceCreatedAt: '2026-08-31T01:00:00.000Z', sourceUpdatedAt: '2026-08-31T02:00:00.000Z', sourceFinishedAt: '2026-09-01T01:00:00.000Z', activeTasks: [], startedInWeek: true, completedInWeek: true,
|
||
}] : [],
|
||
})
|
||
|
||
function payload() {
|
||
const brands = ['亿家达', '卓禾', '欧意朗', '顾家', '索菲亚', '朴美'].map((brand) => ({
|
||
brand,
|
||
processes: [
|
||
{ key: 'art', label: '美术设计流程', weeks: Array.from({ length: 8 }, (_, index) => week(`art-${index}`, index === 7 ? 1 : 0, index === 7 ? 1 : 0)) },
|
||
{ key: 'content', label: '内容视频流程', weeks: Array.from({ length: 8 }, (_, index) => week(`content-${index}`)) },
|
||
],
|
||
}))
|
||
return { brands, summary: { weekStart: '2026-07-12T16:00:00.000Z', weekEnd: '2026-09-06T16:00:00.000Z', lastSyncedAt: '2026-09-01T01:00:00.000Z', syncStatus: 'ready' } }
|
||
}
|
||
|
||
describe('BusinessTrendsDashboard', () => {
|
||
beforeEach(() => {
|
||
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: async () => ({ success: true, data: payload() }) }) as jest.Mock
|
||
})
|
||
|
||
it('shows the six-brand navigation and two process trend sections', async () => {
|
||
render(<BusinessTrendsDashboard />)
|
||
expect(await screen.findByRole('heading', { level: 1, name: '业务趋势' })).toBeInTheDocument()
|
||
expect(screen.getByRole('navigation', { name: '选择品牌' })).toBeInTheDocument()
|
||
for (const brand of ['亿家达', '卓禾', '欧意朗', '顾家', '索菲亚', '朴美']) expect(screen.getByRole('button', { name: brand })).toBeInTheDocument()
|
||
expect(await screen.findByRole('heading', { name: '美术设计流程' })).toBeInTheDocument()
|
||
expect(screen.getByRole('heading', { name: '内容视频流程' })).toBeInTheDocument()
|
||
expect(screen.getAllByText(/每小时增量同步/).length).toBeGreaterThan(0)
|
||
})
|
||
|
||
it('switches brand and displays a selected week task detail', async () => {
|
||
render(<BusinessTrendsDashboard />)
|
||
await screen.findByRole('heading', { name: '美术设计流程' })
|
||
fireEvent.click(screen.getByRole('button', { name: '朴美' }))
|
||
expect(screen.getByRole('heading', { name: '朴美' })).toBeInTheDocument()
|
||
const weekButton = screen.getAllByRole('button', { name: /8月31日-9月6日,发起1个,完结1个/ })[0]
|
||
fireEvent.click(weekButton)
|
||
expect(screen.getAllByRole('heading', { name: /任务明细/ }).length).toBeGreaterThan(0)
|
||
})
|
||
|
||
it('shows an API error state', async () => {
|
||
;(global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, json: async () => ({ success: false, error: '读取失败' }) })
|
||
render(<BusinessTrendsDashboard />)
|
||
expect(await screen.findByRole('alert')).toHaveTextContent('读取失败')
|
||
})
|
||
})
|