refactor: 将Data到ViewModel的转换由App转移至Server端
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
This commit is contained in:
121
server/mappers/questionMapper.test.ts
Normal file
121
server/mappers/questionMapper.test.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { toProductQuestionView, toQuestionListView } from './questionMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: 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,
|
||||
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,
|
||||
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,
|
||||
title: '',
|
||||
content: '',
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
name: '',
|
||||
type: {
|
||||
id: 1,
|
||||
name: '',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user