-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathadded.test.js
117 lines (101 loc) · 3.99 KB
/
added.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
import addedDiff from '../src/added';
describe('.addedDiff', () => {
describe('base case', () => {
describe('equal', () => {
test.each([
['int', 1],
['string', 'a'],
['boolean', true],
['null', null],
['undefined', undefined],
['object', { a: 1 }],
['array', [1]],
['function', () => ({})],
['date', new Date()],
])('returns empty object when given values of type %s are equal', (type, value) => {
expect(addedDiff(value, value)).toEqual({});
});
});
describe('not equal and not object', () => {
test.each([
[1, 2],
['a', 'b'],
[true, false],
['hello', null],
['hello', undefined],
[null, undefined],
[undefined, null],
[null, { a: 1 }],
['872983', { areaCode: '+44', number: '872983' }],
[100, () => ({})],
[() => ({}), 100],
[new Date('2017-01-01'), new Date('2017-01-02')],
])('returns empty object when values are not equal (%s, %s)', (lhs, rhs) => {
expect(addedDiff(lhs, rhs)).toEqual({});
});
});
});
describe('recursive case', () => {
describe('object', () => {
test('returns empty object when given objects are updated', () => {
expect(addedDiff({ a: 1 }, { a: 2 })).toEqual({});
});
test('returns empty object when right hand side has deletion', () => {
expect(addedDiff({ a: 1, b: 2 }, { a: 1 })).toEqual({});
});
test('returns subset of right hand side value when a key value has been added to the root', () => {
expect(addedDiff({ a: 1 }, { a: 1, b: 2 })).toEqual({ b: 2 });
});
test('returns subset of right hand side value when a key value has been added deeply', () => {
expect(addedDiff({ a: { b: 1} }, { a: { b: 1, c: 2 } })).toEqual({ a: { c: 2 } });
});
test('returns subset of right hand side with added date', () => {
expect(addedDiff({}, { date: new Date('2016') })).toEqual({ date: new Date('2016') });
});
});
describe('arrays', () => {
test('returns empty object when array is updated', () => {
expect(addedDiff([1], [2])).toEqual({});
});
test('returns empty object when right hand side array has deletions', () => {
expect(addedDiff([1, 2, 3], [1, 3])).toEqual({});
});
test('returns subset of right hand side array as object of indices to value when right hand side array has additions', () => {
expect(addedDiff([1, 2, 3], [1, 2, 3, 9])).toEqual({ 3: 9 });
});
test('returns subset of right hand side with added date', () => {
expect(addedDiff([], [new Date('2016')])).toEqual({ 0: new Date('2016') });
});
});
describe('object create null', () => {
test('returns subset of right hand side value when a key value has been added to the root', () => {
const lhs = Object.create(null);
const rhs = Object.create(null);
lhs.a = 1;
rhs.a = 1;
rhs.b = 2;
expect(addedDiff(lhs, rhs)).toEqual({ b: 2 });
});
test('returns subset of right hand side value when a key value has been added deeply', () => {
const lhs = Object.create(null);
const rhs = Object.create(null);
lhs.a = { b: 1};
rhs.a = { b: 1, c: 2 };
expect(addedDiff(lhs, rhs)).toEqual({ a: { c: 2 } });
});
test('returns subset of right hand side with added date', () => {
const lhs = Object.create(null);
const rhs = Object.create(null);
rhs.date = new Date('2016');
expect(addedDiff(lhs, rhs)).toEqual({ date: new Date('2016') });
});
});
describe('object with non-function hasOwnProperty property', () => {
test('can represent the property in diff despite it being part of Object.prototype', () => {
const lhs = {};
const rhs = { hasOwnProperty: true };
expect(addedDiff(lhs, rhs)).toEqual({ hasOwnProperty: true });
});
});
});
});