-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathcalculate-sale-earnings.test.ts
More file actions
149 lines (143 loc) · 3.58 KB
/
Copy pathcalculate-sale-earnings.test.ts
File metadata and controls
149 lines (143 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { describe, expect, test } from "vitest";
const percentageEarnings = (
saleAmount: number,
percent: number,
quantity = 1,
) =>
calculateSaleEarnings({
reward: {
type: "percentage",
amountInCents: null,
amountInPercentage: percent,
},
sale: { amount: saleAmount, quantity },
});
describe("calculateSaleEarnings", () => {
describe("percentage – same as the old truncate path", () => {
test.each([
{
label: "whole cents: $10 sale at 20%",
saleAmount: 1000,
percent: 20,
expected: 200,
},
{
label: "whole cents: $10 sale at 50%",
saleAmount: 1000,
percent: 50,
expected: 500,
},
{
label: "whole cents: $19 sale at 10%",
saleAmount: 1900,
percent: 10,
expected: 190,
},
{
label: "1.4¢ truncates and rounds to the same value",
saleAmount: 7,
percent: 20,
expected: 1,
},
{
label: "0.4¢ stays 0 (below half a cent)",
saleAmount: 2,
percent: 20,
expected: 0,
},
{
label: "zero sale amount",
saleAmount: 0,
percent: 20,
expected: 0,
},
{
label: "zero percent",
saleAmount: 1000,
percent: 0,
expected: 0,
},
])("$label → $expected", ({ saleAmount, percent, expected }) => {
expect(percentageEarnings(saleAmount, percent)).toBe(expected);
});
});
describe("percentage – half-up (new vs old truncate)", () => {
test.each([
{
label: "3¢ sale at 20% (ticket: 0.6¢, used to store 0)",
saleAmount: 3,
percent: 20,
expected: 1,
},
{
label: "15¢ sale at 10% (1.5¢, used to store 1)",
saleAmount: 15,
percent: 10,
expected: 2,
},
{
label: "1¢ sale at 50% (0.5¢, used to store 0)",
saleAmount: 1,
percent: 50,
expected: 1,
},
{
label: "500¢ at 2.9% (14.5¢; float 2.9/100 used to round to 14)",
saleAmount: 500,
percent: 2.9,
expected: 15,
},
])("$label → $expected", ({ saleAmount, percent, expected }) => {
expect(percentageEarnings(saleAmount, percent)).toBe(expected);
});
});
test("percentage ignores quantity (uses sale amount only)", () => {
expect(percentageEarnings(1000, 20, 5)).toBe(200);
});
describe("flat", () => {
test.each([
{
label: "single sale",
amountInCents: 5000,
quantity: 1,
expected: 5000,
},
{
label: "quantity multiplies the flat amount",
amountInCents: 500,
quantity: 2,
expected: 1000,
},
{
label: "zero quantity",
amountInCents: 500,
quantity: 0,
expected: 0,
},
])("$label → $expected", ({ amountInCents, quantity, expected }) => {
expect(
calculateSaleEarnings({
reward: {
type: "flat",
amountInCents,
amountInPercentage: null,
},
sale: { amount: 1000, quantity },
}),
).toBe(expected);
});
});
test("returns 0 when reward type is neither flat nor percentage", () => {
expect(
calculateSaleEarnings({
reward: {
type: "unknown" as "flat",
amountInCents: 500,
amountInPercentage: 20,
},
sale: { amount: 1000, quantity: 1 },
}),
).toBe(0);
});
});