group-row-snapshot.test.mjs
5.21 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
getRowFieldsSnapshot,
buildGroupRowViewMaps
} = await import('../form-engine/group-row-snapshot.js')
const { loadSchemaFixture } = await import('./schemaFixture.mjs')
const negotiationSchema = loadSchemaFixture('negotiation')
const storeSchema = loadSchemaFixture('store')
describe('group-row-snapshot', () => {
it('IT-GROUP-SNAPSHOT-001: snapshot isolates row field values', async () => {
const { cloneRowTemplate } = await import(
'../form-engine/group-row-adapter.js'
)
const { setRowFieldInCache } = await import(
'../form-engine/group-rows-cache.js'
)
const group = negotiationSchema.list.find(item => item.key === 'F3-1')
const template = cloneRowTemplate(group)
let cache = {
'F3-1': [cloneRowTemplate(group), cloneRowTemplate(group)]
}
cache = setRowFieldInCache(cache, 'F3-1', 0, 'F3-1-4', '客户A', template)
cache = setRowFieldInCache(cache, 'F3-1', 1, 'F3-1-4', '客户B', template)
const row0 = getRowFieldsSnapshot(cache, 'F3-1', 0)
const row1 = getRowFieldsSnapshot(cache, 'F3-1', 1)
assert.equal(row0.find(f => f.key === 'F3-1-4')?.value, '客户A')
assert.equal(row1.find(f => f.key === 'F3-1-4')?.value, '客户B')
row0.find(f => f.key === 'F3-1-4').value = '篡改'
const row1Again = getRowFieldsSnapshot(cache, 'F3-1', 1)
assert.equal(row1Again.find(f => f.key === 'F3-1-4')?.value, '客户B')
})
it('IT-GROUP-SNAPSHOT-002: buildGroupRowViewMaps returns totals and visible map', () => {
const cache = { 'F3-1': [[], []] }
const values = { 'F3-1': [{}, {}] }
const { groupRowFieldsMap, groupRowTotalMap, groupRowVisibleMap } =
buildGroupRowViewMaps([{ key: 'F3-1' }], cache, { 'F3-1': 0 }, values)
assert.equal(groupRowTotalMap['F3-1'], 2)
assert.ok(Array.isArray(groupRowFieldsMap['F3-1']))
assert.ok(Array.isArray(groupRowVisibleMap['F3-1']))
})
it('IT-GROUP-SNAPSHOT-004: visible map attaches _wxRowKey', () => {
const group = negotiationSchema.list.find(item => item.key === 'F3-1')
const cache = { 'F3-1': [[{ key: 'F3-1-4', type: 'input', value: 'x' }]] }
const { groupRowVisibleMap } = buildGroupRowViewMaps(
[group],
cache,
{ 'F3-1': 0 },
{ 'F3-1': [{}] }
)
const row = groupRowVisibleMap['F3-1']
assert.ok(row.length >= 1)
assert.match(row[0]._wxRowKey, /^F3-1_0_/)
})
it('IT-GROUP-ISOLATE-001: F3-1 and F3-2 visible maps do not share customer name', async () => {
const { cloneRowTemplate } = await import(
'../form-engine/group-row-adapter.js'
)
const { setRowFieldInCache } = await import(
'../form-engine/group-rows-cache.js'
)
const group1 = negotiationSchema.list.find(item => item.key === 'F3-1')
const group2 = negotiationSchema.list.find(item => item.key === 'F3-2')
const template1 = cloneRowTemplate(group1)
const template2 = cloneRowTemplate(group2)
let cache = {
'F3-1': [cloneRowTemplate(group1)],
'F3-2': [cloneRowTemplate(group2)]
}
cache = setRowFieldInCache(cache, 'F3-1', 0, 'F3-1-4', '客户甲', template1)
cache = setRowFieldInCache(cache, 'F3-2', 0, 'F3-2-4', '客户乙', template2)
const { groupRowVisibleMap } = buildGroupRowViewMaps(
[group1, group2],
cache,
{ 'F3-1': 0, 'F3-2': 0 },
{
'F3-1': [{ 'F3-1-4': '客户甲' }],
'F3-2': [{ 'F3-2-4': '客户乙' }]
}
)
const name1 = groupRowVisibleMap['F3-1'].find(f => f.key === 'F3-1-4')?.value
const name2 = groupRowVisibleMap['F3-2'].find(f => f.key === 'F3-2-4')?.value
assert.equal(name1, '客户甲')
assert.equal(name2, '客户乙')
})
it('IT-GROUP-SNAPSHOT-005: scene spec hides subform columns in visible map', async () => {
const { cloneRowTemplate } = await import('../form-engine/group-row-adapter.js')
const group = storeSchema.list.find(item => item.key === 'F2-11')
const template = cloneRowTemplate(group)
const cache = { 'F2-11': [cloneRowTemplate(group)] }
const warrantyCtx = {
baseSceneCode: 110201,
brandIds: [3067, 2161],
sceneList: [{ baseSceneCode: 110201 }]
}
const { groupRowVisibleMap } = buildGroupRowViewMaps(
[group],
cache,
{ 'F2-11': 0 },
{ 'F2-11': [{}] },
{
ctx: warrantyCtx,
values: { 'F2-11': [{}] },
list: storeSchema.list
}
)
const keys = (groupRowVisibleMap['F2-11'] || []).map(field => field.key)
assert.ok(keys.includes('F2-11-3'))
assert.ok(!keys.includes('F2-11-8'))
})
it('IT-GROUP-SNAPSHOT-003: setRowFieldInCache seeds row from template not empty array', async () => {
const { cloneRowTemplate } = await import(
'../form-engine/group-row-adapter.js'
)
const { setRowFieldInCache, syncValuesFromGroupCache } = await import(
'../form-engine/group-rows-cache.js'
)
const group = negotiationSchema.list.find(item => item.key === 'F3-1')
const template = cloneRowTemplate(group)
const cache = setRowFieldInCache(
{ 'F3-1': [] },
'F3-1',
0,
'F3-1-4',
'测试',
template
)
const row = cache['F3-1'][0] || []
assert.ok(row.length > 0)
assert.equal(row.find(f => f.key === 'F3-1-4')?.value, '测试')
const values = syncValuesFromGroupCache({}, 'F3-1', cache['F3-1'])
assert.equal(values['F3-1'][0]['F3-1-4'], '测试')
})
})