mapDetailResponse.spec.js 2.28 KB
const { mapDetailResponse } = require("@/api/adapters/journalRecord/mapDetailResponse");

describe("mapDetailResponse", () => {
	it("应保留接口返回的模板类型", () => {
		expect(mapDetailResponse({ templateType: 2 }).templateType).toBe(2);
	});

	it("接口缺少模板类型时使用调用上下文", () => {
		expect(mapDetailResponse({}, { templateType: 2 }).templateType).toBe(2);
	});

	it("路由中的单个品牌应归一化成品牌数组", () => {
		expect(mapDetailResponse({}, { brandIds: "3454" }).brandIds).toEqual(["3454"]);
	});

	it("接口缺少场景码时应保留路由上下文", () => {
		expect(mapDetailResponse({}, { baseSceneCode: "100101" }).baseSceneCode).toBe("100101");
	});

	it("详情应使用精确场景码识别延保", () => {
		const detail = mapDetailResponse({
			sceneType: 0,
			baseSceneCode: 110201,
		});
		expect(detail.bizTypeName).toBe("延保");
	});

	it("详情应使用精确场景码识别二手车", () => {
		const detail = mapDetailResponse({
			sceneType: 7,
			baseSceneCode: 120101,
		});
		expect(detail.bizType).toBe(7);
		expect(detail.bizTypeName).toBe("二手车");
	});

	it("列表业务兜底应优先于旧 sceneType 映射", () => {
		const detail = mapDetailResponse(
			{ sceneType: "0" },
			{ bizType: 9, bizTypeName: "延保" },
		);
		expect(detail.bizType).toBe(9);
		expect(detail.bizTypeName).toBe("延保");
	});

	it("精确场景码应优先于列表业务兜底", () => {
		const detail = mapDetailResponse(
			{ sceneType: "1" },
			{ baseSceneCode: 110201, bizType: 9, bizTypeName: "列表兜底" },
		);
		expect(detail.bizType).toBe("延保");
		expect(detail.bizTypeName).toBe("延保");
	});

	it("详情接口明确业务字段应优先于精确场景和列表兜底", () => {
		const detail = mapDetailResponse(
			{
				sceneType: "0",
				baseSceneCode: 110201,
				bizType: 7,
				bizTypeName: "接口业务",
			},
			{ bizType: 9, bizTypeName: "列表兜底" },
		);
		expect(detail.bizType).toBe(7);
		expect(detail.bizTypeName).toBe("接口业务");
	});

	it("空字符串列表兜底应视为缺失并继续使用旧 sceneType", () => {
		const detail = mapDetailResponse(
			{ sceneType: "1" },
			{ bizType: "", bizTypeName: "" },
		);
		expect(detail.bizType).toBe(1);
		expect(detail.bizTypeName).toBe("轻改");
	});
});