orderValidation.helpers.test.mjs
4.37 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
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { resolveConfiguredPhone, validateBoutiqueRequiredFields } from "../services/orderValidation.helpers.js";
function createValidValidationInput(overrides = {}) {
const { customer: customerOverrides = {}, orderObj: orderObjOverrides = {}, ...inputOverrides } = overrides;
const customer = {
customerName: "张三",
salesType: 0,
customerPhone: "13800138000",
carBrand: "宝马",
standNo: "LSVAA4182E2123456",
plateNumber: "粤A12345",
actualDeliveryDate: "2026-09-09",
...customerOverrides,
};
const orderObj = {
userId: "staff-1",
baseSceneCode: "scene-1",
merchantId: "merchant-1",
employeeName: "销售顾问",
sellerUserId: "seller-1",
sellerName: "录单员",
sellerMerchantId: "seller-merchant-1",
payeeType: 0,
selectPayType: 1,
remark: "客户到店",
customerCouponCode: "COUPON-1",
...orderObjOverrides,
customer,
};
return {
save: false,
requiredList: ["carBrand", "standNo", "plateNumber"],
showActualDeliveryDate: false,
...inputOverrides,
orderObj,
};
}
describe("orderValidation.helpers", () => {
it("IT-WO-VALIDATION-001: resolves the matching merchant phone without changing the configuration array", () => {
const configs = [
{ shopMerchantId: "merchant-1", phoneNumber: "13800138001" },
{ shopMerchantId: "merchant-2", phoneNumber: "13800138002" },
];
const snapshot = structuredClone(configs);
assert.equal(resolveConfiguredPhone(configs, "merchant-1"), "13800138001");
assert.deepEqual(configs, snapshot);
});
it("IT-WO-VALIDATION-002: falls back to the final configured phone without popping the source array", () => {
const configs = [
{ shopMerchantId: "merchant-1", phoneNumber: "13800138001" },
{ shopMerchantId: "merchant-2", phoneNumber: "13800138002" },
];
const snapshot = structuredClone(configs);
assert.equal(resolveConfiguredPhone(configs, "missing"), "13800138002");
assert.deepEqual(configs, snapshot);
});
it("IT-WO-VALIDATION-003: empty phone configurations have no fallback", () => {
assert.equal(resolveConfiguredPhone([], "merchant-1"), undefined);
assert.equal(resolveConfiguredPhone(undefined, "merchant-1"), undefined);
});
it("IT-WO-VALIDATION-004: returns one structured failure for remark, delivery date, phone, VIN and dynamic required fields", () => {
const cases = [
{
name: "other payment requires a remark",
input: createValidValidationInput({
orderObj: { selectPayType: 7, remark: "" },
}),
expected: { ok: false, message: "备注不能为空", presentation: "toast" },
},
{
name: "visible delivery date is required",
input: createValidValidationInput({
showActualDeliveryDate: true,
customer: { actualDeliveryDate: "" },
}),
expected: { ok: false, errorKey: "actualDeliveryDate" },
},
{
name: "phone must use the mobile format",
input: createValidValidationInput({
customer: { customerPhone: "12345" },
}),
expected: { ok: false, message: "请输入正确的手机号", presentation: "toast" },
},
{
name: "VIN cannot contain punctuation for a payable order",
input: createValidValidationInput({
customer: { standNo: "LSVAA4182E2-12345" },
}),
expected: { ok: false, message: "请输入正确的VIN码", presentation: "toast" },
},
{
name: "merchant-configured car model remains dynamically required",
input: createValidValidationInput({
customer: { carBrand: "" },
}),
expected: { ok: false, errorKey: "carBrand" },
},
{
name: "saving a customer record still requires the coupon code",
input: createValidValidationInput({
save: true,
orderObj: { customerCouponCode: "" },
}),
expected: { ok: false, errorKey: "customerCouponCode" },
},
];
for (const { name, input, expected } of cases) {
const snapshot = structuredClone(input);
assert.deepEqual(validateBoutiqueRequiredFields(input), expected, name);
assert.deepEqual(input, snapshot, `${name}: input mutated`);
}
});
it("IT-WO-VALIDATION-005: valid zero-valued sales and payee fields are accepted without changing the order", () => {
const input = createValidValidationInput({ showActualDeliveryDate: true });
const snapshot = structuredClone(input);
assert.deepEqual(validateBoutiqueRequiredFields(input), { ok: true });
assert.deepEqual(input, snapshot);
});
});