merchantStaffPicker.component.test.mjs
2.2 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
import assert from 'node:assert/strict'
import { register } from 'node:module'
import test from 'node:test'
register(
'../../../../tests/work-order-migration/shims/create-order-context-loader.mjs',
import.meta.url
)
function createComponentInstance(componentConfig, properties) {
const instance = {
properties: { ...properties },
data: structuredClone(componentConfig.data),
setData(patch) {
Object.assign(this.data, patch)
},
triggerEvent() {}
}
Object.entries(componentConfig.methods).forEach(([name, method]) => {
instance[name] = method.bind(instance)
})
return instance
}
test('同一次打开重复同步 show=true 时只加载一次人员数据', async () => {
let componentConfig
let employeeRequestCount = 0
globalThis.Component = config => {
componentConfig = config
}
globalThis.wx = {
$util: {
showToast() {}
}
}
globalThis.__WORK_ORDER_MOCK_API__ = {
getEmployeeList() {
employeeRequestCount += 1
return {
code: 0,
data: [{ userId: 'staff-1', name: '销售顾问一' }]
}
}
}
try {
await import(
`../components/merchant-staff-picker/merchant-staff-picker.js?single-open=${Date.now()}`
)
const instance = createComponentInstance(componentConfig, {
show: true,
mode: 'sales',
orderObj: {
lowPriceFlag: 1,
merchantId: 44210895,
merchantName: '当前下单门店'
},
userContext: {},
pointsMultiRole: 'Y',
orderCreateType: 'createdOrder'
})
const showObserver = componentConfig.properties.show.observer
showObserver.call(instance, true, false)
showObserver.call(instance, true, true)
await new Promise(resolve => setImmediate(resolve))
assert.equal(employeeRequestCount, 1)
assert.equal(instance.data.staffList[0].staffName, '销售顾问一')
instance.properties.show = false
showObserver.call(instance, false, true)
instance.properties.show = true
showObserver.call(instance, true, false)
await new Promise(resolve => setImmediate(resolve))
assert.equal(employeeRequestCount, 2)
} finally {
delete globalThis.__WORK_ORDER_MOCK_API__
delete globalThis.Component
}
})