/
paths.spec.js
68 lines (60 loc) · 1.24 KB
/
paths.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {paths} from './paths'
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
p: [{q: 3}],
x: {
y: 'FOO',
z: [[{}]],
},
}
test('with string path + curry', () => {
const pathsInput = ['a.b.d', 'p.q']
const expected = [2, undefined]
const result = paths(pathsInput, obj)
const curriedResult = paths(pathsInput)(obj)
expect(result).toEqual(expected)
expect(curriedResult).toEqual(expected)
})
test('with array path', () => {
const result = paths(
[
['a', 'b', 'c'],
['x', 'y'],
],
obj
)
expect(result).toEqual([1, 'FOO'])
})
test('takes a paths that contains indices into arrays', () => {
expect(
paths(
[
['p', 0, 'q'],
['x', 'z', 0, 0],
],
obj
)
).toEqual([3, {}])
expect(
paths(
[
['p', 0, 'q'],
['x', 'z', 2, 1],
],
obj
)
).toEqual([3, undefined])
})
test("gets a deep property's value from objects", () => {
expect(paths([['a', 'b']], obj)).toEqual([obj.a.b])
expect(paths([['p', 0]], obj)).toEqual([obj.p[0]])
})
test('returns undefined for items not found', () => {
expect(paths([['a', 'x', 'y']], obj)).toEqual([undefined])
expect(paths([['p', 2]], obj)).toEqual([undefined])
})