diff --git a/src/helper/collection.test.ts b/src/helper/collection.test.ts new file mode 100644 index 0000000..86ac240 --- /dev/null +++ b/src/helper/collection.test.ts @@ -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(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(data, 'en-US'); + expect(result).toEqual([ + { + id: 1, + translations: [] + } + ]); +}); + +test('filterTranslations - no translations field', () => { + const data = [ + { + id: 1, + name: 'No Translations' + } + ]; + + const result = filterTranslations(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(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'])); +}); \ No newline at end of file