-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.test.js
55 lines (47 loc) · 1.33 KB
/
paths.test.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
const { paths } = require("../src/datamix");
describe("paths", () => {
it("returns empty objects as an empty array", function () {
expect(paths({})).toStrictEqual([]);
});
it("returns an entry for empty objects", function () {
expect(paths({o: {}})).toStrictEqual(['o']);
});
it("returns an entry for empty arrays", function () {
expect(paths({l: []})).toStrictEqual(['l']);
});
it("returns shallow object as is", function () {
expect(paths({a: 1, b: 2})).toStrictEqual(['a', 'b']);
});
it("returns paths of deep objects", function () {
expect(paths({
a: {x: 1, y: 2},
b: ['foo', "bar"],
c: [{active: false}],
})).toStrictEqual(['a.x', 'a.y', 'b', 'c']);
});
it("returns paths of deep objects by traversing arrays", function () {
expect(paths({
a: {x: 1, y: 2},
b: 'foo',
c: [{bar: 'baz'}],
}, true)).toStrictEqual(['a.x', 'a.y', 'b', 'c.0.bar']);
});
it("returns paths of arrays", function () {
expect(paths({
list: [
{a: 1},
{b: 2},
{d: 4},
]
}, false)).toStrictEqual(['list']);
});
it("returns paths of arrays by traversing arrays", function () {
expect(paths({
list: [
{a: 1},
{b: 2},
{d: 4},
]
}, true)).toStrictEqual(['list.0.a', 'list.1.b', 'list.2.d']);
});
});