simpleQuotationTotal.helpers.test.mjs
4.83 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { calculateSimpleQuotationTotal } from '../pages/carQuotation/services/simpleQuotationTotal.helpers.js'
describe('simpleQuotationTotal.helpers', () => {
it('IT-EQ-SIMPLE-TOTAL-001: returns an exact zero total for an empty selection', () => {
assert.deepEqual(calculateSimpleQuotationTotal([]), {
goodsList: [],
totalCents: 0,
totalYuan: '0.00'
})
})
it('IT-EQ-SIMPLE-TOTAL-002: adds normal, multi-spec and gift rows as integer cents', () => {
const goodsList = [
{ count: 2, goods: { price: 101 } },
{
count: 3,
multipleSpecificationsGoods: { goods: { price: 33 } }
},
{
count: 99,
goods: { price: 999, finalPrice: 0, discountType: 4 }
}
]
const snapshot = structuredClone(goodsList)
const result = calculateSimpleQuotationTotal(goodsList)
assert.equal(result.totalCents, 301)
assert.equal(result.totalYuan, '3.01')
assert.deepEqual(goodsList, snapshot)
assert.notEqual(result.goodsList, goodsList)
})
it('IT-EQ-SIMPLE-TOTAL-003: recalculates package child discounts before applying quantity', () => {
const goodsList = [
{
count: 2,
price: 999999,
packageGoods: {
price: 999999,
goodsList: [
{
price: 125,
finalPrice: 999,
discountType: 2,
discountValue: '0.05'
},
{
price: 250,
finalPrice: 999,
discountType: 1,
discountValue: '9'
}
]
}
}
]
const snapshot = structuredClone(goodsList)
const result = calculateSimpleQuotationTotal(goodsList)
assert.equal(result.totalCents, 690)
assert.equal(result.totalYuan, '6.90')
assert.equal(result.goodsList[0].packageGoods.price, 345)
assert.equal(result.goodsList[0].price, 345)
assert.deepEqual(goodsList, snapshot)
})
it('IT-EQ-SIMPLE-TOTAL-006: recalculates a normal row from its current base price and discount', () => {
const goodsList = [
{
count: 2,
goods: {
price: 101,
finalPrice: 999,
discountType: 1,
discountValue: '8.5'
}
}
]
const snapshot = structuredClone(goodsList)
const result = calculateSimpleQuotationTotal(goodsList)
assert.equal(result.totalCents, 172)
assert.equal(result.totalYuan, '1.72')
assert.equal(result.goodsList[0].goods.finalPrice, 86)
assert.deepEqual(goodsList, snapshot)
})
it('IT-EQ-SIMPLE-TOTAL-004: formats a large safe integer total without a float tail', () => {
const result = calculateSimpleQuotationTotal([
{ count: 1, goods: { price: 9007199254740900 } }
])
assert.equal(result.totalCents, 9007199254740900)
assert.equal(result.totalYuan, '90071992547409.00')
assert.equal(Number.isSafeInteger(result.totalCents), true)
})
it('IT-EQ-SIMPLE-TOTAL-005: rejects malformed strategy totals instead of producing NaN or silently zeroing them', () => {
assert.throws(
() =>
calculateSimpleQuotationTotal([
{
packageGoods: {
price: 100,
goodsList: [{ price: 'invalid' }]
}
}
]),
/商品金额/
)
})
it('IT-EQ-SIMPLE-TOTAL-007: rejects invalid normal and multi-spec cent prices instead of folding them to zero', () => {
for (const goodsList of [
[{ count: 1, goods: { price: 'invalid' } }],
[
{
count: 1,
multipleSpecificationsGoods: { goods: { price: '12.5' } }
}
]
]) {
assert.throws(
() => calculateSimpleQuotationTotal(goodsList),
RangeError
)
}
})
it('IT-EQ-SIMPLE-TOTAL-008: rejects invalid quantities instead of treating them as a zero subtotal', () => {
for (const count of [-1, '1.5', 'invalid', Number.MAX_SAFE_INTEGER + 1]) {
assert.throws(
() =>
calculateSimpleQuotationTotal([
{ count, goods: { price: 100 } }
]),
RangeError
)
}
})
it('IT-EQ-SIMPLE-TOTAL-009: rejects a count-price product beyond the safe integer range', () => {
assert.throws(
() =>
calculateSimpleQuotationTotal([
{ count: 2, goods: { price: Number.MAX_SAFE_INTEGER } }
]),
RangeError
)
})
it('IT-EQ-SIMPLE-TOTAL-010: preserves explicit zero prices, gifts and zero quantities as valid zero totals', () => {
const result = calculateSimpleQuotationTotal([
{ count: 2, goods: { price: 0 } },
{ count: 3, goods: { price: 999, discountType: 4 } },
{ count: 0, goods: { price: 123 } }
])
assert.equal(result.totalCents, 0)
assert.equal(result.totalYuan, '0.00')
})
})