Files
jinshen-website/app/models/mappers/homepageMapper.test.ts
2025-11-11 16:15:05 +08:00

89 lines
2.1 KiB
TypeScript

import { describe, test, expect } from 'vitest';
/**
* 单元测试: 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: '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: '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: null }],
recommendSolutions: [{ id: 1, title: '', summary: '', cover: null }],
});
});
});