From 445ee3fd92ab3616391efd0e756e5632647897c8 Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Thu, 23 Oct 2025 08:38:42 +0000 Subject: [PATCH] =?UTF-8?q?test(collection):=20=E6=B7=BB=E5=8A=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helper/collection.test.ts | 106 ++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/helper/collection.test.ts 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