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:
95
server/mappers/homepageMapper.test.ts
Normal file
95
server/mappers/homepageMapper.test.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toHomepageView } from './homepageMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toHomepageView
|
||||
*/
|
||||
describe('toHomepageView', () => {
|
||||
test('convert raw data to HomepageView correctly', () => {
|
||||
const rawData: Homepage = {
|
||||
id: 1,
|
||||
carousel: [
|
||||
{
|
||||
id: 1,
|
||||
directus_files_id: {
|
||||
id: 'file-uuid-1',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
directus_files_id: {
|
||||
id: 'file-uuid-2',
|
||||
},
|
||||
},
|
||||
],
|
||||
recommend_products: [
|
||||
{
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Product 1', summary: 'Summary 1' }],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-1',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
translations: [{ id: 2, name: 'Product 2', summary: 'Summary 2' }],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-2',
|
||||
},
|
||||
},
|
||||
],
|
||||
recommend_solutions: [
|
||||
{
|
||||
id: 1,
|
||||
translations: [{ id: 1, title: 'Solution 1', summary: 'Summary 1' }],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-1',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(toHomepageView(rawData)).toEqual({
|
||||
id: 1,
|
||||
carousel: ['file-uuid-1', 'file-uuid-2'],
|
||||
recommendProducts: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Product 1',
|
||||
summary: 'Summary 1',
|
||||
cover: 'cover-file-uuid-1',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'Product 2',
|
||||
summary: 'Summary 2',
|
||||
cover: 'cover-file-uuid-2',
|
||||
},
|
||||
],
|
||||
recommendSolutions: [
|
||||
{
|
||||
id: 1,
|
||||
title: 'Solution 1',
|
||||
summary: 'Summary 1',
|
||||
cover: 'cover-file-uuid-1',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Homepage = {
|
||||
id: 1,
|
||||
carousel: [],
|
||||
recommend_products: [{ id: 1, translations: [], cover: null }],
|
||||
recommend_solutions: [{ id: 1, translations: [], cover: null }],
|
||||
};
|
||||
|
||||
expect(toHomepageView(rawData)).toEqual({
|
||||
id: 1,
|
||||
carousel: [],
|
||||
recommendProducts: [{ id: 1, name: '', summary: '', cover: '' }],
|
||||
recommendSolutions: [{ id: 1, title: '', summary: '', cover: '' }],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user