test(collection): 添加测试用例
This commit is contained in:
106
src/helper/collection.test.ts
Normal file
106
src/helper/collection.test.ts
Normal file
@ -0,0 +1,106 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { buildQueryFields, filterTranslations } from './collection';
|
||||
|
||||
test('filterTranslations - basic functionality', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 1,
|
||||
translations: [
|
||||
{ languages_code: 'zh-CN', name: '中文名称1' },
|
||||
{ languages_code: 'en-US', name: 'English Name1' }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
translations: [
|
||||
{ languages_code: 'zh-CN', name: '中文名称2' },
|
||||
{ languages_code: 'en-US', name: 'English Name2' }
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
const result = filterTranslations<typeof data[0]>(data, 'en-US');
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
translations: [
|
||||
{ languages_code: 'en-US', name: 'English Name1' }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
translations: [
|
||||
{ languages_code: 'en-US', name: 'English Name2' }
|
||||
]
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test('filterTranslations - no matching language', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 1,
|
||||
translations: [
|
||||
{ languages_code: 'zh-CN', name: '中文名称1' }
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
const result = filterTranslations<typeof data[0]>(data, 'en-US');
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
translations: []
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test('filterTranslations - no translations field', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'No Translations'
|
||||
}
|
||||
];
|
||||
|
||||
const result = filterTranslations<typeof data[0]>(data, 'en-US');
|
||||
expect(result).toEqual(data);
|
||||
});
|
||||
|
||||
test('filterTranslations - translations in deeper structure', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 1,
|
||||
details: {
|
||||
translations: [
|
||||
{ languages_code: 'zh-CN', name: '中文名称1' },
|
||||
{ languages_code: 'en-US', name: 'English Name1' }
|
||||
]
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
const result = filterTranslations<typeof data[0]>(data, 'zh-CN');
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
details: {
|
||||
translations: [
|
||||
{ languages_code: 'zh-CN', name: '中文名称1' }
|
||||
]
|
||||
}
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test('buildQueryFields - basic functionality', () => {
|
||||
const config = {
|
||||
name: 'translations.name',
|
||||
summary: 'translations.summary',
|
||||
description: 'translations.description',
|
||||
type: 'product_type.translations.name'
|
||||
};
|
||||
|
||||
const result = buildQueryFields(config);
|
||||
expect(new Set(result)).toEqual(new Set(['id', 'translations.languages_code.code', 'translations.name', 'translations.summary', 'translations.description', 'product_type.translations.languages_code.code', 'product_type.translations.name']));
|
||||
});
|
||||
Reference in New Issue
Block a user