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:
106
app/models/mappers/documentMapper.test.ts
Normal file
106
app/models/mappers/documentMapper.test.ts
Normal file
@ -0,0 +1,106 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
|
||||
/**
|
||||
* 单元测试: toProductDocumentView
|
||||
*/
|
||||
describe('toProductDocumentView', () => {
|
||||
test('convert raw data with fileId to ProductDocumentView correctly', () => {
|
||||
const rawData: ProductDocument = {
|
||||
id: 1,
|
||||
file: 'rand-om__-uuid-1234',
|
||||
translations: [{ id: 10, title: 'Document Title' }],
|
||||
};
|
||||
|
||||
expect(toProductDocumentView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: undefined,
|
||||
title: 'Document Title',
|
||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
||||
size: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with fileMeta to ProductDocumentView correctly', () => {
|
||||
const rawData: ProductDocument = {
|
||||
id: 1,
|
||||
file: {
|
||||
id: 'rand-om__-uuid-1234',
|
||||
filename_download: 'document.pdf',
|
||||
filesize: 2048,
|
||||
},
|
||||
translations: [{ id: 10, title: 'Document Title' }],
|
||||
};
|
||||
|
||||
expect(toProductDocumentView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: 'document.pdf',
|
||||
title: 'Document Title',
|
||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
||||
size: 2048,
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: ProductDocument = {
|
||||
id: 1,
|
||||
file: 'rand-om__-uuid-1234',
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toProductDocumentView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: undefined,
|
||||
title: '',
|
||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
||||
size: undefined,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toDocumentListView
|
||||
*/
|
||||
describe('toProductDocumentView', () => {
|
||||
test('convert raw data with fileId to DocumentListView correctly', () => {
|
||||
const rawData: ProductDocument = {
|
||||
id: 1,
|
||||
file: 'rand-om__-uuid-1234',
|
||||
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' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(toDocumentListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: undefined,
|
||||
title: 'Document Title',
|
||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
||||
size: undefined,
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Product A',
|
||||
type: {
|
||||
id: 1,
|
||||
name: 'Type A',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user