Files
jinshen-website/server/mappers/documentMapper.test.ts
R2m1liA c27337a145 feat: 文档库查询添加文档类型字段
- graphQL查询修改:添加type字段查询
- 视图模型字段更新:添加DocumentTypeView视图模型并为DocumentList类型添加type字段
- mapper更新: 添加DocumentType的转换方法
2025-12-03 17:30:16 +08:00

168 lines
3.7 KiB
TypeScript

import { describe, test, expect } from 'vitest';
import {
toProductDocumentView,
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: '',
});
});
});
/**
* 单元测试: toProductDocumentView
*/
describe('toProductDocumentView', () => {
const baseData: ProductDocument = {
id: 1,
file: {
id: 'rand-om__-uuid-1234',
filename_download: 'document.pdf',
filesize: 2048,
},
translations: [{ id: 10, title: 'Document Title' }],
};
test('convert raw data with fileMeta to ProductDocumentView correctly', () => {
const rawData: ProductDocument = { ...baseData };
expect(toProductDocumentView(rawData)).toEqual({
id: '1',
fileId: 'rand-om__-uuid-1234',
filename: 'document.pdf',
title: 'Document Title',
url: '/api/assets/rand-om__-uuid-1234',
size: 2048,
});
});
test('convert raw data with fileId', () => {
const rawData: ProductDocument = {
...baseData,
file: 'rand-om__-uuid-1234',
};
expect(toProductDocumentView(rawData)).toEqual({
id: '1',
fileId: '',
filename: '',
title: 'Document Title',
url: '/api/assets/',
size: 0,
});
});
test('convert raw data with missing translations', () => {
const rawData: ProductDocument = {
...baseData,
translations: [],
};
expect(toProductDocumentView(rawData)).toEqual({
id: '1',
fileId: 'rand-om__-uuid-1234',
filename: 'document.pdf',
title: '',
url: '/api/assets/rand-om__-uuid-1234',
size: 2048,
});
});
});
/**
* 单元测试: 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',
},
},
],
});
});
});