fix: 调整nuxt测试路径

- 将nuxt测试路径调整到组件旁
This commit is contained in:
2025-10-27 13:50:36 +08:00
parent 5be3c45ac5
commit 9294cd3199
3 changed files with 53 additions and 20 deletions

View File

@ -0,0 +1,49 @@
import { describe, it, expect } from 'vitest';
describe('toProductSpecView', () => {
it('should map translations.key an value corrently', () => {
const raw: ProductSpec = {
id: 1,
value: '100W',
translations: [
{
id: 1,
key: 'Power',
},
],
};
const expected: ProductSpecView = {
id: 1,
key: 'Power',
value: '100W',
};
expect(toProductSpecView(raw)).toEqual(expected);
});
it('should handle missing translations gracefully', () => {
const raw: ProductSpec = { id: 2, value: '220V', translations: [] };
const view = toProductSpecView(raw);
expect(view.key).toBe('');
});
});
describe('toProductSpecGroupView', () => {
it('should map translations.name and specs correctly', () => {
const raw: ProductSpecGroup = {
id: 1,
translations: [{ id: 1, name: 'Electrical' }],
specs: [
{ id: 1, value: '100W', translations: [{ id: 1, key: 'Power' }] },
{ id: 2, value: '220V', translations: [{ id: 2, key: 'Voltage' }] },
],
};
const view: ProductSpecGroupView = toProductSpecGroupView(raw);
expect(view.name).toBe('Electrical');
expect(view.specs).toHaveLength(2);
expect(view.specs[0].key).toBe('Power');
expect(view.specs[1].value).toBe('220V');
});
});

51
app/utils/file.test.ts Normal file
View File

@ -0,0 +1,51 @@
import {
formatFileSize,
getFileExtension,
formatFileExtension,
} from '@/utils/file';
import { expect, test, describe } from 'vitest';
/**
* 单元测试: formatFileSize
*/
describe('formatFileSize', () => {
test('format bytes correctly', () => {
expect(formatFileSize(500)).toBe('500.00 B');
});
test('format kilobytes correctly', () => {
expect(formatFileSize(2048)).toBe('2.00 KB');
});
test('format megabytes correctly', () => {
expect(formatFileSize(5 * 1024 * 1024)).toBe('5.00 MB');
});
});
/**
* 单元测试: getFileExtension
*/
describe('getFileExtension', () => {
test('extract extension from filename', () => {
expect(getFileExtension('document.pdf')).toBe('pdf');
});
test('handle filenames without extension', () => {
expect(getFileExtension('README')).toBe('');
});
test('handle multiple dots in filename', () => {
expect(getFileExtension('archive.tar.gz')).toBe('gz');
});
test('handle empty filename', () => {
expect(getFileExtension('')).toBe('');
});
});
/**
* 单元测试: formatFileExtension
*/
describe('formatFileExtension', () => {
test('format extension without dot', () => {
expect(formatFileExtension('txt')).toBe('TXT');
});
test('format extension with dot', () => {
expect(formatFileExtension('.jpg')).toBe('JPG');
});
});