customerProfileEffects.test.mjs
5.78 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
isRemarkEmpty,
isStandNoEmpty,
isCarModelEmpty,
resolveCustomerId,
shouldFetchCustomerProfile,
parseCustomerProfileResponse,
buildCustomerProfileEchoPatch
} from '../services/customerProfileEffects.js'
describe('customerProfileEffects', () => {
it('isRemarkEmpty treats blank and whitespace as empty', () => {
assert.equal(isRemarkEmpty(''), true)
assert.equal(isRemarkEmpty(' '), true)
assert.equal(isRemarkEmpty(null), true)
assert.equal(isRemarkEmpty(undefined), true)
assert.equal(isRemarkEmpty('有备注'), false)
})
it('isStandNoEmpty treats blank and whitespace as empty', () => {
assert.equal(isStandNoEmpty(''), true)
assert.equal(isStandNoEmpty(' '), true)
assert.equal(isStandNoEmpty('LSVAA4182E2123456'), false)
})
it('isCarModelEmpty checks carSeriesName', () => {
assert.equal(isCarModelEmpty({}), true)
assert.equal(isCarModelEmpty({ carBrand: '宝马' }), true)
assert.equal(isCarModelEmpty({ carSeriesName: '3系' }), false)
assert.equal(isCarModelEmpty({ carSeriesName: ' ' }), true)
})
it('resolveCustomerId prefers customerId then id', () => {
assert.equal(resolveCustomerId({ customerId: 1, id: 2 }), 1)
assert.equal(resolveCustomerId({ id: 2 }), 2)
assert.equal(resolveCustomerId(null), undefined)
assert.equal(resolveCustomerId({}), undefined)
})
it('shouldFetchCustomerProfile when any of remark standNo car model is empty', () => {
const full = {
remark: '备注',
customer: {
standNo: 'LSVAA4182E2123456',
carSeriesName: '3系'
}
}
assert.equal(shouldFetchCustomerProfile(full), false)
assert.equal(
shouldFetchCustomerProfile({ remark: '', customer: full.customer }),
true
)
assert.equal(
shouldFetchCustomerProfile({
remark: 'x',
customer: { standNo: '', carSeriesName: '3系' }
}),
true
)
assert.equal(
shouldFetchCustomerProfile({
remark: 'x',
customer: { standNo: 'VIN', carSeriesName: '' }
}),
true
)
assert.equal(shouldFetchCustomerProfile(null), false)
})
it('parseCustomerProfileResponse accepts string code "0"', () => {
const res = {
code: '0',
data: {
id: 33842,
remark: '888888888',
standNo: '33333333333333333',
carBrand: 'AITO 问界',
carChildsBrandName: '问界M5',
carSeriesName: '问界M5',
carBrandId: 3454,
carChildsBrandId: 1149146,
carSeriesId: 1149146
}
}
assert.deepEqual(parseCustomerProfileResponse(res), {
remark: '888888888',
standNo: '33333333333333333',
carBrand: 'AITO 问界',
carChildsBrandName: '问界M5',
carSeriesName: '问界M5',
carBrandId: 3454,
carChildsBrandId: 1149146,
carSeriesId: 1149146
})
})
it('parseCustomerProfileResponse reads profile fields', () => {
const res = {
code: 0,
data: {
remark: ' 备注 ',
standNo: ' LSVAA4182E2123456 ',
carBrand: '宝马',
carChildsBrandName: '华晨宝马',
carSeriesName: '3系',
carBrandId: 1,
carChildsBrandId: 2,
carSeriesId: 3
}
}
assert.deepEqual(parseCustomerProfileResponse(res), {
remark: '备注',
standNo: 'LSVAA4182E2123456',
carBrand: '宝马',
carChildsBrandName: '华晨宝马',
carSeriesName: '3系',
carBrandId: 1,
carChildsBrandId: 2,
carSeriesId: 3
})
assert.equal(parseCustomerProfileResponse({ code: 1, data: {} }), null)
assert.equal(parseCustomerProfileResponse({ code: 0, data: {} }), null)
assert.equal(parseCustomerProfileResponse(null), null)
})
it('buildCustomerProfileEchoPatch only fills empty fields', () => {
const orderObj = {
remark: '已有备注',
customer: {
standNo: '',
carSeriesName: ''
}
}
const profile = {
remark: '接口备注',
standNo: 'LSVAA4182E2123456',
carBrand: '宝马',
carChildsBrandName: '华晨宝马',
carSeriesName: '3系',
carBrandId: 10,
carChildsBrandId: 20,
carSeriesId: 30
}
const { patch, echoedStandNo } = buildCustomerProfileEchoPatch(
orderObj,
profile
)
assert.equal(patch['orderObj.remark'], undefined)
assert.equal(patch['orderObj.customer.standNo'], 'LSVAA4182E2123456')
assert.equal(patch['orderObj.customer.carBrand'], '宝马')
assert.equal(patch['orderObj.customer.carChildsBrandName'], '华晨宝马')
assert.equal(patch['orderObj.customer.carSeriesName'], '3系')
assert.equal(patch['orderObj.customer.carBrandId'], 10)
assert.equal(patch['orderObj.customer.carChildsBrandId'], 20)
assert.equal(patch['orderObj.customer.carSeriesId'], 30)
assert.equal(echoedStandNo, 'LSVAA4182E2123456')
})
it('buildCustomerProfileEchoPatch patches remark with word count', () => {
const { patch } = buildCustomerProfileEchoPatch(
{ remark: '', customer: { standNo: 'VIN', carSeriesName: 'X' } },
{ remark: 'abc', standNo: '', carBrand: '', carChildsBrandName: '', carSeriesName: '' }
)
assert.deepEqual(patch, {
'orderObj.remark': 'abc',
currentWordNumber: 3
})
})
it('buildCustomerProfileEchoPatch returns empty patch when nothing to fill', () => {
const orderObj = {
remark: 'x',
customer: { standNo: 'VIN', carSeriesName: '车系' }
}
const { patch, echoedStandNo } = buildCustomerProfileEchoPatch(orderObj, {
remark: 'y',
standNo: 'OTHER',
carBrand: '宝马',
carChildsBrandName: '子品牌',
carSeriesName: '3系'
})
assert.deepEqual(patch, {})
assert.equal(echoedStandNo, '')
})
})