low-price-api-contract.test.mjs
15.9 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
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: () => ({}),
clearTimeout,
filterOrderMerchantOptions,
normalizeOrderMerchantOptions,
setTimeout,
wx: {
$util: {
showToast() {}
}
}
})
return componentConfig
}
function createOrderPromptInstance(componentConfig) {
const component = {
properties: {
lowPriceMode: true,
djQrOnlyMode: false
},
data: JSON.parse(JSON.stringify(componentConfig.data)),
setData(patch) {
Object.assign(this.data, patch)
}
}
Object.entries(componentConfig.methods).forEach(([name, method]) => {
component[name] = method.bind(component)
})
return component
}
async function loadStoreSelectComponent(api) {
const componentSource = await source(
'tabWorkOrder/pages/index/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('tabWorkOrder/pages/index/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('选择下单门店 11 条数据首屏加载 10 条并在触底后加载第 11 条', async () => {
const calls = []
const componentConfig = await loadOrderPromptComponent({
getShopWithYqList(params) {
calls.push({ ...params })
const total = 11
const pageNum = params?.pageNum || 1
const pageSize = params?.pageSize || 10
const start = (pageNum - 1) * pageSize + 1
const end = Math.min(pageNum * pageSize, total)
return Promise.resolve({
code: 0,
data: {
pageNum,
pageSize,
total,
pages: Math.ceil(total / pageSize),
list: Array.from(
{ length: Math.max(0, end - start + 1) },
(_, index) => {
const merchantId = start + index
return {
shopMerchantId: merchantId,
shopMerchantName: `4S 店${merchantId}`,
yqMerchantId: 100,
yqMerchantName: '园区甲'
}
}
)
}
})
}
})
const component = createOrderPromptInstance(componentConfig)
await component.loadOrderMerchantList()
assert.deepEqual(calls, [
{
pageNum: 1,
pageSize: 10,
sceneType: 1,
merchantNameOrId: ''
}
])
assert.equal(component.data.merchantList.length, 10)
assert.equal(component.data.merchantHasMore, true)
await component.loadMoreOrderMerchantList()
assert.deepEqual(calls, [
{
pageNum: 1,
pageSize: 10,
sceneType: 1,
merchantNameOrId: ''
},
{
pageNum: 2,
pageSize: 10,
sceneType: 1,
merchantNameOrId: ''
}
])
assert.equal(component.data.merchantList.length, 11)
assert.equal(component.data.merchantList[10].merchantName, '4S 店11')
assert.equal(component.data.merchantHasMore, false)
})
it('低价单两个步骤默认使用半屏高度', async () => {
const componentConfig = await loadOrderPromptComponent({})
assert.equal(componentConfig.properties.merchantHeight.value, '50%')
})
it('选择下单门店列表不再额外限制八行高度', async () => {
const componentStyle = await source(
'components/orderPromptPop/orderPromptPop.sass'
)
const merchantListStyle = componentStyle.match(
/\.merchant-list\s*\{([\s\S]*?)\}/
)?.[1]
assert.ok(merchantListStyle)
assert.doesNotMatch(merchantListStyle, /max-height:/)
})
it('选择下单门店输入搜索词后通过接口搜索名称或商户号', async () => {
const calls = []
const componentConfig = await loadOrderPromptComponent({
getShopWithYqList(params) {
calls.push({ ...params })
return Promise.resolve({
code: 0,
data: {
pageNum: 1,
pageSize: 10,
total: 1,
pages: 1,
list: [
{
shopMerchantId: 11,
shopMerchantName: '目标门店',
yqMerchantId: 100,
yqMerchantName: '园区甲'
}
]
}
})
}
})
const component = createOrderPromptInstance(componentConfig)
component.onMerchantKeywordInput({ detail: { value: '目标' } })
component.onMerchantKeywordInput({ detail: { value: '目标门店' } })
await new Promise(resolve => setTimeout(resolve, 350))
assert.deepEqual(calls, [
{
pageNum: 1,
pageSize: 10,
sceneType: 1,
merchantNameOrId: '目标门店'
}
])
assert.equal(component.data.merchantList.length, 1)
assert.equal(component.data.merchantList[0].merchantId, 11)
})
it('选择下单门店忽略晚返回的旧搜索结果', async () => {
const pending = new Map()
const componentConfig = await loadOrderPromptComponent({
getShopWithYqList(params) {
return new Promise(resolve => {
pending.set(params.merchantNameOrId, resolve)
})
}
})
const component = createOrderPromptInstance(componentConfig)
const oldRequest = component.loadOrderMerchantList({
reset: true,
keyword: '旧门店'
})
const newRequest = component.loadOrderMerchantList({
reset: true,
keyword: '新门店'
})
pending.get('新门店')({
code: 0,
data: {
pageNum: 1,
pageSize: 10,
total: 1,
pages: 1,
list: [
{
shopMerchantId: 12,
shopMerchantName: '新门店',
yqMerchantId: 100,
yqMerchantName: '园区甲'
}
]
}
})
await newRequest
pending.get('旧门店')({
code: 0,
data: {
pageNum: 1,
pageSize: 10,
total: 1,
pages: 1,
list: [
{
shopMerchantId: 13,
shopMerchantName: '旧门店',
yqMerchantId: 100,
yqMerchantName: '园区甲'
}
]
}
})
await oldRequest
assert.equal(component.data.merchantList.length, 1)
assert.equal(component.data.merchantList[0].merchantName, '新门店')
})
it('输入新关键词后立即忽略防抖开始前返回的旧结果', async () => {
let resolveOldRequest
const componentConfig = await loadOrderPromptComponent({
getShopWithYqList() {
return new Promise(resolve => {
resolveOldRequest = resolve
})
}
})
const component = createOrderPromptInstance(componentConfig)
const oldRequest = component.loadOrderMerchantList({
reset: true,
keyword: '旧门店'
})
component.onMerchantKeywordInput({ detail: { value: '新门店' } })
resolveOldRequest({
code: 0,
data: {
pageNum: 1,
pageSize: 10,
total: 1,
pages: 1,
list: [
{
shopMerchantId: 13,
shopMerchantName: '旧门店',
yqMerchantId: 100,
yqMerchantName: '园区甲'
}
]
}
})
await oldRequest
clearTimeout(component.merchantSearchTimer)
assert.equal(component.data.merchantKeyword, '新门店')
assert.equal(component.data.merchantList.length, 0)
})
it('选择下单门店列表将触底事件接到分页加载方法', async () => {
const templateSource = await source(
'components/orderPromptPop/orderPromptPop.wxml'
)
assert.match(
templateSource,
/class="merchant-list"[\s\S]*bindscrolltolower="loadMoreOrderMerchantList"/
)
})
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/)
})
})