|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
|
|
|
|
|
import { bindMethods } from '../util';
|
|
|
|
|
import { bindMethods, getNestedValue } from '../util';
|
|
|
|
|
|
|
|
|
|
class TestClass {
|
|
|
|
|
public value: string;
|
|
|
|
|
@ -78,3 +78,79 @@ describe('bindMethods', () => {
|
|
|
|
|
expect(value).toBe('test');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getNestedValue', () => {
|
|
|
|
|
interface UserProfile {
|
|
|
|
|
age: number;
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface UserSettings {
|
|
|
|
|
theme: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface Data {
|
|
|
|
|
user: {
|
|
|
|
|
profile: UserProfile;
|
|
|
|
|
settings: UserSettings;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data: Data = {
|
|
|
|
|
user: {
|
|
|
|
|
profile: {
|
|
|
|
|
age: 25,
|
|
|
|
|
name: 'Alice',
|
|
|
|
|
},
|
|
|
|
|
settings: {
|
|
|
|
|
theme: 'dark',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it('should get a nested value when the path is valid', () => {
|
|
|
|
|
const result = getNestedValue(data, 'user.profile.name');
|
|
|
|
|
expect(result).toBe('Alice');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return undefined for non-existent property', () => {
|
|
|
|
|
const result = getNestedValue(data, 'user.profile.gender');
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return undefined when accessing a non-existent deep path', () => {
|
|
|
|
|
const result = getNestedValue(data, 'user.nonexistent.field');
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return undefined if a middle level is undefined', () => {
|
|
|
|
|
const result = getNestedValue({ user: undefined }, 'user.profile.name');
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return the correct value for a nested setting', () => {
|
|
|
|
|
const result = getNestedValue(data, 'user.settings.theme');
|
|
|
|
|
expect(result).toBe('dark');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should work for a single-level path', () => {
|
|
|
|
|
const result = getNestedValue({ a: 1, b: 2 }, 'b');
|
|
|
|
|
expect(result).toBe(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return the entire object if path is empty', () => {
|
|
|
|
|
expect(() => getNestedValue(data, '')()).toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle paths with array indexes', () => {
|
|
|
|
|
const complexData = { list: [{ name: 'Item1' }, { name: 'Item2' }] };
|
|
|
|
|
const result = getNestedValue(complexData, 'list.1.name');
|
|
|
|
|
expect(result).toBe('Item2');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return undefined when accessing an out-of-bounds array index', () => {
|
|
|
|
|
const complexData = { list: [{ name: 'Item1' }] };
|
|
|
|
|
const result = getNestedValue(complexData, 'list.2.name');
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|