dailyReportCarBrand.test.mjs 2.39 KB
import { describe, it, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const {
	normalizeCarBrandListForSave,
	normalizeSceneCodeListToSceneList,
	fetchDailyReportCarBrandList,
	clearDailyReportCarBrandCache,
	__setStaffDailyReportCarBrand
} = require('../api/dailyReportCarBrand.js')

describe('dailyReportCarBrand', () => {
	beforeEach(() => {
		clearDailyReportCarBrandCache()
		__setStaffDailyReportCarBrand(null)
	})

	it('normalizeCarBrandListForSave maps four fields', () => {
		const list = normalizeCarBrandListForSave([
			{
				firstBrandId: 3454,
				firstBrandName: '问界',
				brandId: 3454,
				brandName: 'AITO',
				brandType: 0
			}
		])
		assert.deepEqual(list, [
			{
				firstBrandId: 3454,
				firstBrandName: '问界',
				brandId: 3454,
				brandName: 'AITO'
			}
		])
	})

	it('normalizeCarBrandListForSave uses firstBrandName when brandId is -1', () => {
		const list = normalizeCarBrandListForSave([
			{
				firstBrandId: 999,
				firstBrandName: '自定义品牌',
				brandId: -1,
				brandName: ''
			}
		])
		assert.equal(list[0].brandName, '自定义品牌')
	})

	it('normalizeSceneCodeListToSceneList converts string codes', () => {
		assert.deepEqual(normalizeSceneCodeListToSceneList(['110201', '100101']), [
			{ baseSceneCode: 110201 },
			{ baseSceneCode: 100101 }
		])
	})

	it('normalizeSceneCodeListToSceneList converts scene objects', () => {
		assert.deepEqual(
			normalizeSceneCodeListToSceneList([
				{ baseSceneCode: '110201', name: '售前延保' },
				{ baseSceneCode: 100101, name: '精品' }
			]),
			[{ baseSceneCode: 110201 }, { baseSceneCode: 100101 }]
		)
	})

	it('fetchDailyReportCarBrandList uses mock fixture when useMock', async () => {
		const res = await fetchDailyReportCarBrandList({
			merchantId: 'm1',
			sceneType: 1,
			useMock: true
		})
		assert.equal(res.code, 0)
		assert.equal(res.data.length, 2)
	})

	it('fetchDailyReportCarBrandList caches by merchantId and sceneType', async () => {
		let calls = 0
		__setStaffDailyReportCarBrand(async () => {
			calls += 1
			return {
				code: 0,
				data: [{ firstBrandId: 1, firstBrandName: 'A', brandId: 1, brandName: 'A' }]
			}
		})
		await fetchDailyReportCarBrandList({ merchantId: 'm1', sceneType: 0 })
		await fetchDailyReportCarBrandList({ merchantId: 'm1', sceneType: 0 })
		assert.equal(calls, 1)
	})
})