FrontendAtlas
Study
▾
Dashboard
Pricing
Get full access
</> Code
Test cases
Language
JavaScript
TypeScript
export default function getByPath(obj, path, fallback) { throw new Error('Not implemented'); }
import getByPath from './file'; describe('getByPath', () => { test('reads nested values by dot path', () => { const data = { user: { profile: { name: 'Ada' } } }; expect(getByPath(data, 'user.profile.name')).toBe('Ada'); }); test('reads nested values by array path', () => { const data = { user: { flags: { pro: false } } }; expect(getByPath(data, ['user', 'flags', 'pro'])).toBe(false); }); test('supports array indexing via numeric segment', () => { const data = { items: [{ name: 'x' }, { name: 'y' }] }; expect(getByPath(data, 'items.0.name')).toBe('x'); expect(getByPath(data, ['items', 1, 'name'])).toBe('y'); }); test('returns fallback only when missing', () => { const data = { a: { b: 0, c: '', d: null } }; expect(getByPath(data, 'a.b', 123)).toBe(0); expect(getByPath(data, 'a.c', 'fb')).toBe(''); expect(getByPath(data, 'a.d', 'fb')).toBe(null); expect(getByPath(data, 'a.missing', 'fb')).toBe('fb'); }); test('returns undefined when missing and no fallback provided', () => { const data = { a: {} }; expect(getByPath(data, 'a.nope')).toBe(undefined); }); test('empty path returns the input object', () => { const data = { a: 1 }; expect(getByPath(data, '', 'fb')).toBe(data); expect(getByPath(data, [], 'fb')).toBe(data); }); test('does not throw on nullish intermediates', () => { const data = { a: null }; expect(getByPath(data, 'a.b.c', 'fb')).toBe('fb'); }); test('treats present-but-undefined as present (returns undefined)', () => { const data = { a: { b: undefined } }; expect(getByPath(data, 'a.b', 'fb')).toBe(undefined); }); });
▶ Run tests
Results
Console
Run tests to see results.