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:
100
server/mappers/solutionMapper.test.ts
Normal file
100
server/mappers/solutionMapper.test.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toSolutionListView, toSolutionView } from './solutionMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toSolutionListView
|
||||
*/
|
||||
describe('toSolutionListView', () => {
|
||||
const baseData: Solution = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{ id: 1, title: 'Solution Title', summary: 'Solution Summary' },
|
||||
],
|
||||
type: {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Type Name' }],
|
||||
sort: 1,
|
||||
},
|
||||
};
|
||||
test('convert raw data to SolutionListView correctly', () => {
|
||||
const rawData: Solution = { ...baseData };
|
||||
|
||||
expect(toSolutionListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: 'Solution Title',
|
||||
summary: 'Solution Summary',
|
||||
solution_type: {
|
||||
id: 1,
|
||||
name: 'Type Name',
|
||||
sort: 1,
|
||||
},
|
||||
cover: '',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Solution = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toSolutionListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: '',
|
||||
summary: '',
|
||||
solution_type: {
|
||||
id: 1,
|
||||
name: 'Type Name',
|
||||
sort: 1,
|
||||
},
|
||||
cover: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toSolutionView
|
||||
*/
|
||||
describe('toSolutionView', () => {
|
||||
const baseData: Solution = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{
|
||||
id: 1,
|
||||
title: 'Solution Title',
|
||||
summary: 'Solution Summary',
|
||||
content: 'Solution Content',
|
||||
},
|
||||
],
|
||||
create_at: '2023-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
test('convert raw data to SolutionView correctly', () => {
|
||||
const rawData: Solution = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toSolutionView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: 'Solution Title',
|
||||
summary: 'Solution Summary',
|
||||
content: 'Solution Content',
|
||||
createAt: '2023-01-01T00:00:00Z',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Solution = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toSolutionView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: '',
|
||||
summary: '',
|
||||
content: '',
|
||||
createAt: '2023-01-01T00:00:00Z',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user