Files
jinshen-website/server/mappers/homepageMapper.test.ts
R2m1liA 23f2700c0f
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
refactor: 将Data到ViewModel的转换由App转移至Server端
- 将逻辑转移到Server端后,简化前端逻辑
2025-11-13 20:45:43 +08:00

96 lines
2.2 KiB
TypeScript

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: '' }],
});
});
});