car-brand-series-selection.test.mjs
2.89 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
buildSeriesSelection,
buildShapeSelection,
resolveSelectionForBrand,
isSeriesSelected,
isShapeSelected
} = require('../pages/carBrandSeries/carBrandSeries.helpers.js')
const brandInfo = {
vehicleBrandId: 1,
vehicleBrandName: '鸿蒙智行'
}
const section = { id: 2, name: 'AITO 问界' }
const series = { id: 3, name: '问界M5' }
const shape = { id: 4, name: '2025款 增程 Ultra 后驱版' }
describe('car brand series selection helpers', () => {
it('builds a third-level series selection without a stale shape', () => {
const selected = buildSeriesSelection(brandInfo, section, series)
assert.equal(selected.vehicleBrandId, 1)
assert.equal(selected.vehicleSubBrandId, 2)
assert.equal(selected.vehicleSeriesId, 3)
assert.equal(selected.vehicleShapeId, undefined)
assert.match(selected.displayText, /问界M5/)
})
it('builds a four-level shape selection with all parent identifiers', () => {
const selected = buildShapeSelection(brandInfo, section, series, shape)
assert.equal(selected.vehicleBrandId, 1)
assert.equal(selected.vehicleSubBrandId, 2)
assert.equal(selected.vehicleSeriesId, 3)
assert.equal(selected.vehicleShapeId, 4)
assert.match(selected.displayText, /2025款 增程 Ultra 后驱版/)
})
it('does not highlight a parent series when its child shape is selected', () => {
const selected = buildShapeSelection(brandInfo, section, series, shape)
assert.equal(isSeriesSelected(selected, 3), false)
assert.equal(isShapeSelected(selected, 4), true)
})
it('matches identifiers across number and string API representations', () => {
const seriesSelected = buildSeriesSelection(brandInfo, section, series)
const shapeSelected = buildShapeSelection(brandInfo, section, series, shape)
assert.equal(isSeriesSelected(seriesSelected, '3'), true)
assert.equal(isSeriesSelected(seriesSelected, '4'), false)
assert.equal(isShapeSelected(shapeSelected, '4'), true)
assert.equal(isShapeSelected(shapeSelected, '3'), false)
})
it('restores only a normalized selection for the same main brand, including brand id zero', () => {
const sameBrand = resolveSelectionForBrand(
JSON.stringify({ vehicleBrandId: '1', vehicleSeriesId: 3 }),
{ vehicleBrandId: 1 }
)
assert.equal(sameBrand.vehicleSeriesId, 3)
assert.equal(
resolveSelectionForBrand(
{ vehicleBrandId: 0, vehicleSeriesId: 3 },
{ vehicleBrandId: '0' }
).vehicleBrandId,
0
)
assert.equal(
resolveSelectionForBrand(
{ vehicleBrandId: 1, vehicleSeriesId: 3 },
{ vehicleBrandId: 2 }
),
null
)
assert.equal(resolveSelectionForBrand({ vehicleSeriesId: 3 }, { vehicleBrandId: 1 }), null)
assert.equal(
resolveSelectionForBrand(
{ vehicleBrandId: 1, vehicleSeriesId: 3 },
{}
),
null
)
})
})