gongxiangp2/lib/__tests__/salary-grade-mutations.test.ts

71 lines
6.1 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.

/** @jest-environment node */
import { prisma } from '../prisma'
import { createSalaryPosition, updateSalaryPosition, deleteSalaryPosition } from '../salary-positions'
import { jobGradeSchema, updateSalarySchema } from '../personnel-salary-contract'
import { updateSalaryRecord } from '../personnel-salary'
import { positionFieldsSchema } from '../salary-position-contract'
jest.mock('../prisma', () => ({ prisma: { $transaction: jest.fn() } }))
jest.mock('../personnel-salary', () => ({ ...jest.requireActual('../personnel-salary'), requireSalaryAccess: jest.fn().mockResolvedValue({ canEdit: true }) }))
const parent = '00000000-0000-4000-8000-000000000001'
const value = { name: '策划P3', salaryRole: 'ai-planning', description: '', dimensions: [], personnelPositionId: parent, jobGrade: 'P3' }
let db: any
beforeEach(() => {
jest.clearAllMocks()
db = { salaryPositionConfig: { findFirst: jest.fn().mockResolvedValue({ id: parent, salaryRole: 'ai-planning' }), count: jest.fn().mockResolvedValue(0), findMany: jest.fn().mockResolvedValue([]), create: jest.fn().mockResolvedValue({ ...value, id: 'scheme', revision: 1, updatedAt: new Date() }), findUnique: jest.fn(), updateMany: jest.fn(), findUniqueOrThrow: jest.fn() }, personnelSalaryBase: { count: jest.fn().mockResolvedValue(0) }, performanceSurveyTemplate: { count: jest.fn().mockResolvedValue(0) }, salaryPositionConfigAudit: { create: jest.fn() } }
;(prisma.$transaction as jest.Mock).mockImplementation(work => work(db))
})
test('职级空白默认、规范化及输入校验,不读取HR职级', () => {
expect(jobGradeSchema.parse(undefined)).toBe('')
expect(jobGradeSchema.parse(' p4 ')).toBe('P4')
for (const grade of ['P3/P4', 'P', 'P123', '3', '高级']) expect(jobGradeSchema.safeParse(grade).success).toBe(false)
expect(positionFieldsSchema.safeParse({ ...value, jobGrade: '' }).success).toBe(false)
expect(positionFieldsSchema.safeParse({ ...value, personnelPositionId: null }).success).toBe(false)
expect(updateSalarySchema.safeParse({ jobGrade: 'P3' }).success).toBe(false)
})
test('新增职级方案保存稳定关联并写审计;同人员岗相同职级拒绝', async () => {
expect(await createSalaryPosition(value, 'admin')).toMatchObject({ personnelPositionId: parent, jobGrade: 'P3' })
expect(db.salaryPositionConfigAudit.create).toHaveBeenCalledTimes(1)
db.salaryPositionConfig.count.mockResolvedValue(1)
await expect(createSalaryPosition(value, 'admin')).rejects.toMatchObject({ code: 'DUPLICATE_GRADE_SCHEME', status: 409 })
expect(db.salaryPositionConfig.create).toHaveBeenCalledTimes(1)
})
test('不能跨薪资入口、关联无效父岗或将有人员的岗位变为职级方案', async () => {
db.salaryPositionConfig.findFirst.mockResolvedValueOnce({ id: parent, salaryRole: 'management' }).mockResolvedValueOnce(null)
await expect(createSalaryPosition(value, 'admin')).rejects.toMatchObject({ status: 400 })
await expect(createSalaryPosition(value, 'admin')).rejects.toMatchObject({ status: 400 })
db.salaryPositionConfig.findUnique.mockResolvedValue({ ...value, id: 'scheme', revision: 1 })
db.personnelSalaryBase.count.mockResolvedValue(1)
await expect(updateSalaryPosition('scheme', { ...value, revision: 1 }, 'admin')).rejects.toMatchObject({ status: 409 })
expect(db.salaryPositionConfig.updateMany).not.toHaveBeenCalled()
})
test('关联职级方案的父岗不可删除;过期版本不能覆盖关联及配置', async () => {
db.salaryPositionConfig.findUnique.mockResolvedValue({ ...value, id: parent, revision: 2 })
await expect(updateSalaryPosition(parent, { ...value, revision: 1 }, 'admin')).rejects.toMatchObject({ code: 'REVISION_CONFLICT' })
db.salaryPositionConfig.count.mockResolvedValue(2)
await expect(deleteSalaryPosition(parent, { revision: 2 }, 'admin')).rejects.toMatchObject({ code: 'POSITION_HAS_GRADE_SCHEMES' })
expect(db.salaryPositionConfig.updateMany).not.toHaveBeenCalled()
})
test('人员职级规范化保存并写审计;旧客户端省略职级不清空,旧版本不写入', async () => {
const fields = { name: '测试', flowerName: '', department: '品牌中心', brand: '', baseSalary: '0', gradeSalary: '0', performanceSalary: '0', positionId: parent }
const before = { ...fields, id: 'personnel', employeeProfileId: 'employee', jobGrade: 'P4', revision: 1, updatedAt: new Date(), deletedAt: null }
db.user = { findUnique: jest.fn().mockResolvedValue({ id: 'admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null }) }
db.organizationUnit = { findMany: jest.fn().mockResolvedValue([{ sourceDeptId: 1, name: '品牌中心', path: '品牌中心' }]) }
db.performanceSurveyEmployeeProfile = { findMany: jest.fn().mockResolvedValue([{ id: 'employee', departmentPath: '品牌中心', departmentMemberships: [] }]) }
db.personnelSalaryBase.findUnique = jest.fn().mockResolvedValue(before)
db.personnelSalaryBase.updateMany = jest.fn().mockResolvedValue({ count: 1 })
// Serializer only needs a Decimal-like two-decimal view for this mutation test.
const money = { toFixed: () => '0.00' }
db.personnelSalaryBase.findUniqueOrThrow = jest.fn().mockResolvedValue({ ...before, baseSalary: money, gradeSalary: money, performanceSalary: money, revision: 2 })
db.personnelSalaryBaseAudit = { create: jest.fn() }
before.baseSalary = money as any; before.gradeSalary = money as any; before.performanceSalary = money as any
await updateSalaryRecord('personnel', { ...fields, jobGrade: 'p3', revision: 1 }, 'admin')
expect(db.personnelSalaryBase.updateMany).toHaveBeenLastCalledWith(expect.objectContaining({ data: expect.objectContaining({ jobGrade: 'P3' }) }))
await updateSalaryRecord('personnel', { ...fields, revision: 1 }, 'admin')
expect(db.personnelSalaryBase.updateMany).toHaveBeenLastCalledWith(expect.objectContaining({ data: expect.objectContaining({ jobGrade: 'P4' }) }))
expect(db.personnelSalaryBaseAudit.create).toHaveBeenCalledTimes(2)
await expect(updateSalaryRecord('personnel', { ...fields, jobGrade: 'P3', revision: 2 }, 'admin')).rejects.toMatchObject({ code: 'REVISION_CONFLICT' })
expect(db.personnelSalaryBase.updateMany).toHaveBeenCalledTimes(2)
})