/
path.spec.js
33 lines (24 loc) · 880 Bytes
/
path.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {path} from './path'
test('with array inside object', () => {
const obj = {a: {b: [1, {c: 1}]}}
expect(path('a.b.1.c', obj)).toBe(1)
})
test('works with undefined', () => {
const obj = {a: {b: {c: 1}}}
expect(path('a.b.c.d.f', obj)).toBeUndefined()
expect(path('foo.babaz', undefined)).toBeUndefined()
expect(path('foo.babaz')(undefined)).toBeUndefined()
})
test('works with string instead of array', () => {
expect(path('foo.bar.baz')({foo: {bar: {baz: 'yes'}}})).toEqual('yes')
})
test('path', () => {
expect(path(['foo', 'bar', 'baz'])({foo: {bar: {baz: 'yes'}}})).toEqual(
'yes'
)
expect(path(['foo', 'bar', 'baz'])(null)).toBeUndefined()
expect(path(['foo', 'bar', 'baz'])({foo: {bar: 'baz'}})).toBeUndefined()
})
test('null is not a valid path', () => {
expect(path('audio_tracks', {a: 1, audio_tracks: null})).toBeUndefined()
})