goodsPriceBoundary.helpers.test.mjs
3.6 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
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
findGoodsPriceBoundaryError,
validateGoodsPriceInput
} from '../services/goodsPriceBoundary.helpers.js'
describe('goods price boundary validation', () => {
it('rejects negative prices without converting them to positive values', () => {
assert.deepEqual(validateGoodsPriceInput('-1'), {
valid: false,
value: null,
message: '商品价格不能小于0元'
})
})
it('rejects values over 100000 without clamping', () => {
assert.deepEqual(validateGoodsPriceInput('100000.01'), {
valid: false,
value: null,
message: '商品价格不能大于100000元'
})
})
it('accepts exact zero and zero-valued drafts during editing and blur', () => {
const drafts = [
['0', '0'],
['0.', '0.'],
['0.0', '0.0'],
['0.01', '0.01']
]
for (const [rawPrice, expectedValue] of drafts) {
assert.deepEqual(
validateGoodsPriceInput(rawPrice),
{
valid: true,
value: expectedValue,
message: ''
}
)
}
})
it('accepts the upper boundary and normalizes to two decimals', () => {
assert.deepEqual(validateGoodsPriceInput('100000'), {
valid: true,
value: '100000',
message: ''
})
assert.deepEqual(validateGoodsPriceInput('12.345'), {
valid: true,
value: '12.34',
message: ''
})
})
it('finds invalid single, selected SKU and package child prices', () => {
assert.equal(
findGoodsPriceBoundaryError([
{ goodsFlag: 1, goods: { priceBoundaryError: '' } },
{
goodsFlag: 3,
packageGoods: {
goodsList: [
{ priceBoundaryError: '' },
{ priceBoundaryError: '商品价格不能大于100000元' }
]
}
}
]),
'商品价格不能大于100000元'
)
})
it('rejects invalid prices already present in route or echo data', () => {
assert.equal(
findGoodsPriceBoundaryError([
{ goodsFlag: 1, goods: { basePrice: -1, sellPrice: -1 } }
]),
'商品价格不能小于0元'
)
assert.equal(
findGoodsPriceBoundaryError([
{
goodsFlag: 2,
multipleSpecificationsGoods: { goods: { sellPrice: 100000.01 } }
}
]),
'商品价格不能大于100000元'
)
})
it('allows exact zero prices but rejects decimal-zero drafts on submit', () => {
const validGoodsLists = [
[{ goodsFlag: 1, goods: { basePrice: 0, sellPrice: 0 } }],
[{ goodsFlag: 1, goods: { basePrice: '0', sellPrice: '0' } }]
]
const invalidGoodsLists = [
[
{
goodsFlag: 2,
multipleSpecificationsGoods: { goods: { sellPrice: '0.' } }
}
],
[
{
goodsFlag: 3,
packageGoods: {
goodsList: [{ sellPrice: '0.0' }, { sellPrice: '0.00' }]
}
}
]
]
for (const goodsList of validGoodsLists) {
assert.equal(findGoodsPriceBoundaryError(goodsList), '')
}
for (const goodsList of invalidGoodsLists) {
assert.equal(
findGoodsPriceBoundaryError(goodsList),
'商品价格不能等于0元'
)
}
})
it('allows a zero gift final price when its editable base price is positive', () => {
assert.equal(
findGoodsPriceBoundaryError([
{
goodsFlag: 1,
goods: {
basePrice: 10,
sellPrice: 10,
finalPrice: 0,
discountType: 4
}
}
]),
''
)
})
})