-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathpreserveArray.test.js
115 lines (109 loc) · 2.5 KB
/
preserveArray.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
import preserveArray from './preserveArray';
describe('.preserveArray', () => {
test('returns diff with nested objects converted back to arrays when property is deleted', () => {
const left = { a: [{ b: ['#', '#', '#', { hello: '' }, { hi: ''}] }, '#', { c: '', d: ['#', ''] }, '#'] };
const right = { a: [{ b: ['#', '#', '#', { hello: 'world' }] }, '#', { c: 'hello', d: ['#', 'bob'] }] };
const diff = {
a: {
0: {
b: {
3: {
hello: 'world'
},
4: undefined
}
},
2: {
c: 'hello',
d: {
1: 'bob'
}
},
3: undefined
}
};
const expected = {
a: [
{
b: [
'empty',
'empty',
'empty',
{
hello: 'world'
},
undefined
]
},
'empty',
{
c: 'hello',
d: ['empty', 'bob']
},
undefined
]
};
// Remove 'empty'
delete expected.a[0].b[0];
delete expected.a[0].b[1];
delete expected.a[0].b[2];
delete expected.a[1];
delete expected.a[2].d[0];
expect(preserveArray(diff, left, right)).toEqual(expected);
});
test('returns diff with nested objects converted back to arrays when new property is added', () => {
const left = { a: [{ b: ['#', '#', '#', { hello: '' }] }, '#', { c: '', d: ['#', ''] }] };
const right = { a: [{ b: ['#', '#', '#', { hello: 'world' }, {hi: ''}] }, '#', { c: 'hello', d: ['#', 'bob'] }, 'foobar'] };
const diff = {
a: {
0: {
b: {
3: {
hello: 'world'
},
4: {
hi: ''
}
}
},
2: {
c: 'hello',
d: {
1: 'bob'
}
},
3: 'foobar'
}
};
const expected = {
a: [
{
b: [
'empty',
'empty',
'empty',
{
hello: 'world'
},
{
hi: ''
}
]
},
'empty',
{
c: 'hello',
d: ['empty', 'bob']
},
'foobar'
]
};
// Remove 'empty'
delete expected.a[0].b[0];
delete expected.a[0].b[1];
delete expected.a[0].b[2];
delete expected.a[1];
delete expected.a[2].d[0];
expect(preserveArray(diff, left, right)).toEqual(expected);
});
});