storeDailyFormLoader.test.mjs
4.92 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const { loadStoreDailyForm } = require('../services/storeDailyFormLoader.js')
const { loadSchemaFixture } = await import('./schemaFixture.mjs')
const schemaTemplate = loadSchemaFixture('store')
function createDeps(overrides = {}) {
const pageContext = {
baseSceneCode: 110201,
brandIds: [3454],
sceneList: [{ baseSceneCode: 110201 }],
merchantId: 'm1',
merchantName: '测试门店'
}
return {
fetchMerchantBrandIds: async () => ({ code: 0, data: [3454] }),
fetchMerchantScene: async () => ({
code: 0,
sceneList: [{ baseSceneCode: 110201 }],
baseSceneCode: 110201
}),
fetchReportDetail: async () => ({
code: 0,
data: {
reportTemplateId: 10002,
dailyReportRecordId: null,
values: {}
}
}),
fetchDailyReportCarBrandList: async () => ({ code: 0, data: [] }),
fetchPageContext: async () => ({ code: 0, data: pageContext }),
fetchSchema: async () => ({
code: 0,
data: schemaTemplate
}),
...overrides
}
}
describe('storeDailyFormLoader', () => {
it('IT-STORE-LOADER-001: assembles pageContext then schema in order', async () => {
const calls = []
const deps = createDeps({
fetchMerchantBrandIds: async () => {
calls.push('brand')
return { code: 0, data: [3454] }
},
fetchMerchantScene: async () => {
calls.push('scene')
return {
code: 0,
sceneList: [{ baseSceneCode: 110201 }],
baseSceneCode: 110201
}
},
fetchReportDetail: async () => {
calls.push('detail')
return {
code: 0,
data: { reportTemplateId: 10002, dailyReportRecordId: null, values: {} }
}
},
fetchPageContext: async () => {
calls.push('pageContext')
return {
code: 0,
data: {
baseSceneCode: 110201,
brandIds: [3454],
sceneList: [{ baseSceneCode: 110201 }]
}
}
},
fetchSchema: async ({ baseSceneCode }) => {
calls.push(`schema:${baseSceneCode}`)
return { code: 0, data: schemaTemplate }
}
})
const res = await loadStoreDailyForm(
{
userContext: { merchantId: 'm1', staffUuid: 's1' },
reportDate: '2026-05-26',
timeBean: { reportStartDate: '2026-05-26', reportEndDate: '2026-05-26' },
reportTemplateId: 10002
},
deps
)
assert.equal(res.code, 0)
assert.ok(res.data.pageContext)
assert.ok(res.data.schema)
assert.ok(calls.indexOf('pageContext') > calls.indexOf('detail'))
assert.ok(calls.includes('schema:110201'))
assert.equal(res.data.weeklyList, undefined)
})
it('IT-STORE-LOADER-002: returns error when detail fails', async () => {
const deps = createDeps({
fetchReportDetail: async () => ({ code: -1, message: 'detail failed' })
})
const res = await loadStoreDailyForm(
{
userContext: { merchantId: 'm1', staffUuid: 's1' },
reportDate: '2026-05-26',
timeBean: {}
},
deps
)
assert.equal(res.code, -1)
assert.equal(res.message, 'detail failed')
})
it('IT-STORE-LOADER-003: empty sceneList returns 暂无数据', async () => {
const deps = createDeps({
fetchMerchantScene: async () => ({
code: 0,
sceneList: [],
baseSceneCode: null
})
})
const res = await loadStoreDailyForm(
{
userContext: { merchantId: 'm1', staffUuid: 's1' },
reportDate: '2026-05-26',
timeBean: {}
},
deps
)
assert.equal(res.code, -1)
assert.equal(res.message, '暂无数据')
assert.equal(res.data, null)
})
it('IT-STORE-LOADER-004: passes merchant.info brand tree separately from save carBrandList', async () => {
const merchantCarBrandList = [
{ id: 2161, name: '宝马', childs: [{ id: 2161, name: '宝马' }] }
]
const saveCarBrandList = [
{ firstBrandId: 2161, firstBrandName: '宝马', brandId: 2161, brandName: '宝马' }
]
let capturedPageContextOptions = null
const deps = createDeps({
fetchMerchantBrandIds: async () => ({
code: 0,
data: [3454],
merchantCarBrandList
}),
fetchDailyReportCarBrandList: async () => ({ code: 0, data: saveCarBrandList }),
fetchPageContext: async options => {
capturedPageContextOptions = options
return {
code: 0,
data: {
baseSceneCode: 110201,
brandIds: [2161],
merchantCarBrandList,
carBrandList: saveCarBrandList,
sceneList: [{ baseSceneCode: 110201 }]
}
}
}
})
const res = await loadStoreDailyForm(
{
userContext: { merchantId: 'm1', staffUuid: 's1' },
reportDate: '2026-08-13',
reportTemplateId: 10002
},
deps
)
assert.equal(res.code, 0)
assert.deepEqual(capturedPageContextOptions.brandIds, [2161])
assert.deepEqual(capturedPageContextOptions.merchantCarBrandList, merchantCarBrandList)
assert.deepEqual(capturedPageContextOptions.carBrandList, saveCarBrandList)
assert.notStrictEqual(
capturedPageContextOptions.merchantCarBrandList,
capturedPageContextOptions.carBrandList
)
})
})