gongxiangp2/components/department-dashboard/PerformanceSalaryDashboard.test.tsx

146 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { ReactNode } from 'react'
import PerformanceSalaryDashboard from './PerformanceSalaryDashboard'
const mockPush = jest.fn(), mockReplace = jest.fn()
const mockRouter = { push: mockPush, replace: mockReplace }
let mockConfiguration: any
jest.mock('next/navigation', () => ({ useRouter: () => mockRouter }))
jest.mock('./DepartmentShell', () => ({ __esModule: true, default: ({ children }: { children: ReactNode }) => <>{children}</> }))
jest.mock('./SalaryConfigurationContext', () => ({
SalaryConfigurationProvider: ({ children }: { children: ReactNode }) => <>{children}</>,
useSalaryConfiguration: () => mockConfiguration,
}))
jest.mock('./SalaryPositionPanel', () => ({ __esModule: true, default: ({ selectedId, month }: { selectedId?: string; month?: string }) => <p data-testid="performance-selection">{selectedId || 'empty'} · {month}</p> }))
jest.mock('./PersonnelSalaryPanel', () => ({ __esModule: true, default: () => <p>人员内容</p> }))
jest.mock('./PerformanceSalaryDetails', () => ({ __esModule: true, default: () => <p>薪资明细</p> }))
const positions = [{ id: 'first', name: '主管', personnelCount: 0 }, { id: 'second', name: 'AI策划师-创意部', personnelCount: 7 }, { id: 'third', name: 'AI技术设计师', personnelCount: 0 }]
beforeEach(() => {
jest.clearAllMocks()
mockConfiguration = { payload: { positions: positions.map(item => ({ ...item })), canEdit: true }, loading: false, error: '' }
})
test('绩效岗位子导航使用共享岗位顺序,仅显示名称并保留月份', () => {
render(<PerformanceSalaryDashboard view="configuration" config="performance" position="second" month="2026-09" />)
const navigation = screen.getByRole('list', { name: '岗位绩效配置岗位' })
const links = within(navigation).getAllByRole('link')
expect(links.map(link => link.textContent)).toEqual(positions.map(item => item.name))
expect(links[1]).toHaveAttribute('href', '/workspaces/organization/performance-review?view=configuration&config=performance&month=2026-09&position=second')
expect(links[1]).toHaveAttribute('aria-current', 'page')
expect(within(navigation).queryByText('未分配')).not.toBeInTheDocument()
expect(within(navigation).queryByText('全部人员')).not.toBeInTheDocument()
expect(screen.getByTestId('performance-selection')).toHaveTextContent('second · 2026-09')
expect(mockReplace).not.toHaveBeenCalled()
expect(screen.queryByRole('list', { name: '人员薪资配置岗位' })).not.toBeInTheDocument()
})
test.each([undefined, 'invalid', 'unassigned'])('缺省或无效绩效岗位统一默认首岗并修正网址:%s', position => {
render(<PerformanceSalaryDashboard view="configuration" config="performance" position={position} month="2026-09" />)
expect(within(screen.getByRole('list', { name: '岗位绩效配置岗位' })).getByRole('link', { name: '主管' })).toHaveAttribute('aria-current', 'page')
expect(screen.getByTestId('performance-selection')).toHaveTextContent('first · 2026-09')
expect(mockReplace).toHaveBeenCalledWith('/workspaces/organization/performance-review?view=configuration&config=performance&month=2026-09&position=first', { scroll: false })
})
test('岗位路由和共享配置更新后导航与内容同步', () => {
const view = render(<PerformanceSalaryDashboard view="configuration" config="performance" position="second" month="2026-10" />)
view.rerender(<PerformanceSalaryDashboard view="configuration" config="performance" position="third" month="2026-10" />)
expect(screen.getByTestId('performance-selection')).toHaveTextContent('third')
mockConfiguration.payload.positions[2].name = '更名后的技术岗位'
mockConfiguration.payload.positions.push({ id: 'new', name: '新增岗位', personnelCount: 0 })
view.rerender(<PerformanceSalaryDashboard view="configuration" config="performance" position="new" month="2026-10" />)
const navigation = screen.getByRole('list', { name: '岗位绩效配置岗位' })
expect(within(navigation).getByRole('link', { name: '更名后的技术岗位' })).toBeInTheDocument()
expect(within(navigation).getByRole('link', { name: '新增岗位' })).toHaveAttribute('aria-current', 'page')
mockConfiguration.payload.positions = []
view.rerender(<PerformanceSalaryDashboard view="configuration" config="performance" position="new" month="2026-10" />)
expect(within(navigation).queryByRole('link')).not.toBeInTheDocument()
expect(screen.getByTestId('performance-selection')).toHaveTextContent('empty')
expect(mockReplace).toHaveBeenLastCalledWith('/workspaces/organization/performance-review?view=configuration&config=performance&month=2026-10', { scroll: false })
})
test.each(['loading', 'error', 'personnel', 'details'])('加载/错误或其他页面不自动改写岗位:%s', state => {
mockConfiguration.loading = state === 'loading'
mockConfiguration.error = state === 'error' ? '读取失败' : ''
render(<PerformanceSalaryDashboard view={state === 'details' ? 'details' : 'configuration'} config={state === 'personnel' ? 'personnel' : 'performance'} month="2026-10" />)
expect(mockReplace).not.toHaveBeenCalled()
})
test('两个配置分组独立展开收起,键盘操作不导航也不改变右侧内容', async () => {
const user = userEvent.setup()
render(<PerformanceSalaryDashboard view="configuration" config="performance" position="second" month="2026-10" />)
const personnel = screen.getByRole('button', { name: '人员薪资配置' })
const performance = screen.getByRole('button', { name: '岗位绩效配置' })
expect(personnel).toHaveAttribute('aria-expanded', 'false')
expect(performance).toHaveAttribute('aria-expanded', 'true')
expect(document.getElementById(personnel.getAttribute('aria-controls')!)).not.toBeVisible()
personnel.focus()
await user.keyboard('{Enter}')
expect(personnel).toHaveAttribute('aria-expanded', 'true')
expect(performance).toHaveAttribute('aria-expanded', 'true')
expect(within(screen.getByRole('list', { name: '人员薪资配置岗位' })).getByRole('link', { name: '全部人员' })).toHaveAttribute('href', '/workspaces/organization/performance-review?view=configuration&config=personnel&month=2026-10')
await user.keyboard(' ')
expect(personnel).toHaveAttribute('aria-expanded', 'false')
expect(personnel).toHaveFocus()
expect(screen.queryByRole('list', { name: '人员薪资配置岗位' })).not.toBeInTheDocument()
await user.click(performance)
expect(performance).toHaveAttribute('aria-expanded', 'false')
expect(screen.queryByRole('list', { name: '岗位绩效配置岗位' })).not.toBeInTheDocument()
expect(screen.getByTestId('performance-selection')).toHaveTextContent('second · 2026-10')
expect(mockPush).not.toHaveBeenCalled()
expect(mockReplace).not.toHaveBeenCalled()
})
test('人员页默认展开人员组,全部人员、岗位及未分配仍保留选中态和月份', () => {
const view = render(<PerformanceSalaryDashboard view="configuration" config="personnel" month="2026-09" />)
const personnel = screen.getByRole('list', { name: '人员薪资配置岗位' })
expect(within(personnel).getByRole('link', { name: '全部人员' })).toHaveAttribute('aria-current', 'page')
expect(within(personnel).getAllByRole('link').map(item => item.textContent)).toEqual(['全部人员', '主管0', 'AI策划师-创意部7', 'AI技术设计师0', '未分配'])
expect(screen.getByRole('button', { name: '岗位绩效配置' })).toHaveAttribute('aria-expanded', 'false')
view.rerender(<PerformanceSalaryDashboard view="configuration" config="personnel" position="unassigned" month="2026-09" />)
expect(within(personnel).getByRole('link', { name: '未分配' })).toHaveAttribute('aria-current', 'page')
expect(within(personnel).getByRole('link', { name: '未分配' })).toHaveAttribute('href', '/workspaces/organization/performance-review?view=configuration&config=personnel&month=2026-09&position=unassigned')
})
test('直接访问和前进后退切换岗位时展开目标分组,刷新使用当前路由', async () => {
const user = userEvent.setup()
const view = render(<PerformanceSalaryDashboard view="details" month="2026-10" />)
expect(screen.getByRole('button', { name: '人员薪资配置' })).toHaveAttribute('aria-expanded', 'false')
expect(screen.getByRole('button', { name: '岗位绩效配置' })).toHaveAttribute('aria-expanded', 'false')
view.rerender(<PerformanceSalaryDashboard view="configuration" config="performance" position="second" month="2026-10" />)
expect(screen.getByRole('button', { name: '岗位绩效配置' })).toHaveAttribute('aria-expanded', 'true')
await user.click(screen.getByRole('button', { name: '岗位绩效配置' }))
view.rerender(<PerformanceSalaryDashboard view="configuration" config="performance" position="second" month="2026-09" />)
expect(screen.getByRole('button', { name: '岗位绩效配置' })).toHaveAttribute('aria-expanded', 'false')
view.rerender(<PerformanceSalaryDashboard view="configuration" config="performance" position="third" month="2026-09" />)
expect(screen.getByRole('button', { name: '岗位绩效配置' })).toHaveAttribute('aria-expanded', 'true')
expect(within(screen.getByRole('list', { name: '岗位绩效配置岗位' })).getByRole('link', { name: 'AI技术设计师' })).toHaveAttribute('aria-current', 'page')
})
test('只读用户同样能展开配置导航,分组操作不触发维护接口', async () => {
const user = userEvent.setup()
mockConfiguration.payload.canEdit = false
render(<PerformanceSalaryDashboard view="configuration" config="personnel" position="second" month="2026-10" />)
await user.click(screen.getByRole('button', { name: '岗位绩效配置' }))
const performance = screen.getByRole('list', { name: '岗位绩效配置岗位' })
expect(within(performance).getAllByRole('link')).toHaveLength(3)
expect(mockPush).not.toHaveBeenCalled()
expect(mockReplace).not.toHaveBeenCalled()
})
test('人员导航合并策划岗位,绩效导航保留P3/P4方案,直接访问子方案人员链接修正到父岗', async () => {
mockConfiguration.payload.positions.push(...['P3', 'P4'].map(grade => ({ id: grade, name: `策划${grade}`, personnelPositionId: 'second', jobGrade: grade, personnelCount: 0 })))
const user = userEvent.setup()
render(<PerformanceSalaryDashboard view="configuration" config="personnel" position="P3" month="2026-10" />)
expect(mockReplace).toHaveBeenCalledWith('/workspaces/organization/performance-review?view=configuration&config=personnel&month=2026-10&position=second', { scroll: false })
const people = screen.getByRole('list', { name: '人员薪资配置岗位' })
expect(within(people).queryByRole('link', { name: '策划P3' })).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: '岗位绩效配置' }))
const schemes = screen.getByRole('list', { name: '岗位绩效配置岗位' })
expect(within(schemes).queryByRole('link', { name: 'AI策划师-创意部' })).not.toBeInTheDocument()
expect(within(schemes).getByRole('link', { name: '策划P3' })).toBeInTheDocument()
expect(within(schemes).getByRole('link', { name: '策划P4' })).toBeInTheDocument()
})