option-visibility.test.mjs
7.09 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
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
filterMultipleSelectValueByVisibleOptions,
getVisibleFieldOptions
} from '../form-engine/option-visibility.js'
import { clearUnavailableOptionValues } from '../form-engine/values.js'
import { createFormEngine } from '../form-engine/core.js'
const LIGHT_SCENE = '100101'
const FIVE_REALM_BRAND = '3454'
const OTHER_BRAND = '9999'
function createCustomerTagField() {
const fiveRealmLightOption = label => ({
label,
value: label,
baseSceneCode: [LIGHT_SCENE],
brandId: [FIVE_REALM_BRAND]
})
return {
key: 'F2-107-7',
type: 'multipleSelect',
options: [
{ label: '已触客', value: '已触客' },
fiveRealmLightOption('已拉群'),
{
label: '已交付引荐',
value: '已交付引荐',
baseSceneCode: [LIGHT_SCENE],
filterBrandId: [FIVE_REALM_BRAND],
matchEffect: 'exclude'
},
fiveRealmLightOption('首保维保客户'),
fiveRealmLightOption('事故车客户')
]
}
}
function optionValues(field, pageContext) {
return getVisibleFieldOptions(field, pageContext).map(option => option.value)
}
describe('declarative option visibility', () => {
it('IT-OPTION-VIS-001: filters customer tags by scene and merchant brand matrix', () => {
const field = createCustomerTagField()
assert.deepEqual(
optionValues(field, {
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [FIVE_REALM_BRAND]
}),
['已触客', '已拉群', '已交付引荐', '首保维保客户', '事故车客户']
)
assert.deepEqual(
optionValues(field, {
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [OTHER_BRAND]
}),
['已触客']
)
assert.deepEqual(
optionValues(field, {
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [FIVE_REALM_BRAND, OTHER_BRAND]
}),
['已触客', '已拉群', '首保维保客户', '事故车客户']
)
assert.deepEqual(
optionValues(field, {
sceneList: [{ baseSceneCode: '110201' }],
brandIds: [OTHER_BRAND]
}),
['已触客', '已交付引荐']
)
})
it('IT-OPTION-VIS-002: missing context and malformed metadata fail closed', () => {
const field = createCustomerTagField()
field.options.push({ label: '非法规则', value: '非法规则', baseSceneCode: '100101' })
field.options.push({
label: '非法效果',
value: '非法效果',
baseSceneCode: [LIGHT_SCENE],
matchEffect: 'invalid'
})
field.options.push({ label: '空排除条件', value: '空排除条件', matchEffect: 'exclude' })
assert.deepEqual(optionValues(field, {}), ['已触客'])
})
it('IT-OPTION-VIS-003: options without declarative conditions remain visible', () => {
const field = {
type: 'multipleSelect',
options: [
{ label: 'A', value: 'A' },
{ label: 'B', value: 'B' }
]
}
assert.deepEqual(getVisibleFieldOptions(field, {}), field.options)
})
it('IT-OPTION-VIS-003b: top-level filterBrandId shows an option for any other brand', () => {
const field = {
type: 'multipleSelect',
options: [
{ label: '常规', value: '常规' },
{ label: '其他品牌', value: '其他品牌', filterBrandId: [FIVE_REALM_BRAND] }
]
}
assert.deepEqual(optionValues(field, { brandIds: [FIVE_REALM_BRAND] }), ['常规'])
assert.deepEqual(optionValues(field, { brandIds: [OTHER_BRAND] }), ['常规', '其他品牌'])
assert.deepEqual(
optionValues(field, { brandIds: [FIVE_REALM_BRAND, OTHER_BRAND] }),
['常规', '其他品牌']
)
assert.deepEqual(optionValues(field, {}), ['常规'])
})
it('IT-OPTION-VIS-004: removes invisible selections and preserves visible objects', () => {
const field = createCustomerTagField()
const objectSelection = { id: '已交付引荐', name: '已交付引荐' }
const selected = ['已触客', '已拉群', objectSelection]
assert.deepEqual(
filterMultipleSelectValueByVisibleOptions(field, selected, {
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [FIVE_REALM_BRAND, OTHER_BRAND]
}),
['已触客', '已拉群']
)
const warrantyValue = filterMultipleSelectValueByVisibleOptions(field, selected, {
sceneList: [{ baseSceneCode: '110201' }],
brandIds: [OTHER_BRAND]
})
assert.deepEqual(warrantyValue, ['已触客', objectSelection])
assert.equal(warrantyValue[1], objectSelection)
})
it('IT-OPTION-VIS-004b: matches object selections by option id and removes unknown values', () => {
const field = {
type: 'multipleSelect',
options: [
{ id: 'tag-contacted', label: '已触客', value: '已触客' },
{
id: 'tag-referral',
label: '已交付引荐',
value: '已交付引荐',
baseSceneCode: [LIGHT_SCENE],
filterBrandId: [FIVE_REALM_BRAND],
matchEffect: 'exclude'
}
]
}
const contacted = { id: 'tag-contacted', name: '已触客' }
const referral = { id: 'tag-referral', name: '已交付引荐' }
const unknown = { id: 'obsolete-tag', name: '旧标签' }
assert.deepEqual(
filterMultipleSelectValueByVisibleOptions(field, [contacted, referral, unknown], {
sceneList: [{ baseSceneCode: '110201' }],
brandIds: [OTHER_BRAND]
}),
[contacted, referral]
)
assert.deepEqual(
filterMultipleSelectValueByVisibleOptions(field, [contacted, referral, unknown], {
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [OTHER_BRAND]
}),
[contacted]
)
})
it('IT-OPTION-VIS-005: clears unavailable selections in top, static-group and subform values', () => {
const topField = { ...createCustomerTagField(), key: 'top-tags' }
const staticField = { ...createCustomerTagField(), key: 'static-tags' }
const subformField = { ...createCustomerTagField(), key: 'subform-tags' }
const list = [
topField,
{ key: 'static-group', type: 'group', children: [staticField] },
{
key: 'subform',
type: 'subform',
origin_children: [subformField],
children: [[subformField]]
}
]
const values = {
'top-tags': ['已触客', '已交付引荐'],
'static-tags': ['已触客', '已拉群'],
subform: [{ 'subform-tags': ['已触客', '已交付引荐'] }]
}
const filtered = clearUnavailableOptionValues(
list,
{
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [OTHER_BRAND]
},
values
)
assert.deepEqual(filtered, {
'top-tags': ['已触客'],
'static-tags': ['已触客'],
subform: [{ 'subform-tags': ['已触客'] }]
})
})
it('IT-OPTION-VIS-006: engine clears stale tag selections when page context changes', () => {
const field = createCustomerTagField()
const schema = {
list: [
{
key: 'F2-107',
type: 'subform',
origin_children: [field],
children: [[field]]
}
]
}
const engine = createFormEngine(schema)
engine.init(schema, {
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [FIVE_REALM_BRAND]
})
engine.addGroupRow('F2-107')
engine.setGroupFieldValue('F2-107', 0, 'F2-107-7', [
'已触客',
'已拉群',
'已交付引荐'
])
engine.setPageContext({
sceneList: [{ baseSceneCode: LIGHT_SCENE }],
brandIds: [FIVE_REALM_BRAND, OTHER_BRAND]
})
assert.deepEqual(engine.getValues()['F2-107'][0]['F2-107-7'], ['已触客', '已拉群'])
})
})