low-price-api-contract.test.mjs
8.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import assert from 'node:assert/strict'
import { generate } from '@babel/generator'
import { parse } from '@babel/parser'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, it } from 'node:test'
import { runInNewContext } from 'node:vm'
import {
filterOrderMerchantOptions,
normalizeOrderMerchantOptions
} from '../../components/orderPromptPop/orderMerchantContext.helpers.js'
const testDir = path.dirname(fileURLToPath(import.meta.url))
const projectRoot = path.resolve(testDir, '../..')
async function source(relativePath) {
return readFile(path.join(projectRoot, relativePath), 'utf8')
}
async function loadOrderPromptComponent(api) {
const componentSource = await source(
'components/orderPromptPop/orderPromptPop.js'
)
const ast = parse(componentSource, { sourceType: 'module' })
ast.program.body = ast.program.body.filter(
node => node.type !== 'ImportDeclaration'
)
let componentConfig
runInNewContext(generate(ast).code, {
Component(config) {
componentConfig = config
},
api,
buildLowPriceMerchantContext: () => ({}),
filterOrderMerchantOptions,
normalizeOrderMerchantOptions,
wx: {
$util: {
showToast() {}
}
}
})
return componentConfig
}
async function loadStoreSelectComponent(api) {
const componentSource = await source(
'components/StoreSelectPopup/StoreSelectPopup.js'
)
const ast = parse(componentSource, { sourceType: 'module' })
ast.program.body = ast.program.body.filter(
node => node.type !== 'ImportDeclaration'
)
let componentConfig
runInNewContext(generate(ast).code, {
Component(config) {
componentConfig = config
},
IMG_URL: '',
api,
normalizeOrderMerchantOptions,
wx: {
$util: {
showToast() {}
}
}
})
return componentConfig
}
describe('低价单接口与页面接线契约', () => {
it('订单列表与统计使用区域经理低价单接口', async () => {
const apiSource = await source('pages/tabWorkOrder/api.js')
assert.match(apiSource, /dj\.api\.smartcarlife\.order\.search\.containPackageGoods\.byArea/)
assert.match(apiSource, /dj\.api\.smartcarlife\.order\.statByApplyStatus/)
assert.match(apiSource, /lowPriceFlag\s*:\s*1/)
})
it('公共 API 同时提供 curYqList 与 shopWithYqList', async () => {
const apiSource = await source('api/api.js')
assert.match(apiSource, /dj\.api\.smartcarlife\.serviceMerchant\.curYqList/)
assert.match(apiSource, /dj\.api\.smartcarlife\.serviceMerchant\.shopWithYqList/)
})
it('选择下单门店按精品升级场景查询账号关联门店', async () => {
const componentConfig = await loadOrderPromptComponent({
getYQList() {
return Promise.reject(new Error('不应使用 curYqList 加载下单门店'))
},
getShopWithYqList(params) {
if (params?.sceneType !== 1) {
return Promise.resolve({
code: 400,
message: 'sceneType 为必填参数',
data: []
})
}
return Promise.resolve({
code: 0,
data: {
list: [
{
merchantId: 100,
merchantName: '园区甲',
shopMerchantList: [
{ merchantId: 201, merchantName: '4S 店一' }
]
}
]
}
})
}
})
const component = {
data: JSON.parse(JSON.stringify(componentConfig.data)),
setData(patch) {
Object.assign(this.data, patch)
}
}
await componentConfig.methods.loadOrderMerchantList.call(component)
assert.deepEqual(
JSON.parse(JSON.stringify(component.data.merchantList)),
[
{
merchantId: 201,
merchantName: '4S 店一',
yqMerchantId: 100,
yqMerchantName: '园区甲',
key: '100:201'
}
]
)
assert.equal(component.data.merchantError, '')
})
it('低价单筛选按精品升级场景查询账号关联门店', async () => {
const componentConfig = await loadStoreSelectComponent({
getShopWithYqList(params) {
if (params?.sceneType !== 1) {
return Promise.resolve({
code: 400,
message: 'sceneType 为必填参数',
data: []
})
}
return Promise.resolve({
code: 0,
data: [
{
merchantId: 100,
merchantName: '园区甲',
shopMerchantList: [
{ merchantId: 201, merchantName: '4S 店一' }
]
}
]
})
}
})
const component = {
data: JSON.parse(JSON.stringify(componentConfig.data)),
setData(patch) {
Object.assign(this.data, patch)
}
}
componentConfig.methods.getLowPriceStoreList.call(
component,
2,
'',
[]
)
await new Promise(resolve => setImmediate(resolve))
assert.deepEqual(
JSON.parse(JSON.stringify(component.data.storeRangeList || [])),
[
{
merchantId: '',
merchantName: '全部',
active: true
},
{
merchantId: 201,
merchantName: '4S 店一',
yqMerchantId: 100,
yqMerchantName: '园区甲',
key: '100:201',
active: false
}
]
)
})
it('工作台启用两步低价单弹层并把门店参数传给待补录', async () => {
const [pageSource, helperSource, templateSource] = await Promise.all([
source('pages/tab/workspace/workspace.js'),
source('pages/tab/workspace/services/workOrderEntry.helpers.js'),
source('pages/tab/workspace/workspace.wxml')
])
assert.match(templateSource, /low-price-mode/)
assert.match(templateSource, /lowPriceFlag="\{\{/)
assert.match(templateSource, /merchantId="\{\{/)
assert.match(templateSource, /yqMerchantId="\{\{/)
assert.match(pageSource, /resolveDirectWorkOrderAction/)
assert.match(helperSource, /sellerMerchantId/)
})
it('待补录分页携带低价单、4S 店和园区店参数', async () => {
const componentSource = await source('tabIndex/components/recordingOrderPopup/recordingOrderPopup.js')
assert.match(componentSource, /lowPriceFlag/)
assert.match(componentSource, /merchantId/)
assert.match(componentSource, /yqMerchantId/)
assert.match(componentSource, /params\.lowPriceFlag\s*=\s*lowPriceFlag/)
assert.match(componentSource, /params\.merchantId\s*=\s*merchantId/)
assert.match(componentSource, /params\.yqMerchantId\s*=\s*yqMerchantId/)
assert.match(componentSource, /waitinputorder\(params\)/)
})
it('创建页把低价单上下文用于待补录统计和补录弹层', async () => {
const [pageSource, templateSource] = await Promise.all([
source('tabWorkOrder/pages/createOrder/createOrder.js'),
source('tabWorkOrder/pages/createOrder/createOrder.wxml')
])
assert.match(pageSource, /mergeLowPriceOrderContext/)
assert.match(pageSource, /waitinputorder\(\{[\s\S]*lowPriceFlag/)
assert.match(templateSource, /lowPriceFlag="\{\{\s*orderObj\.lowPriceFlag/)
assert.match(templateSource, /yqMerchantId="\{\{\s*orderObj\.yqMerchantId/)
})
it('低价单弹层样式使用微信 sass 插件要求的花括号语法', async () => {
const styleSource = await source(
'components/orderPromptPop/orderPromptPop.sass'
)
const firstSelector = styleSource
.split(/\r?\n/)
.map(line => line.trim())
.find(line => line.startsWith('.'))
assert.match(firstSelector || '', /\{$/)
assert.equal(
(styleSource.match(/\{/g) || []).length,
(styleSource.match(/\}/g) || []).length
)
})
it('低价单两个步骤使用统一响应式高度并为底部安全区预留空间', async () => {
const [componentSource, templateSource] = await Promise.all([
source('components/orderPromptPop/orderPromptPop.js'),
source('components/orderPromptPop/orderPromptPop.wxml')
])
assert.match(
templateSource,
/height="\{\{\s*lowPriceMode\s*\?\s*merchantHeight\s*:/
)
assert.match(componentSource, /env\(safe-area-inset-bottom\)/)
})
it('低价单收款方式位于固定头部下方的剩余高度滚动区', async () => {
const [templateSource, styleSource] = await Promise.all([
source('components/orderPromptPop/orderPromptPop.wxml'),
source('components/orderPromptPop/orderPromptPop.sass')
])
assert.match(templateSource, /<view class="prompt-layout">/)
assert.match(
templateSource,
/<scroll-view[\s\S]*class="payment-scroll"[\s\S]*scroll-y[\s\S]*>[\s\S]*<view class="prompt_content"/
)
assert.match(styleSource, /\.prompt-layout\s*\{[\s\S]*height:\s*100%/)
assert.match(styleSource, /\.payment-scroll\s*\{[\s\S]*flex:\s*1/)
})
})