work-order-list-page.test.mjs
8.88 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
import assert from 'node:assert/strict'
import { generate } from '@babel/generator'
import { parse } from '@babel/parser'
import { access, readFile } from 'node:fs/promises'
import { describe, it } from 'node:test'
import { runInNewContext } from 'node:vm'
async function loadListPage() {
await assert.doesNotReject(
access('tabWorkOrder/pages/index/index.js'),
'迁移后的工单列表实现不存在'
)
const pageSource = await readFile(
'tabWorkOrder/pages/index/index.js',
'utf8'
)
const ast = parse(pageSource, { sourceType: 'module' })
ast.program.body = ast.program.body.filter(
node => node.type !== 'ImportDeclaration'
)
let pageConfig
const navigateBackCalls = []
const showTabBarCalls = []
const routerCalls = []
const wxRuntime = {
$orderListRefresh: false,
navigateBack: options => navigateBackCalls.push(options),
showTabBar: options => showTabBarCalls.push(options),
$router: {
go(name, params) {
routerCalls.push({ name, params })
}
}
}
runInNewContext(generate(ast).code, {
Page(config) {
pageConfig = config
},
order_mixins: {
data: {}
},
wx: wxRuntime,
console,
setTimeout,
clearTimeout
})
return {
pageConfig,
navigateBackCalls,
showTabBarCalls,
routerCalls,
wxRuntime
}
}
function createPageContext(pageConfig, initialData) {
const context = {
...pageConfig,
data: {
...pageConfig.data,
...initialData
},
setData(patch, callback) {
Object.assign(this.data, patch)
callback?.()
}
}
return context
}
describe('工单列表页面行为', () => {
it('筛选层打开时顶部返回只关闭筛选层', async () => {
const { pageConfig, navigateBackCalls, showTabBarCalls } =
await loadListPage()
assert.equal(typeof pageConfig.handleBack, 'function')
assert.equal(typeof pageConfig.closeFilter, 'function')
const context = createPageContext(pageConfig, { show_confirm: true })
pageConfig.handleBack.call(context)
assert.equal(context.data.show_confirm, false)
assert.equal(navigateBackCalls.length, 0)
assert.equal(showTabBarCalls.length, 1)
})
it('筛选层关闭时顶部返回上一页', async () => {
const { pageConfig, navigateBackCalls, showTabBarCalls } =
await loadListPage()
const context = createPageContext(pageConfig, { show_confirm: false })
pageConfig.handleBack.call(context)
assert.equal(navigateBackCalls.length, 1)
assert.equal(showTabBarCalls.length, 0)
})
it('列表页仅注册低价单新增流程,不恢复扫码旧流程', async () => {
const { pageConfig } = await loadListPage()
const pageJson = JSON.parse(
await readFile('tabWorkOrder/pages/index/index.json', 'utf8')
)
for (const methodName of [
'scan',
'promptHandleConfirm',
'promptCancel',
'tipsEvent',
'closeSwitchPopup',
'onPageScroll'
]) {
assert.equal(pageConfig[methodName], undefined)
}
for (const stateName of [
'showIcon1',
'showIcon2',
'isScan',
'sceneObj',
'showPromptDialog'
]) {
assert.equal(pageConfig.data[stateName], undefined)
}
assert.equal(pageConfig.data.showLowPriceOrderPrompt, false)
assert.equal(pageConfig.data.showOrderpop, false)
assert.equal(typeof pageConfig.openLowPriceOrderPrompt, 'function')
assert.equal(typeof pageConfig.onLowPriceOrderPromptConfirm, 'function')
assert.equal(
pageJson.usingComponents['order-prompt-pop'],
'/components/orderPromptPop/orderPromptPop'
)
assert.equal(
pageJson.usingComponents['recording-order-popup'],
'/tabIndex/components/recordingOrderPopup/recordingOrderPopup'
)
})
it('列表模板固定显示返回和新建低价单,不渲染扫码或旧悬浮箭头', async () => {
const [template, style, createIcon] = await Promise.all([
readFile('tabWorkOrder/pages/index/index.wxml', 'utf8'),
readFile('tabWorkOrder/pages/index/index.sass', 'utf8'),
readFile('tabWorkOrder/images/new-low-price-order.png')
])
assert.match(
template,
/<navigation-bar\b[^>]*showBack="\{\{\s*true\s*\}\}"[^>]*bindback="handleBack"/
)
assert.match(
template,
/<page-container\b[^>]*bind:afterleave="closeFilter"/
)
assert.doesNotMatch(template, /data-flag="new"/)
assert.doesNotMatch(template, /bindtap="scan"/)
assert.doesNotMatch(template, /class="arrowIcon"/)
assert.doesNotMatch(template, /<order-prompt-popup\b/)
assert.match(template, /class="low-price-create"/)
assert.match(
template,
/<image\s+src="\/tabWorkOrder\/images\/new-low-price-order\.png"\s*\/>/
)
assert.match(template, /<order-prompt-pop\b/)
assert.match(template, /<recording-order-popup\b/)
assert.match(template, /low-price-mode="\{\{\s*true\s*\}\}"/)
assert.doesNotMatch(style, /\.fixed-btns\b|\.arrowIcon\b/)
assert.deepEqual(
[...createIcon.subarray(0, 8)],
[137, 80, 78, 71, 13, 10, 26, 10]
)
assert.equal(createIcon.readUInt32BE(16), 56)
assert.equal(createIcon.readUInt32BE(20), 56)
})
it('低价提单列表使用关键词搜索和专属卡片且不显示筛选按钮', async () => {
const [pageSource, template, style] = await Promise.all([
readFile('tabWorkOrder/pages/index/index.js', 'utf8'),
readFile('tabWorkOrder/pages/index/index.wxml', 'utf8'),
readFile('tabWorkOrder/pages/index/index.sass', 'utf8')
])
assert.match(template, />\s*低价提单\s*<\/navigation-bar>/)
assert.match(template, /placeholder="关键词"/)
assert.doesNotMatch(template, /bindtap="showConfirm"/)
assert.doesNotMatch(template, /class="search-filter"/)
assert.match(template, /class="low-price-card-header"/)
assert.match(template, /class="low-price-card-row"/)
assert.match(template, /class="low-price-view"/)
for (const label of [
'订单类型',
'销售顾问',
'转化人员',
'工单编号',
'工单时间'
]) {
assert.match(template, new RegExp(`>${label}<`))
}
assert.match(pageSource, /lowPriceOrderTypeText\s*=\s*'低价订单'/)
assert.match(pageSource, /salesConsultantText/)
assert.match(pageSource, /conversionStaffText/)
assert.match(style, /\.low-price-card\b/)
assert.match(style, /\.low-price-view\b/)
})
it('routes list cards directly by order status without permission gates', async () => {
const { pageConfig, routerCalls } = await loadListPage()
const template = await readFile(
'tabWorkOrder/pages/index/index.wxml',
'utf8'
)
const item = {
status: 1,
orderCode: 'FOLLOW-1',
lowPriceFlag: 1,
merchantId: 'SHOP-1',
customer: { customerName: '张三', vin: 'VIN-001' },
subOrderList: [{ merchantGoodsId: 'GOODS-1', finalPrice: 99 }],
selectPayType: 3
}
const context = createPageContext(pageConfig, {})
const routeBindings =
template.match(
/data-name="\{\{\s*li\.status\s*==\s*3\s*\?\s*'confirmOrder'\s*:\s*li\.status\s*==\s*1\s*\?\s*'createOrder'\s*:\s*'orderDetail'\s*\}\}"/g
) || []
assert.equal(
routeBindings.length,
2,
'card body and footer must use the same status target'
)
pageConfig.jump.call(context, {
currentTarget: { dataset: { name: 'createOrder', item } }
})
assert.equal(routerCalls.length, 1)
assert.equal(routerCalls[0].name, 'tabWorkOrder.createOrder')
assert.deepEqual(
JSON.parse(JSON.stringify(routerCalls[0].params)),
{
...item,
page: 'edit'
}
)
})
it('详情卡片使用命名路由打开且原参数不变', async () => {
const { pageConfig, routerCalls } = await loadListPage()
const item = {
status: 4,
orderCode: 'DETAIL-1',
merchantId: 'SHOP-1'
}
const context = createPageContext(pageConfig, {})
pageConfig.jump.call(context, {
currentTarget: { dataset: { name: 'orderDetail', item } }
})
assert.deepEqual(
JSON.parse(JSON.stringify(routerCalls)),
[{ name: 'tabWorkOrder.orderDetail', params: item }]
)
})
it('返回列表时消费详情设置的刷新标记并重新查询', async () => {
const { pageConfig, wxRuntime } = await loadListPage()
let searchCalls = 0
let settingCalls = 0
const context = createPageContext(pageConfig, {
searchObj: { merchantId: 'OLD-SHOP' },
keyword: '旧关键词'
})
context.search = () => {
searchCalls += 1
}
context.getCreateOrderSetting = () => {
settingCalls += 1
}
wxRuntime.$orderListRefresh = true
pageConfig.loadPageData.call(context)
assert.equal(wxRuntime.$orderListRefresh, false)
assert.deepEqual(JSON.parse(JSON.stringify(context.data.searchObj)), {})
assert.equal(context.data.keyword, '')
assert.equal(searchCalls, 1)
assert.equal(settingCalls, 1)
})
})