- 空值处理与类型控制:为相关关系型字段的数据处理添加空值处理与类型控制 - 调整目录结构:将文件目录按照实际查询划分为Product和ProductList两个文件
140 lines
2.8 KiB
TypeScript
140 lines
2.8 KiB
TypeScript
import { describe, expect, test } from 'vitest';
|
|
import { toQuestionListView, toQuestionTypeView } from './questionMapper';
|
|
|
|
/**
|
|
* 单元测试: toQuestionTypeView
|
|
*/
|
|
describe('toQuestionTypeView', () => {
|
|
const baseData: QuestionType = {
|
|
id: 1,
|
|
translations: [{ id: 1, name: 'Type Name' }],
|
|
};
|
|
|
|
test('convert raw data to QuestionTypeView correctly', () => {
|
|
const rawData: QuestionType = {
|
|
...baseData,
|
|
};
|
|
|
|
expect(toQuestionTypeView(rawData)).toEqual({
|
|
id: '1',
|
|
name: 'Type Name',
|
|
});
|
|
});
|
|
|
|
test('convert raw data with missing translations', () => {
|
|
const rawData: QuestionType = {
|
|
...baseData,
|
|
translations: [],
|
|
};
|
|
|
|
expect(toQuestionTypeView(rawData)).toEqual({
|
|
id: '1',
|
|
name: '',
|
|
});
|
|
});
|
|
|
|
test('convert null input to default QuestionTypeView', () => {
|
|
const rawData: QuestionType | null = null;
|
|
|
|
expect(toQuestionTypeView(rawData)).toEqual({
|
|
id: '-1',
|
|
name: '',
|
|
});
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 单元测试: toQuestionListView
|
|
*/
|
|
describe('toQuestionListView', () => {
|
|
const baseData: Question = {
|
|
id: 1,
|
|
type: {
|
|
id: 1,
|
|
translations: [{ id: 1, name: 'Type Name' }],
|
|
},
|
|
translations: [
|
|
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
|
],
|
|
products: [
|
|
{
|
|
id: 1,
|
|
products_id: {
|
|
id: 1,
|
|
translations: [{ id: 1, name: 'Product Name' }],
|
|
product_type: {
|
|
id: 1,
|
|
translations: [{ id: 1, name: 'Type Name' }],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
test('convert raw data to QuestionListView correctly', () => {
|
|
const rawData: Question = {
|
|
...baseData,
|
|
};
|
|
|
|
expect(toQuestionListView(rawData)).toEqual({
|
|
id: '1',
|
|
type: {
|
|
id: '1',
|
|
name: 'Type Name',
|
|
},
|
|
title: 'Question Title',
|
|
content: 'Question Answer',
|
|
products: [
|
|
{
|
|
id: '1',
|
|
name: 'Product Name',
|
|
type: {
|
|
id: '1',
|
|
name: 'Type Name',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('convert raw data with missing translations', () => {
|
|
const rawData: Question = {
|
|
...baseData,
|
|
translations: [],
|
|
products: [
|
|
{
|
|
id: 1,
|
|
products_id: {
|
|
id: 1,
|
|
translations: [],
|
|
product_type: {
|
|
id: 1,
|
|
translations: [],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
expect(toQuestionListView(rawData)).toEqual({
|
|
id: '1',
|
|
type: {
|
|
id: '1',
|
|
name: 'Type Name',
|
|
},
|
|
title: '',
|
|
content: '',
|
|
products: [
|
|
{
|
|
id: '1',
|
|
name: '',
|
|
type: {
|
|
id: '1',
|
|
name: '',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
});
|
|
});
|