gongxiangp2/lib/__tests__/personnel-salary-contract.test.ts

26 lines
1.9 KiB
TypeScript

import { createSalarySchema, salaryAmountSchema, updateSalarySchema } from '../personnel-salary-contract'
test.each(['0', '0.00', '0.01', '123.4', '9999999999.99'])('精确金额接受 %s', value => {
expect(salaryAmountSchema.parse(value)).toBe(value)
})
test.each(['', '-1', '1.001', '1e3', 'NaN', 'Infinity', ' 1', '01', '+1', '.1', '10000000000', 1, null])('拒绝非法金额 %s', value => {
expect(salaryAmountSchema.safeParse(value).success).toBe(false)
})
const fields = { name: '张三', flowerName: '', department: '品牌中心', brand: '', baseSalary: '0', gradeSalary: '0', performanceSalary: '0' }
test('品牌自由文本、花名和品牌可空,姓名和部门必填', () => {
expect(updateSalarySchema.parse({ ...fields, brand: '自由文本创意组', revision: 1 }).brand).toBe('自由文本创意组')
expect(updateSalarySchema.safeParse({ ...fields, name: ' ', revision: 1 }).success).toBe(false)
expect(updateSalarySchema.safeParse({ ...fields, department: '', revision: 1 }).success).toBe(false)
})
test('品牌可省略且不生成空值,兼容旧客户端显式传值', () => {
const { brand, ...withoutBrand } = fields
expect(updateSalarySchema.parse({ ...withoutBrand, revision: 1 })).not.toHaveProperty('brand')
expect(createSalarySchema.parse({ ...withoutBrand, employeeProfileId: '00000000-0000-4000-8000-000000000001' })).not.toHaveProperty('brand')
})
test('身份、版本和额外字段不能伪造', () => {
expect(createSalarySchema.safeParse({ ...fields, employeeProfileId: 'name-not-id' }).success).toBe(false)
expect(updateSalarySchema.safeParse({ ...fields, revision: 0 }).success).toBe(false)
expect(updateSalarySchema.safeParse({ ...fields, revision: 1, employeeProfileId: 'changed' }).success).toBe(false)
expect(createSalarySchema.safeParse({ ...fields, employeeProfileId: '00000000-0000-4000-8000-000000000001', actualGuarantee: '1' }).success).toBe(false)
})