car-brand-series-selection.test.mjs 2.89 KB
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
		)
	})
})