-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
130 lines (106 loc) · 2.82 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const objectDiff = require('./index');
const {assert} = require('chai');
const _ = require('lodash');
describe('object diff', () => {
it('different types', () => {
const o1 = 1;
const o2 = true;
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, [
undefined
]);
});
it('empty objects', () => {
const o1 = {};
const o2 = {};
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, []);
});
it('empty objects', () => {
const o1 = {};
const o2 = {};
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, []);
});
it('a single object entry is different', () => {
const o1 = {
a: {
b: {
c: { s: 't' },
}
}
};
const o2 = {...o1, a: {...o1.a, b: {...o1.a.b, d: 'e'}}};
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, [
'a.b.d'
]);
});
it('null vs undefined', () => {
const o1 = {
foo: null
};
const o2 = {
foo: undefined
};
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, [
'foo'
]);
});
it('entries are missing/different from each objects', () => {
const o1 = {
a: {
b: {
fn: () => 4,
arr: [1,2,3],
c: {
s: 't',
y: 'g'
},
}
}
};
const o2 = {...o1, a: {...o1.a, b: { ...o1.a.b, fn: () => 4, arr: [1,2,3,4,5], c: {s: 'e', t: 'e'}}}};
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, [
'a.b.fn',
'a.b.arr.[3]',
'a.b.arr.[4]',
'a.b.c.s',
'a.b.c.y',
'a.b.c.t'
]);
});
it('nested arrays', () => {
const o1 = {
a: {
arr: [1,[{one: 1, two: 1}], {
foo: 'bar'
}, 4]
}
};
const o2 = {...o1, a: {...o1.a, arr: [1, [{one: 2, two: 2}], {
foo: 'bar'
}, 4]}};
const diff = objectDiff(o1, o2);
assert.deepEqual(diff, [
'a.arr.[1].[0].one',
'a.arr.[1].[0].two',
]);
});
it('lodash compatibility', () => {
const o1 = {
a: {
arr: [1,[{one: 1, two: 1}], {
foo: 'bar'
}, 4]
}
};
const o2 = {...o1, a: {...o1.a, arr: [1, [{one: 42, two: 2}], {
foo: 'bar'
}, 4]}};
const diff = objectDiff(o1, o2);
assert.equal(_.get(o2, diff[0]), 42);
});
});