form-field-multiple-select-option-visibility.test.mjs
5.46 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
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { describe, it } from 'node:test'
import vm from 'node:vm'
import {
filterMultipleSelectValueByVisibleOptions,
getCanonicalOptionKey,
getVisibleFieldOptions,
resolveOptionForSelection
} from '../form-engine/option-visibility.js'
function loadComponentDefinition({
fetchFieldOptions = async () => ({ code: 0, data: [] })
} = {}) {
const source = readFileSync(
new URL('../components/form-field-multipleSelect/form-field-multipleSelect.js', import.meta.url),
'utf8'
)
let definition
const stubs = {
'../../api/fieldOptionsService.js': { fetchFieldOptions },
'../../form-engine/multiple-select-value.js': {
coerceMultipleSelectValue: value => Array.isArray(value) ? value : value == null ? value : [value]
},
'../../form-engine/multiple-select-display.helpers.js': {
resolveMultipleSelectItemDisplayLabel: ({ selectedKey, matched }) => matched?.label || selectedKey
},
'../../form-engine/field-submit-value.js': { normalizeOptionRecord: option => option },
'../../form-engine/option-visibility.js': {
filterMultipleSelectValueByVisibleOptions,
getCanonicalOptionKey,
getVisibleFieldOptions,
resolveOptionForSelection
},
'../../utils/field-options-popup.js': {
isRemoteFieldOptionsKey: key => ['orderList', 'storeStaffList', '4sStaffList'].includes(key),
shouldRefreshOptionsOnPopup: () => false,
isEmployeeListPickerSearchable: () => false
},
'../../utils/upgrade-icon-url.js': {
buildUpgradeIconUrl: value => value,
FORM_FIELD_ARROW_ICON: 'arrow'
}
}
vm.runInNewContext(source, {
Component(value) { definition = value },
require(id) {
if (!stubs[id]) throw new Error(`missing test stub: ${id}`)
return stubs[id]
},
console,
wx: {}
}, { filename: 'form-field-multipleSelect.js' })
return definition
}
function createField() {
return {
key: 'F2-107-7',
type: 'multipleSelect',
options: [
{ label: '已触客', value: '已触客' },
{
label: '已拉群',
value: '已拉群',
baseSceneCode: ['100101'],
brandId: ['3454']
},
{
label: '已交付引荐',
value: '已交付引荐',
baseSceneCode: ['100101'],
filterBrandId: ['3454'],
matchEffect: 'exclude'
}
]
}
}
function createInstance(definition, field, requestContext, value) {
const instance = {
data: {
...structuredClone(definition.data),
field,
requestContext,
value
},
properties: { groupKey: '', rowIndex: -1 },
setData(patch) { Object.assign(this.data, patch) }
}
Object.entries(definition.methods).forEach(([key, method]) => {
instance[key] = method.bind(instance)
})
return instance
}
describe('form-field-multipleSelect option visibility', () => {
it('IT-COMPONENT-OPTION-VIS-001: popup, display and submit share filtered static options', async () => {
const definition = loadComponentDefinition()
const value = ['已触客', '已拉群', '已交付引荐']
const instance = createInstance(
definition,
createField(),
{ sceneList: [{ baseSceneCode: '100101' }], brandIds: ['3454', '9999'] },
value
)
assert.deepEqual(
Array.from(instance.getStaticOptions(), option => option.value),
['已触客', '已拉群']
)
instance.updateDisplayLabel(instance.data.field, value)
assert.equal(instance.data.displayLabel, '已触客/已拉群')
assert.deepEqual(Array.from(instance.buildSubmitValues(value)), ['已触客', '已拉群'])
instance.data.requestContext = {
sceneList: [{ baseSceneCode: '100101' }],
brandIds: ['9999']
}
await instance.syncDisplayOptions()
assert.equal(instance.data.displayLabel, '已触客')
})
it('IT-COMPONENT-OPTION-VIS-002: object selections match static option ids canonically', () => {
const definition = loadComponentDefinition()
const field = createField()
field.options = field.options.map((option, index) => ({
...option,
id: `tag-${index + 1}`
}))
const value = [
{ id: 'tag-1', name: '已触客' },
{ id: 'tag-2', name: '已拉群' },
{ id: 'tag-3', name: '已交付引荐' },
{ id: 'obsolete', name: '旧标签' }
]
const instance = createInstance(
definition,
field,
{ sceneList: [{ baseSceneCode: '100101' }], brandIds: ['3454', '9999'] },
value
)
instance.updateDisplayLabel(field, value)
assert.equal(instance.data.displayLabel, '已触客/已拉群')
assert.deepEqual(Array.from(instance.data.pickerValueIds), ['已触客', '已拉群'])
assert.deepEqual(Array.from(instance.buildSubmitValues(value)), ['已触客', '已拉群'])
})
})
describe("form-field-multipleSelect optionsKey 'options'", () => {
it('IT-COMPONENT-OPTION-VIS-003: opens filtered template options without remote fetch', async () => {
let fetchCount = 0
const definition = loadComponentDefinition({
fetchFieldOptions: async () => {
fetchCount += 1
return { code: 0, data: [] }
}
})
const field = {
...createField(),
optionsKey: 'options'
}
const instance = createInstance(
definition,
field,
{ sceneList: [{ baseSceneCode: '100101' }], brandIds: ['3454', '9999'] },
[]
)
await instance.onTap()
assert.equal(fetchCount, 0)
assert.equal(instance.data.showPicker, true)
assert.equal(instance.data.pickerOptions.length, 2)
assert.deepEqual(
Array.from(instance.buildSubmitValues(['已触客', '已拉群'])),
['已触客', '已拉群']
)
instance.updateDisplayLabel(field, [{ id: '已触客', name: '已触客' }])
assert.equal(instance.data.displayLabel, '已触客')
})
})