Files
jinshen-website/server/mappers/documentMapper.test.ts
R2m1liA 1245df497b refactor: 重构产品相关Mapper
- 空值处理与类型控制:为相关关系型字段的数据处理添加空值处理与类型控制
- 调整目录结构:将文件目录按照实际查询划分为Product和ProductList两个文件
2025-12-04 17:38:43 +08:00

107 lines
2.3 KiB
TypeScript

import { describe, test, expect } from 'vitest';
import { toDocumentListView, toDocumentTypeView } from './documentMapper';
/**
* 单元测试: toDocumentTypeView
*/
describe('toDocumentTypeView', () => {
const baseData: DocumentType = {
id: 1,
translations: [{ id: 1, name: 'Type Name' }],
};
test('convert raw data to DocumentTypeView correctly', () => {
const rawData: DocumentType = {
...baseData,
};
expect(toDocumentTypeView(rawData)).toEqual({
id: '1',
name: 'Type Name',
});
});
test('convert raw data with missing translations', () => {
const rawData: DocumentType = {
...baseData,
translations: [],
};
expect(toDocumentTypeView(rawData)).toEqual({
id: '1',
name: '',
});
});
test('convert null input to default DocumentTypeView', () => {
const rawData: DocumentType | null = null;
expect(toDocumentTypeView(rawData)).toEqual({
id: '-1',
name: '',
});
});
});
/**
* 单元测试: toDocumentListView
*/
describe('toDocumentListView', () => {
const baseData: ProductDocument = {
id: 1,
type: {
id: 1,
translations: [
{
id: 1,
name: 'Type A',
},
],
},
file: {
id: 'rand-om__-uuid-1234',
filename_download: 'document.pdf',
filesize: 2048,
},
translations: [{ id: 10, title: 'Document Title' }],
products: [
{
id: 10,
products_id: {
id: 1,
translations: [{ id: 1, name: 'Product A' }],
product_type: {
id: 1,
translations: [{ id: 1, name: 'Type A' }],
},
},
},
],
};
test('convert raw data with fileMeta to DocumentListView correctly', () => {
const rawData: ProductDocument = { ...baseData };
expect(toDocumentListView(rawData)).toEqual({
id: '1',
fileId: 'rand-om__-uuid-1234',
filename: 'document.pdf',
title: 'Document Title',
url: '/api/assets/rand-om__-uuid-1234',
size: 2048,
type: {
id: '1',
name: 'Type A',
},
products: [
{
id: '1',
name: 'Product A',
type: {
id: '1',
name: 'Type A',
},
},
],
});
});
});