test(nest): 添加测试用例
This commit is contained in:
79
src/helper/nest.test.ts
Normal file
79
src/helper/nest.test.ts
Normal file
@ -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<number>(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<string>(data, 'translations.name')).toEqual(['中文', 'English']);
|
||||||
|
})
|
||||||
|
|
||||||
|
test('getNestedProperty - missing path', () => {
|
||||||
|
const data = {
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c: 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect(getNestedProperty<number>(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<string>(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<string>(data, 'groups.value.text')).toEqual([['a', 'b'], ['c', 'd']]);
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user