- 空值处理与类型控制:为相关关系型字段的数据处理添加空值处理与类型控制 - 调整目录结构:将文件目录按照实际查询划分为Product和ProductList两个文件
126 lines
2.5 KiB
TypeScript
126 lines
2.5 KiB
TypeScript
import { describe, expect, test } from 'vitest';
|
|
import { toProductTypeView, toProductListView } from './productListMapper';
|
|
|
|
/**
|
|
* 单元测试: toProductTypeView
|
|
*/
|
|
describe('toProductTypeView', () => {
|
|
const baseData: ProductType = {
|
|
id: 1,
|
|
translations: [
|
|
{
|
|
id: 1,
|
|
name: 'Type Name',
|
|
},
|
|
],
|
|
sort: 5,
|
|
};
|
|
|
|
test('convert raw data to ProductTypeView correctly', () => {
|
|
const rawData: ProductType = { ...baseData };
|
|
|
|
expect(toProductTypeView(rawData)).toEqual({
|
|
id: '1',
|
|
name: 'Type Name',
|
|
sort: 5,
|
|
});
|
|
});
|
|
|
|
test('convert raw data with missing translations', () => {
|
|
const rawData: ProductType = {
|
|
...baseData,
|
|
translations: [],
|
|
};
|
|
|
|
expect(toProductTypeView(rawData)).toEqual({
|
|
id: '1',
|
|
name: '',
|
|
sort: 5,
|
|
});
|
|
});
|
|
|
|
test('convert raw data with missing sort', () => {
|
|
const rawData: ProductType = {
|
|
...baseData,
|
|
sort: undefined,
|
|
};
|
|
|
|
expect(toProductTypeView(rawData)).toEqual({
|
|
id: '1',
|
|
name: 'Type Name',
|
|
sort: 999,
|
|
});
|
|
});
|
|
|
|
test('convert null input to default value', () => {
|
|
const rawData: ProductType | string | null = null;
|
|
|
|
expect(toProductTypeView(rawData)).toEqual({
|
|
id: '-1',
|
|
name: '',
|
|
sort: 999,
|
|
});
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 单元测试: toProductListView
|
|
*/
|
|
describe('toProductListView', () => {
|
|
test('convert raw data to ProductListView correctly', () => {
|
|
const rawData: Product = {
|
|
id: 1,
|
|
translations: [
|
|
{ id: 10, name: 'Product Name', summary: 'Product Summary' },
|
|
],
|
|
cover: {
|
|
id: 'cover-file-uuid-1234',
|
|
},
|
|
product_type: {
|
|
id: 1,
|
|
translations: [{ id: 20, name: 'Type Name' }],
|
|
sort: 1,
|
|
},
|
|
};
|
|
|
|
expect(toProductListView(rawData)).toEqual({
|
|
id: '1',
|
|
name: 'Product Name',
|
|
summary: 'Product Summary',
|
|
cover: 'cover-file-uuid-1234',
|
|
product_type: {
|
|
id: '1',
|
|
name: 'Type Name',
|
|
sort: 1,
|
|
},
|
|
});
|
|
});
|
|
|
|
test('convert raw data with missing translations', () => {
|
|
const rawData: Product = {
|
|
id: 1,
|
|
translations: [],
|
|
cover: {
|
|
id: 'cover-file-uuid-1234',
|
|
},
|
|
product_type: {
|
|
id: 20,
|
|
translations: [],
|
|
sort: 1,
|
|
},
|
|
};
|
|
|
|
expect(toProductListView(rawData)).toEqual({
|
|
id: '1',
|
|
name: '',
|
|
summary: '',
|
|
cover: 'cover-file-uuid-1234',
|
|
product_type: {
|
|
id: '20',
|
|
name: '',
|
|
sort: 1,
|
|
},
|
|
});
|
|
});
|
|
});
|