From 180ef959b350de243e6aac6fa9a5f00354f791c4 Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Thu, 23 Oct 2025 06:26:43 +0000 Subject: [PATCH] =?UTF-8?q?test(nest):=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/nest.test.ts | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/helper/nest.test.ts diff --git a/src/helper/nest.test.ts b/src/helper/nest.test.ts new file mode 100644 index 0000000..9af96bd --- /dev/null +++ b/src/helper/nest.test.ts @@ -0,0 +1,79 @@ +import { expect, test } from 'vitest'; +import { getNestedProperty } from './nest'; + +test('getNestedProperty - basic object', () => { + const data = { + a: { + b: { + c: 42 + } + } + } + expect(getNestedProperty(data, 'a.b.c')).toBe(42); +}) + +test('getNestedProperty - nested arrays', () => { + const data = { + translations: [ + { languages_code: 'zh-CN', name: '中文' }, + { languages_code: 'en-US', name: 'English' } + ] + }; + expect(getNestedProperty(data, 'translations.name')).toEqual(['中文', 'English']); +}) + +test('getNestedProperty - missing path', () => { + const data = { + a: { + b: { + c: 42 + } + } + } + expect(getNestedProperty(data, 'a.x.c')).toBeUndefined(); +}) + +test('getNestedProperty - array of objects', () => { + const data = { + items: [ + { + id: 1, value: { + text: 'one' + } + }, + { + id: 2, value: { + text: 'two' + } + }, + { + id: 3, value: { + text: 'three' + } + } + ] + }; + expect(getNestedProperty(data, 'items.value.text')).toEqual(['one', 'two', 'three']); +}) + +test('getNestedProperty - array in array', () => { + const data = { + groups: [ + { + name: 'group1', + value: [ + { text: 'a' }, + { text: 'b' } + ] + }, + { + name: 'group2', + value: [ + { text: 'c' }, + { text: 'd' } + ] + } + ] + } + expect(getNestedProperty(data, 'groups.value.text')).toEqual([['a', 'b'], ['c', 'd']]); +}) \ No newline at end of file