store-app-json-echo.test.mjs
7.61 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { getSubformRowTemplate } from '../form-engine/field-types.js'
import {
mergeAppJsonIntoSchema,
serializeFormListForAppJsonStorage
} from '../form-engine/app-subform-json.js'
import {
mergeValuesIntoSchema,
parseJsonContentDetailValues
} from '../form-engine/submit-adapter.js'
import { createFormEngine } from '../form-engine/core.js'
import { buildDailyReportSaveRequest } from '../form-engine/save-request.js'
import { DAILY_REPORT_TEMPLATE_TYPE } from '../config/dailyReportConstants.js'
import { isRowFieldsList } from '../form-engine/group-row-adapter.js'
function selectValueMapId(value) {
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
return value.id ?? value.value
}
return value
}
import { loadSchemaFixture } from './schemaFixture.mjs'
const originChildren = [
{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
]
const schemaRoot = {
list: [
{
key: 'F2-9',
label: '问界鸿蒙售前数据',
type: 'subform',
children: []
}
]
}
describe('store App jsonContent subform echo', () => {
it('IT-STORE-ECHO-001: getSubformRowTemplate falls back to origin_children', () => {
const template = getSubformRowTemplate({
key: 'F2-9',
type: 'subform',
children: [],
origin_children: originChildren
})
assert.equal(template.length, 2)
assert.equal(template[0].key, 'F2-9-1')
})
it('IT-STORE-ECHO-002: mergeAppJsonIntoSchema syncs origin_children and children template', () => {
const jsonContent = JSON.stringify([
{
key: 'F2-9',
type: 'subform',
children: [],
origin_children: originChildren
}
])
const merged = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
const subform = merged.list.find(f => f.key === 'F2-9')
assert.ok(Array.isArray(subform.origin_children))
assert.equal(subform.origin_children.length, 2)
assert.ok(Array.isArray(subform.children[0]))
assert.equal(subform.children[0].length, 2)
})
it('IT-STORE-ECHO-003: App dual-key row in jsonContent merges into schema value', () => {
const jsonContent = JSON.stringify([
{
key: 'F2-9',
type: 'subform',
origin_children: originChildren,
children: [
[
{
key: 'F2-9-1',
label: '交付专员',
type: 'select',
optionsKey: '4sStaffList',
value: '张三',
'F2-9-1': 'uuid-staff-001'
},
{
key: 'F2-9-2',
label: '今日交车',
type: 'numberInput',
value: 5
}
]
]
}
])
const merged = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
const subform = merged.list.find(f => f.key === 'F2-9')
assert.ok(isRowFieldsList(subform.value))
assert.equal(subform.value[0].find(f => f.key === 'F2-9-1').value, '张三')
assert.equal(subform.value[0].find(f => f.key === 'F2-9-1')['F2-9-1'], 'uuid-staff-001')
})
it('IT-STORE-ECHO-004: parseJsonContentDetailValues reads App children rows', () => {
const jsonContent = JSON.stringify([
{
key: 'F2-9',
type: 'subform',
origin_children: originChildren,
children: [
[
{
key: 'F2-9-1',
type: 'select',
optionsKey: '4sStaffList',
value: '交付专员A',
'F2-9-1': 'uuid-staff-002'
},
{ key: 'F2-9-2', type: 'numberInput', value: 3 }
]
]
}
])
let schema = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
const values = parseJsonContentDetailValues(jsonContent, schemaRoot.list)
schema = mergeValuesIntoSchema(schema, values)
const engine = createFormEngine()
engine.init(schema, { baseSceneCode: 110201, brandIds: [3454] })
const rows = engine.getGroupRows('F2-9')
assert.equal(rows.length, 1)
assert.equal(selectValueMapId(rows[0]['F2-9-1']), 'uuid-staff-002')
assert.equal(rows[0]['F2-9-2'], 3)
})
it('IT-STORE-ECHO-005: legacy subform.value in jsonContent merges when children empty', () => {
const jsonContent = JSON.stringify([
{
key: 'F2-9',
type: 'subform',
children: [],
origin_children: originChildren,
value: [{ 'F2-9-1': '4s-001', 'F2-9-2': 2 }]
}
])
const values = parseJsonContentDetailValues(jsonContent, schemaRoot.list)
assert.equal(values['F2-9'].length, 1)
assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), '4s-001')
assert.equal(values['F2-9'][0]['F2-9-2'], 2)
})
it('IT-STORE-ECHO-006: normalizeJsonContentList accepts { list: [] } wrapper', () => {
const wrapped = {
list: [
{
key: 'F2-9',
type: 'subform',
children: [
[
{ key: 'F2-9-1', type: 'select', value: '4s-001' },
{ key: 'F2-9-2', type: 'numberInput', value: 4 }
]
]
}
]
}
const values = parseJsonContentDetailValues(JSON.stringify(wrapped), schemaRoot.list)
assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), '4s-001')
assert.equal(values['F2-9'][0]['F2-9-2'], 4)
})
it('IT-STORE-ECHO-007: template-only jsonContent (empty children, no lists) yields no subform rows', () => {
const jsonContent = JSON.stringify([
{
key: 'F2-9',
type: 'subform',
children: [],
origin_children: originChildren
}
])
const values = parseJsonContentDetailValues(jsonContent, schemaRoot.list)
assert.equal(values['F2-9'], undefined)
let schema = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
schema = mergeValuesIntoSchema(schema, values)
const engine = createFormEngine()
engine.init(schema, { baseSceneCode: 110201, brandIds: [3454] })
assert.equal(engine.getGroupRows('F2-9').length, 0)
})
it('IT-STORE-ECHO-008: save jsonContent includes populated subform childrenValue when form filled', () => {
const storeSchema = loadSchemaFixture('store')
const engine = createFormEngine(storeSchema)
engine.setPageContext({
baseSceneCode: 110201,
brandIds: [3454, 3067],
merchantId: '1001',
sceneList: [{ baseSceneCode: 110201 }]
})
engine.setAppGroupFieldValue('F2-9', 0, 'F2-9-1', {
id: '4s-001',
displayLabel: '交付专员A'
})
engine.setGroupFieldValue('F2-9', 0, 'F2-9-2', 5)
const { request } = buildDailyReportSaveRequest({
reportDate: '2026-06-02',
userContext: { merchantId: 1001, staffUuid: 's1' },
pageContext: { merchantId: 1001, brandIds: [3454] },
engine,
reportTemplateId: 10002,
templateType: DAILY_REPORT_TEMPLATE_TYPE.store
})
const parsed = JSON.parse(request.jsonContent)
const f29 = parsed.find(item => item.key === 'F2-9')
assert.ok(f29, 'F2-9 should be in save jsonContent')
assert.ok(
Array.isArray(f29.childrenValue) && f29.childrenValue.length > 0,
'save should write subform rows'
)
assert.equal(f29.childrenValue[0]['F2-9']['F2-9-2'], 5)
})
it('IT-STORE-ECHO-009: childrenValue jsonContent echoes via parseJsonContentDetailValues', () => {
const originChildren = [
{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
]
const jsonContent = JSON.stringify(
serializeFormListForAppJsonStorage([
{
key: 'F2-9',
type: 'subform',
origin_children: originChildren,
children: [
[
{
key: 'F2-9-1',
type: 'select',
optionsKey: '4sStaffList',
value: '交付专员A',
'F2-9-1': { id: '4s-001', name: '交付专员A' }
},
{ key: 'F2-9-2', type: 'numberInput', value: 5 }
]
]
}
])
)
const values = parseJsonContentDetailValues(jsonContent, [])
assert.equal(values['F2-9'][0]['F2-9-2'], 5)
assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), '4s-001')
})
})