166 lines
12 KiB
TypeScript
166 lines
12 KiB
TypeScript
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('月份查询采用原生月份控件当前值', () => {
|
||
render(<SalaryPositionPanel selectedId={position.id} month="2026-10" />)
|
||
const input = screen.getByLabelText('薪资月份') as HTMLInputElement
|
||
// Native month controls may commit the value only when the form is submitted.
|
||
input.value = '2026-11'
|
||
fireEvent.submit(input.form!)
|
||
expect(mockPush).toHaveBeenCalledWith(expect.stringContaining('month=2026-11'), { scroll: false })
|
||
})
|
||
|
||
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()
|
||
})
|
||
|
||
const versionPosition = () => ({...position,performanceVersions:[
|
||
{id:'40000000-0000-4000-8000-000000000001',effectiveMonth:'2026-11',endsBeforeMonth:null,revision:1,description:'11月方案',dimensions:position.dimensions},
|
||
{id:'40000000-0000-4000-8000-000000000002',effectiveMonth:'0000-00',endsBeforeMonth:'2026-11',revision:2,description:'历史方案',dimensions:position.dimensions},
|
||
]})
|
||
test('月份定位版本,版本选择同步URL,历史修正确认区间后才发送保存',async()=>{
|
||
context.payload.positions=[versionPosition()]
|
||
render(<SalaryPositionPanel month="2026-10" />)
|
||
expect(screen.getByLabelText('绩效版本')).toHaveValue('40000000-0000-4000-8000-000000000002')
|
||
fireEvent.change(screen.getByLabelText('绩效版本'),{target:{value:'40000000-0000-4000-8000-000000000001'}})
|
||
expect(mockPush).toHaveBeenCalledWith(expect.stringContaining('month=2026-11'),{scroll:false})
|
||
fireEvent.click(screen.getByRole('button',{name:'编辑此版本'}))
|
||
fireEvent.change(screen.getByLabelText('方案说明'),{target:{value:'历史修正草稿'}})
|
||
fireEvent.click(screen.getByRole('button',{name:'保存绩效版本'}))
|
||
expect(fetch).not.toHaveBeenCalled()
|
||
expect(screen.getByRole('status')).toHaveTextContent('2026年10月及以前')
|
||
expect(screen.getByRole('button',{name:'确认影响并保存'})).toHaveFocus()
|
||
fireEvent.click(screen.getByRole('button',{name:'确认影响并保存'}))
|
||
await waitFor(()=>expect(fetch).toHaveBeenCalled())
|
||
const sent=JSON.parse((fetch as jest.Mock).mock.calls[0][1].body)
|
||
expect(sent).toMatchObject({operation:'update-version',versionRevision:2,description:'历史修正草稿',confirmedImpact:{effectiveMonth:'0000-00',endsBeforeMonth:'2026-11'}})
|
||
})
|
||
test('新增版本复制选中方案,重复实施月份阻止保存,未来版本保存新实施月',async()=>{
|
||
context.payload.positions=[versionPosition()]
|
||
render(<SalaryPositionPanel month="2026-11" />)
|
||
fireEvent.click(screen.getByRole('button',{name:'新增版本'}))
|
||
fireEvent.click(screen.getByRole('button',{name:'保存绩效版本'}))
|
||
expect(screen.getAllByText('该岗位已有此实施月份的版本').length).toBeGreaterThan(0)
|
||
expect(fetch).not.toHaveBeenCalled()
|
||
const input = screen.getByLabelText(/^实施月份/) as HTMLInputElement
|
||
input.value = '2027-01'
|
||
fireEvent.click(screen.getByRole('button',{name:'保存绩效版本'}))
|
||
await waitFor(()=>expect(fetch).toHaveBeenCalled())
|
||
expect(JSON.parse((fetch as jest.Mock).mock.calls[0][1].body)).toMatchObject({operation:'create-version',effectiveMonth:'2027-01',copyFromVersionId:'40000000-0000-4000-8000-000000000001',dimensions:position.dimensions})
|
||
})
|