gongxiangp2/lib/__tests__/performance-salary-pay.test.ts

33 lines
1.6 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/client'
import { calculateSalaryPay } from '../performance-salary-pay'
const wages = (base = '2500', grade = '1000', performance = '1500') => ({
baseSalary: new Prisma.Decimal(base), gradeSalary: new Prisma.Decimal(grade), performanceSalary: new Prisma.Decimal(performance),
})
test.each([
[103.04, 1.03, '5045.00'],
[100.49, 1, '5000.00'],
[100.5, 1.01, '5015.00'],
[99.5, 1, '5000.00'],
[120, 1.2, '5300.00'],
[0, 0, '3500.00'],
])('得分%s先保留两位系数%s再计算薪资', (score, coefficient, total) => {
expect(calculateSalaryPay(score, wages())).toEqual({ performanceCoefficient: coefficient, currentTotal: total })
})
test('小数金额精确计算并四舍五入到分,不受Decimal全局舍入配置影响', () => {
const previous = Prisma.Decimal.rounding
try {
Prisma.Decimal.set({ rounding: Prisma.Decimal.ROUND_DOWN })
expect(calculateSalaryPay(100.5, wages('0.10', '0.20', '0.50'))).toEqual({ performanceCoefficient: 1.01, currentTotal: '0.81' })
expect(calculateSalaryPay(103.04, wages('100.01', '20.02', '30.03'))).toEqual({ performanceCoefficient: 1.03, currentTotal: '150.96' })
expect(calculateSalaryPay(100, wages('99999999.99', '0.01', '0'))).toEqual({ performanceCoefficient: 1, currentTotal: '100000000.00' })
} finally { Prisma.Decimal.set({ rounding: previous }) }
})
test.each([null, NaN, Infinity])('缺失或非有限得分%s不计算系数和工资', score => {
expect(calculateSalaryPay(score, wages())).toEqual({ performanceCoefficient: null, currentTotal: null })
})