car-brand-series-flow.test.mjs
9.46 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
import { readFileSync } from 'node:fs'
const require = createRequire(import.meta.url)
const carBrandApi = require('../api/carBrandApi.js')
const {
resolveBrandNodeId,
resolveChildBrandId,
resolveCarSeriesId,
buildCarSeriesListParams
} = carBrandApi
const carBrandPickerBridge = require('../services/carBrandPickerBridge.js')
const selectionHelpers = require('../pages/carBrandSeries/carBrandSeries.helpers.js')
const {
getMockCarSeriesList,
getMockCarTypeList
} = require('../fixtures/mock-car-model-api.js')
const seriesPageRoot = new URL('../pages/carBrandSeries/', import.meta.url)
const seriesPageSource = readFileSync(new URL('carBrandSeries.js', seriesPageRoot), 'utf8')
const seriesPageWxml = readFileSync(new URL('carBrandSeries.wxml', seriesPageRoot), 'utf8')
const sharedPickerCss = readFileSync(new URL('../styles/car-brand-picker.wxss', import.meta.url), 'utf8')
function setDataPath(data, path, value) {
const parts = path.replace(/\[(\d+)\]/g, '.$1').split('.')
let cursor = data
for (let index = 0; index < parts.length - 1; index += 1) {
const part = parts[index]
if (cursor[part] == null) cursor[part] = /^\d+$/.test(parts[index + 1]) ? [] : {}
cursor = cursor[part]
}
cursor[parts.at(-1)] = value
}
function createPageInstance(definition) {
const page = { ...definition, data: structuredClone(definition.data) }
page.setData = function (patch, callback) {
Object.entries(patch).forEach(([path, value]) => setDataPath(this.data, path, value))
callback?.()
}
return page
}
function loadSeriesPage(apiOverrides = {}) {
let definition
const api = { ...carBrandApi, ...apiOverrides }
const requireForPage = specifier => {
if (specifier === '../../api/carBrandApi.js') return api
if (specifier === '../../services/carBrandPickerBridge.js') return carBrandPickerBridge
if (specifier === './carBrandSeries.helpers.js') return selectionHelpers
throw new Error(`Unexpected carBrandSeries dependency: ${specifier}`)
}
const wx = {
$util: {},
setNavigationBarTitle() {}
}
new Function('require', 'Page', 'wx', seriesPageSource)(
requireForPage,
config => {
definition = config
},
wx
)
return createPageInstance(definition)
}
function deferred() {
let resolve
let reject
const promise = new Promise((resolvePromise, rejectPromise) => {
resolve = resolvePromise
reject = rejectPromise
})
return { promise, resolve, reject }
}
function flushPromises() {
return new Promise(resolve => setImmediate(resolve))
}
function seedSeriesPage(page, selectedCarInfo = null) {
page.data.brandInfo = { vehicleBrandId: 1, vehicleBrandName: '鸿蒙智行' }
page.data.selectedCarInfo = selectedCarInfo
page.data.canConfirm = !!selectedCarInfo
page.data.list = [
{
id: 10,
name: 'AITO 问界',
childs: [
{
id: 100,
name: '问界M5',
shapeList: [],
shapeLoading: false,
shapeLoaded: false,
shapeError: ''
}
]
}
]
}
describe('car brand series flow', () => {
it('IT-CAR-001: buildCarSeriesListParams normalizes brandId and childBrandId', () => {
assert.equal(resolveBrandNodeId({ id: 3454 }), 3454)
assert.equal(resolveBrandNodeId({ brandId: 3067 }), 3067)
assert.equal(resolveChildBrandId({ brandId: 3454, id: 9999 }), 3454)
assert.equal(resolveChildBrandId({ id: 3503 }), 3503)
assert.equal(resolveCarSeriesId({ id: 'series-m7' }), 'series-m7')
assert.equal(resolveCarSeriesId({ carSeriesId: 'series-free' }), 'series-free')
assert.deepEqual(buildCarSeriesListParams(3454, { brandId: 3454, id: 3454 }), {
brandId: 3454,
childBrandId: 3454
})
})
it('IT-CAR-002: carBrandPickerBridge stores and reads seriesNav on pending', () => {
carBrandPickerBridge.clearPending()
const nav = carBrandPickerBridge.getSeriesNavParams(
{ vehicleBrandId: 3067, vehicleBrandName: '岚图' },
[{ id: 3067, brandId: 3067, name: '岚图汽车' }],
'tips'
)
carBrandPickerBridge.goCarBrandSeries(nav)
assert.deepEqual(carBrandPickerBridge.getSeriesNav(), nav)
const parsed = carBrandPickerBridge.parseSeriesNavFromQuery({
brandId: '3067',
brandName: encodeURIComponent('岚图'),
list: encodeURIComponent(JSON.stringify([{ id: 3067, name: '岚图汽车' }])),
cartips: encodeURIComponent('hint')
})
assert.equal(parsed.brandInfo.vehicleBrandId, '3067')
assert.equal(parsed.brandInfo.vehicleBrandName, '岚图')
assert.equal(parsed.list.length, 1)
assert.equal(parsed.cartips, 'hint')
carBrandPickerBridge.clearPending()
assert.equal(carBrandPickerBridge.getSeriesNav(), null)
})
it('IT-CAR-003: mock L3 to L4 chain returns selectable shapes', () => {
const seriesList = getMockCarSeriesList({ brandId: 3454, childBrandId: 3454 })
assert.ok(seriesList.length >= 1)
const types = getMockCarTypeList(seriesList[0].id)
assert.ok(types.length >= 1)
assert.ok(types[0].id)
assert.ok(types[0].name)
})
it('IT-CAR-004: series selection and expansion use independent click zones', () => {
assert.match(seriesPageWxml, /catchtap="selectSeries"/)
assert.match(seriesPageWxml, /catchtap="toggleSeries"/)
assert.match(seriesPageWxml, /catchtap="selectShape"/)
assert.match(seriesPageWxml, /bindtap="confirmSelection"/)
assert.doesNotMatch(seriesPageWxml, /car-type-box/)
assert.doesNotMatch(sharedPickerCss, /\.car-type-box|\.car-view/)
})
it('IT-CAR-005: loading and successful empty shape results are requested once', async () => {
const response = deferred()
let requests = 0
const page = loadSeriesPage({
getCarTypeList() {
requests += 1
return response.promise
}
})
seedSeriesPage(page)
page.ensureShapeList({ sectionIndex: 0, seriesIndex: 0 })
page.ensureShapeList({ sectionIndex: 0, seriesIndex: 0 })
assert.equal(requests, 1)
assert.equal(page.data.list[0].childs[0].shapeLoading, true)
response.resolve({ code: 0, data: [] })
await flushPromises()
page.ensureShapeList({ sectionIndex: 0, seriesIndex: 0 })
assert.equal(requests, 1)
assert.equal(page.data.list[0].childs[0].shapeLoaded, true)
assert.deepEqual(page.data.list[0].childs[0].shapeList, [])
})
it('IT-CAR-006: a failed shape request stores an error and retry requests again', async () => {
let requests = 0
const page = loadSeriesPage({
getCarTypeList() {
requests += 1
return requests === 1
? Promise.reject(new Error('offline'))
: Promise.resolve({ code: 0, data: [{ id: 1, name: 'Max' }] })
}
})
seedSeriesPage(page)
page.ensureShapeList({ sectionIndex: 0, seriesIndex: 0 })
await flushPromises()
assert.equal(page.data.list[0].childs[0].shapeError, '获取车型失败')
assert.equal(page.data.list[0].childs[0].shapeLoaded, false)
page.retryShapeList({ currentTarget: { dataset: { sectionIndex: 0, seriesIndex: 0 } } })
await flushPromises()
assert.equal(requests, 2)
assert.equal(page.data.list[0].childs[0].shapeLoaded, true)
assert.equal(page.data.list[0].childs[0].shapeError, '')
})
it('IT-CAR-007: a missing restored shape preserves the old selection', async () => {
const selectedCarInfo = {
vehicleBrandId: 1,
vehicleSubBrandId: 10,
vehicleSeriesId: 100,
vehicleShapeId: 999,
displayText: '旧车型'
}
const page = loadSeriesPage({
getCarTypeList: () => Promise.resolve({ code: 0, data: [{ id: 1, name: 'Max' }] })
})
seedSeriesPage(page, selectedCarInfo)
page.ensureShapeList({ sectionIndex: 0, seriesIndex: 0 })
await flushPromises()
assert.equal(page.data.selectedCarInfo, selectedCarInfo)
assert.equal(page.data.selectedCarInfo.vehicleShapeId, 999)
assert.equal(page.data.canConfirm, true)
})
it('IT-CAR-008: a delayed restored series cannot override a manual expansion', async () => {
const selectedSection = deferred()
const otherSection = deferred()
const page = loadSeriesPage({
getCarSeriesList({ childBrandId }) {
return childBrandId === 10 ? selectedSection.promise : otherSection.promise
},
getCarTypeList: () => Promise.resolve({ code: 0, data: [] })
})
page.applySeriesNav({
brandInfo: { vehicleBrandId: 1, vehicleBrandName: '鸿蒙智行' },
currentValue: {
vehicleBrandId: 1,
vehicleSubBrandId: 10,
vehicleSeriesId: 100,
vehicleShapeId: 999
},
list: [
{ id: 10, name: 'AITO 问界' },
{ id: 20, name: 'LUXEED 智界' }
]
})
otherSection.resolve({ code: 0, data: [{ id: 200, name: '智界S7' }] })
await flushPromises()
page.toggleSeries({ currentTarget: { dataset: { sectionIndex: 1, seriesIndex: 0 } } })
assert.equal(page.data.expandedSeriesKey, '1-0')
selectedSection.resolve({ code: 0, data: [{ id: 100, name: '问界M5' }] })
await flushPromises()
assert.equal(page.data.expandedSeriesKey, '1-0')
})
it('IT-CAR-009: applySeriesNav clears cross-brand current values and restores same-brand L3 values', () => {
const page = loadSeriesPage()
page.applySeriesNav({
brandInfo: { vehicleBrandId: 2, vehicleBrandName: '新品牌' },
currentValue: {
vehicleBrandId: 1,
vehicleSeriesId: 100,
vehicleShapeId: 999
},
list: []
})
assert.equal(page.data.selectedCarInfo, null)
assert.equal(page.data.canConfirm, false)
assert.equal(page.data.expandedSeriesKey, '')
assert.equal(page._canAutoRestoreSelection, false)
page.applySeriesNav({
brandInfo: { vehicleBrandId: 0, vehicleBrandName: '零号品牌' },
currentValue: { vehicleBrandId: '0', vehicleSeriesId: 100 },
list: []
})
assert.equal(page.data.selectedCarInfo.vehicleSeriesId, 100)
assert.equal(page.data.canConfirm, true)
})
})