-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.spec.js
117 lines (98 loc) · 4.36 KB
/
index.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
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 { expect } from 'chai';
import forEach from 'mocha-each';
import diff from './';
describe('.diff', () => {
describe('base case', () => {
describe('equal', () => {
forEach([
['int', 1],
['string', 'a'],
['boolean', true],
['null', null],
['undefined', undefined],
['object', { a: 1 }],
['array', [1]],
['function', () => ({})],
['date', new Date()],
]).it('returns empty object when given values of type %s are equal', (type, value) => {
expect(diff(value, value)).to.deep.equal({});
});
});
describe('not equal and not object', () => {
forEach([
[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')],
]).it('returns right hand side value when different to left hand side value (%s, %s)', (lhs, rhs) => {
expect(diff(lhs, rhs)).to.deep.equal(rhs);
});
});
});
describe('recursive case', () => {
describe('object', () => {
it('returns right hand side value when given objects are different', () => {
expect(diff({ a: 1 }, { a: 2 })).to.deep.equal({ a: 2 });
});
it('returns right hand side value when right hand side value is null', () => {
expect(diff({ a: 1 }, { a: null })).to.deep.equal({ a: null });
});
it('returns subset of right hand side value when sibling objects differ', () => {
expect(diff({ a: { b: 1 }, c: 2 }, { a: { b: 1 }, c: 3 })).to.deep.equal({ c: 3 });
});
it('returns subset of right hand side value when nested values differ', () => {
expect(diff({ a: { b: 1, c: 2} }, { a: { b: 1, c: 3 } })).to.deep.equal({ a: { c: 3 } });
});
it('returns subset of right hand side value when nested values differ at multiple paths', () => {
expect(diff({ a: { b: 1 }, c: 2, d: { e: 100 } }, { a: { b: 99 }, c: 3, d: { e: 100 } })).to.deep.equal({ a: { b: 99 }, c: 3 });
});
it('returns subset of right hand side value when a key value has been deleted', () => {
expect(diff({ a: { b: 1 }, c: 2, d: { e: 100 } }, { a: { b: 1 }, c: 2, d: {} })).to.deep.equal({ d: { e: undefined } });
});
it('returns subset of right hand side value when a key value has been added', () => {
expect(diff({ a: 1 }, { a: 1, b: 2 })).to.deep.equal({ b: 2 });
});
it('returns keys as undefined when deleted from right hand side', () => {
expect(diff({ a: 1, b: { c: 2 }}, { a: 1 })).to.deep.equal({ b: undefined });
});
});
describe('arrays', () => {
it('returns right hand side value as object of indices to value when arrays are different', () => {
expect(diff([1], [2])).to.deep.equal({ 0: 2 });
});
it('returns subset of right hand side array as object of indices to value when arrays differs at multiple indicies', () => {
expect(diff([1, 2, 3], [9, 8, 3])).to.deep.equal({ 0: 9, 1: 8 });
});
it('returns subset of right hand side array as object of indices to value when right hand side array has deletions', () => {
expect(diff([1, 2, 3], [1, 3])).to.deep.equal({ 1: 3, 2: undefined });
});
it('returns subset of right hand side array as object of indices to value when right hand side array has additions', () => {
expect(diff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({ 3: 9 });
});
});
describe('date', () => {
const lhs = new Date('2016');
const rhs = new Date('2017');
it('returns right hand side date when updated', () => {
expect(diff({ date: lhs }, { date: rhs })).to.deep.equal({ date: rhs });
expect(diff([lhs], [rhs])).to.deep.equal({ 0: rhs });
});
it('returns undefined when date deleted', () => {
expect(diff({ date: lhs }, {})).to.deep.equal({ date: undefined });
expect(diff([lhs], [])).to.deep.equal({ 0: undefined });
});
it('returns right hand side when date is added', () => {
expect(diff({}, { date: rhs })).to.deep.equal({ date: rhs });
expect(diff([], [rhs])).to.deep.equal({ 0: rhs });
});
});
});
});