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

122 lines
8.7 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 { fireEvent, render, screen, waitFor } from '@testing-library/react'
import SalaryPositionPanel from './SalaryPositionPanel'
import { INITIAL_SALARY_POSITIONS } from '@/lib/salary-position-seeds'
const reload = jest.fn(), context: any = { payload: null, sources: [], loading: false, error: '', reload }
const mockPush = jest.fn(), mockReplace = jest.fn()
const mockRouter = { push: mockPush, replace: mockReplace }
jest.mock('next/navigation', () => ({ useRouter: () => mockRouter }))
jest.mock('./SalaryConfigurationContext', () => ({ ...jest.requireActual('./SalaryConfigurationContext'), useSalaryConfiguration: () => context }))
const position = { ...INITIAL_SALARY_POSITIONS[1].value, id: INITIAL_SALARY_POSITIONS[1].id, revision: 1, updatedAt: '', personnelCount: 7 }
const response = (body: unknown, status = 200) => ({ ok: status < 400, status, json: async () => body })
beforeAll(() => {
HTMLDialogElement.prototype.showModal = function () { this.setAttribute('open', '') }
HTMLDialogElement.prototype.close = function () { this.removeAttribute('open') }
global.structuredClone = value => JSON.parse(JSON.stringify(value))
})
beforeEach(() => {
jest.clearAllMocks(); context.payload = { canEdit: true, positions: [position] }; context.loading = false; context.error = ''
global.fetch = jest.fn().mockResolvedValue(response(position))
})
test('只读可看权重和导出,没有维护按钮', () => {
context.payload.canEdit = false
render(<SalaryPositionPanel />)
expect(screen.getByRole('button', { name: '导出全部岗位' })).toBeEnabled()
expect(screen.queryByRole('button', { name: '编辑岗位' })).not.toBeInTheDocument()
expect(screen.getAllByText('0%')).toHaveLength(2)
expect(screen.getByText(/修改后适用于所有月份/)).toBeInTheDocument()
})
test('关闭岗位编辑后焦点回到触发按钮', () => {
render(<SalaryPositionPanel />)
const trigger = screen.getByRole('button', { name: '编辑岗位' })
trigger.focus(); fireEvent.click(trigger)
screen.getByRole('button', { name: '关闭弹窗' }).focus()
fireEvent.click(screen.getByRole('button', { name: '关闭弹窗' }))
expect(trigger).toHaveFocus()
})
test('权重合计不正确时阻止保存;关闭保留草稿及焦点返回', async () => {
render(<SalaryPositionPanel />)
fireEvent.click(screen.getByRole('button', { name: '编辑岗位' }))
fireEvent.change(screen.getAllByLabelText('维度权重(%)*')[0], { target: { value: '24.99' } })
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
expect(screen.getByRole('alert')).toHaveTextContent('各维度权重合计必须为100%')
await waitFor(() => expect(screen.getByRole('alert')).toHaveFocus())
expect(fetch).not.toHaveBeenCalled()
fireEvent.click(screen.getByRole('button', { name: '关闭弹窗' }))
expect(screen.getByRole('button', { name: '继续编辑' })).toHaveFocus()
fireEvent.click(screen.getByRole('button', { name: '放弃修改' }))
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
})
test('KPI合计错误关联到KPI分组,单项错误关联到字段', async () => {
render(<SalaryPositionPanel />)
fireEvent.click(screen.getByRole('button', { name: '编辑岗位' }))
fireEvent.change(screen.getAllByLabelText('KPI权重(%)*')[0], { target: { value: '99.99' } })
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
expect(screen.getAllByLabelText('维度权重(%)*')[0]).toHaveAttribute('aria-invalid', 'false')
expect(screen.getByRole('region', { name: '维度1' })).toHaveAccessibleDescription('定性内KPI权重合计必须为100%')
fireEvent.change(screen.getAllByLabelText('KPI权重(%)*')[0], { target: { value: '-1' } })
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
expect(screen.getAllByRole('textbox', { name: /^KPI权重/ })[0]).toHaveAttribute('aria-invalid', 'true')
expect(screen.getAllByRole('textbox', { name: /^KPI权重/ })[0]).toHaveAccessibleDescription('请输入 0—100,最多两位小数')
})
test('409保留草稿,核对最新配置后带最新版本重试', async () => {
render(<SalaryPositionPanel />)
fireEvent.click(screen.getByRole('button', { name: '编辑岗位' }))
fireEvent.change(screen.getByLabelText('岗位名称 *'), { target: { value: '我的草稿' } })
;(fetch as jest.Mock).mockResolvedValueOnce(response({ error: '配置冲突', code: 'REVISION_CONFLICT' }, 409)).mockResolvedValueOnce(response({ positions: [{ ...position, revision: 2, name: '他人修改' }] }))
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
await screen.findByRole('button', { name: '已核对,基于最新版本继续编辑' })
expect(screen.getByLabelText('岗位名称 *')).toHaveValue('我的草稿')
expect(screen.getByRole('button', { name: '保存岗位配置' })).toBeDisabled()
fireEvent.click(screen.getByRole('button', { name: '已核对,基于最新版本继续编辑' }))
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
expect(JSON.parse((fetch as jest.Mock).mock.calls[2][1].body)).toMatchObject({ name: '我的草稿', revision: 2 })
})
test('空岗位可保存;读取失败可重试', async () => {
context.payload.positions = []
const { unmount } = render(<SalaryPositionPanel />)
fireEvent.click(screen.getByRole('button', { name: '新增岗位' }))
fireEvent.change(screen.getByLabelText('岗位名称 *'), { target: { value: '新岗位' } })
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
expect(JSON.parse((fetch as jest.Mock).mock.calls[0][1].body).dimensions).toEqual([])
unmount(); context.error = '暂不可用'; render(<SalaryPositionPanel />)
fireEvent.click(screen.getByRole('button', { name: '重新加载' }))
expect(reload).toHaveBeenCalled()
})
test('岗位选择更新网址并保留月份,路由变化后内容及选择同步', () => {
const next = { ...position, id: 'next-position', name: '技术岗位', dimensions: [] }
context.payload.positions.push(next)
const view = render(<SalaryPositionPanel selectedId={position.id} month="2026-09" />)
fireEvent.change(screen.getByRole('combobox', { name: '岗位' }), { target: { value: next.id } })
expect(mockPush).toHaveBeenCalledWith(`/workspaces/organization/performance-review?view=configuration&config=performance&month=2026-09&position=${next.id}`, { scroll: false })
view.rerender(<SalaryPositionPanel selectedId={next.id} month="2026-09" />)
expect(screen.getByRole('combobox', { name: '岗位' })).toHaveValue(next.id)
expect(screen.getByRole('heading', { name: '技术岗位' })).toBeInTheDocument()
view.rerender(<SalaryPositionPanel selectedId={position.id} month="2026-09" />)
expect(screen.getByRole('combobox', { name: '岗位' })).toHaveValue(position.id)
})
test('新增后选中服务端返回的稳定岗位ID', async () => {
render(<SalaryPositionPanel selectedId={position.id} month="2026-09" />)
fireEvent.click(screen.getByRole('button', { name: '新增岗位' }))
fireEvent.change(screen.getByLabelText('岗位名称 *'), { target: { value: '新岗位' } })
;(fetch as jest.Mock).mockResolvedValueOnce(response({ ...position, id: 'created-position' }, 201))
fireEvent.click(screen.getByRole('button', { name: '保存岗位配置' }))
await waitFor(() => expect(mockPush).toHaveBeenCalledWith('/workspaces/organization/performance-review?view=configuration&config=performance&month=2026-09&position=created-position', { scroll: false }))
expect(reload).toHaveBeenCalled()
})
test.each([true, false])('删除当前岗位后进入剩余首岗或空状态:有剩余=%s', async hasRemaining => {
const next = { ...position, id: 'remaining-position', name: '剩余岗位', personnelCount: 0 }
context.payload.positions = [{ ...position, personnelCount: 0 }, ...(hasRemaining ? [next] : [])]
const view = render(<SalaryPositionPanel selectedId={position.id} month="2026-09" />)
fireEvent.click(screen.getByRole('button', { name: '删除岗位' }))
fireEvent.click(screen.getByRole('button', { name: '确认删除' }))
await waitFor(() => expect(mockReplace).toHaveBeenCalledWith(`/workspaces/organization/performance-review?view=configuration&config=performance&month=2026-09${hasRemaining ? '&position=remaining-position' : ''}`, { scroll: false }))
context.payload.positions = hasRemaining ? [next] : []
view.rerender(<SalaryPositionPanel selectedId={hasRemaining ? next.id : undefined} month="2026-09" />)
expect(screen.getByRole('heading', { name: hasRemaining ? '剩余岗位' : '暂无岗位配置' })).toHaveFocus()
})