app-children-value-json.test.mjs
3.39 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
childrenToChildrenValue,
childrenValueToRowFieldsList,
serializeFormListForAppJsonStorage,
buildAppNormalizedListsFromJsonContent
} from '../form-engine/app-subform-json.js'
import { parseJsonContentDetailValues } from '../form-engine/submit-adapter.js'
function selectValueMapId(value) {
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
return value.id ?? value.value
}
return value
}
const originChildren = [
{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
]
const sectionWithChildren = {
key: 'F2-9',
label: '子表',
type: 'subform',
origin_children: originChildren,
children: [
[
{
key: 'F2-9-1',
label: '交付专员',
type: 'select',
optionsKey: '4sStaffList',
value: '张三',
'F2-9-1': { id: 'uuid-1', name: '张三', phone: '13800138000' }
},
{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 5 }
]
]
}
describe('app childrenValue jsonContent codec', () => {
it('IT-APP-CV-001: childrenValue round-trip with dual key and scalar', () => {
const childrenValue = childrenToChildrenValue(sectionWithChildren)
assert.deepEqual(childrenValue[0]['F2-9']['F2-9-1'], {
text: '张三',
value: 'uuid-1'
})
assert.equal(childrenValue[0]['F2-9']['F2-9-2'], 5)
const rows = childrenValueToRowFieldsList({
key: 'F2-9',
origin_children: originChildren,
childrenValue
})
assert.equal(rows.length, 1)
const staff = rows[0].find(f => f.key === 'F2-9-1')
assert.equal(staff.value, '张三')
assert.equal(staff['F2-9-1'], 'uuid-1')
assert.equal(rows[0].find(f => f.key === 'F2-9-2').value, 5)
})
it('IT-APP-CV-002: lists from formList before serialize; childrenValue stores text + id', () => {
const formList = [sectionWithChildren]
const fromChildren = buildAppNormalizedListsFromJsonContent(JSON.stringify(formList))
const fromPreSerialize = buildAppNormalizedListsFromJsonContent(
JSON.stringify(JSON.parse(JSON.stringify(formList)))
)
assert.deepEqual(fromPreSerialize.fieldDataList, fromChildren.fieldDataList)
assert.deepEqual(fromPreSerialize.subformDataList, fromChildren.subformDataList)
const serialized = serializeFormListForAppJsonStorage(formList)
assert.deepEqual(serialized[0].childrenValue[0]['F2-9']['F2-9-1'], {
text: '张三',
value: 'uuid-1'
})
assert.ok(!serialized[0].children)
assert.ok(Array.isArray(serialized[0].childrenValue))
})
it('IT-APP-CV-004: serializeFormListForAppJsonStorage top-level remote select id in dual key', () => {
const serialized = serializeFormListForAppJsonStorage([
{
key: 'F2-9-1',
type: 'select',
optionsKey: '4sStaffList',
value: '张三',
'F2-9-1': { id: 'uuid-1', name: '张三', phone: '13800138000' }
}
])
assert.equal(serialized[0].value, '张三')
assert.equal(serialized[0]['F2-9-1'], 'uuid-1')
})
it('IT-APP-CV-003: parseJsonContentDetailValues reads childrenValue id from jsonContent', () => {
const serialized = serializeFormListForAppJsonStorage([sectionWithChildren])
const fromChildrenValue = parseJsonContentDetailValues(JSON.stringify(serialized), [])
assert.equal(fromChildrenValue['F2-9'][0]['F2-9-2'], 5)
assert.equal(selectValueMapId(fromChildrenValue['F2-9'][0]['F2-9-1']), 'uuid-1')
})
})