test: 为mapper补充测试
All checks were successful
deploy to server / build-and-deploy (push) Successful in 2m56s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 2m56s
This commit is contained in:
78
app/models/mappers/homepageMapper.test.ts
Normal file
78
app/models/mappers/homepageMapper.test.ts
Normal file
@ -0,0 +1,78 @@
|
||||
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: 'file-uuid-1' },
|
||||
{ id: 2, directus_files_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 }],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user