storeDailyReportDetail.test.mjs
5.82 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
parseJsonContentDetailValues,
mergeValuesIntoSchema
} = await import('../form-engine/submit-adapter.js')
const { createFormEngine } = await import('../form-engine/core.js')
const { isReportSubmitted, normalizeReportStatus } = require('../utils/report-status.js')
const { DEFAULT_REPORT_TEMPLATE_ID } = require('../api/mockStoreDynamicDetail.js')
const { loadSchemaFixture } = await import('./schemaFixture.mjs')
function selectValueMapId(value) {
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
return value.id ?? value.value
}
return value
}
function normalizeDetailResponse(detail = {}, reportTemplateId) {
const recordId = detail.dailyReportRecordId ?? detail.id ?? null
const jsonContent = detail.jsonContent || ''
return {
reportTemplateId: detail.reportTemplateId || reportTemplateId || DEFAULT_REPORT_TEMPLATE_ID,
dailyReportRecordId: recordId,
baseSceneCode: detail.baseSceneCode,
reportStatus: normalizeReportStatus(detail.reportStatus),
values: parseJsonContentDetailValues(jsonContent, []),
jsonContent
}
}
describe('storeDailyReport detail response', () => {
it('IT-STORE-DETAIL-001: normalizeDetailResponse maps reportStatus', () => {
const data = normalizeDetailResponse(
{
id: 99001,
reportTemplateId: 10002,
reportStatus: 1,
jsonContent: JSON.stringify([{ key: 'F2-2', type: 'numberInput', value: 3 }])
},
10002
)
assert.equal(data.dailyReportRecordId, 99001)
assert.equal(data.reportStatus, '1')
assert.equal(data.values['F2-2'], 3)
})
it('IT-STORE-DETAIL-002: empty detail defaults reportStatus to 0', () => {
const data = normalizeDetailResponse({}, 10002)
assert.equal(data.reportStatus, '0')
assert.equal(data.dailyReportRecordId, null)
assert.equal(data.reportTemplateId, DEFAULT_REPORT_TEMPLATE_ID)
})
it('IT-STORE-DETAIL-004: F2-1 echoes from jsonContent through schema merge and engine init', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailResponse({
jsonContent: JSON.stringify([
{ key: 'F2-2', type: 'numberInput', value: 3 },
{ key: 'F2-1', type: 'multipleSelect', value: ['staff-001', 'staff-002'] }
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.deepEqual(viewState.values['F2-1'], ['staff-001', 'staff-002'])
assert.equal(viewState.values['F2-2'], 3)
})
it('IT-STORE-DETAIL-006: F2-1 dual key jsonContent echoes id and name pairs', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailResponse({
jsonContent: JSON.stringify([
{ key: 'F2-2', type: 'numberInput', value: 3 },
{
key: 'F2-1',
type: 'multipleSelect',
optionsKey: 'storeStaffList',
value: '张三/李四',
'F2-1': ['staff-001', 'staff-002']
}
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.deepEqual(viewState.values['F2-1'], [
{ id: 'staff-001', name: '张三' },
{ id: 'staff-002', name: '李四' }
])
})
it('IT-STORE-SUB-001: F2-9 dual rows echo via valueMap jsonContent', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailResponse({
jsonContent: JSON.stringify([
{
key: 'F2-9',
type: 'subform',
value: [
{ 'F2-9-1': '4s-001', 'F2-9-2': 1 },
{ 'F2-9-1': '4s-002', 'F2-9-2': 2 }
]
}
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.equal(viewState.values['F2-9'].length, 2)
assert.equal(viewState.groupRowsCache['F2-9'].length, 2)
assert.equal(viewState.values['F2-9'][1]['F2-9-1'], '4s-002')
})
it('IT-STORE-DETAIL-005: F2-9-1 delivery specialist echoes from jsonContent App children', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailResponse({
jsonContent: JSON.stringify([
{
key: 'F2-9',
type: 'subform',
children: [
[
{
key: 'F2-9-1',
type: 'select',
optionsKey: '4sStaffList',
value: '交付专员B',
'F2-9-1': '4s-002'
},
{ key: 'F2-9-2', type: 'numberInput', value: 1 }
]
]
}
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.equal(selectValueMapId(viewState.values['F2-9'][0]['F2-9-1']), '4s-002')
})
})
describe('report-status utils', () => {
it('IT-STORE-DETAIL-003: isReportSubmitted handles string and number', () => {
assert.equal(isReportSubmitted('1'), true)
assert.equal(isReportSubmitted(1), true)
assert.equal(isReportSubmitted('0'), false)
assert.equal(isReportSubmitted(null), false)
assert.equal(normalizeReportStatus(undefined), '0')
})
})