180 lines
3.6 KiB
TypeScript
180 lines
3.6 KiB
TypeScript
import { describe, expect, test } from 'vitest';
|
|
import {
|
|
toProductQuestionView,
|
|
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',
|
|
type: '',
|
|
});
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 单元测试: toProductQuestionView
|
|
*/
|
|
describe('toProductQuestionView', () => {
|
|
const baseData: Question = {
|
|
id: 1,
|
|
translations: [
|
|
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
|
],
|
|
};
|
|
test('convert raw data to ProductQuestionView correctly', () => {
|
|
const rawData: Question = {
|
|
...baseData,
|
|
};
|
|
|
|
expect(toProductQuestionView(rawData)).toEqual({
|
|
id: '1',
|
|
title: 'Question Title',
|
|
content: 'Question Answer',
|
|
});
|
|
});
|
|
|
|
test('convert raw data with missing translations', () => {
|
|
const rawData: Question = {
|
|
...baseData,
|
|
translations: [],
|
|
};
|
|
|
|
expect(toProductQuestionView(rawData)).toEqual({
|
|
id: '1',
|
|
title: '',
|
|
content: '',
|
|
});
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 单元测试: 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: '',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
});
|
|
});
|