-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest.js
More file actions
79 lines (67 loc) · 1.26 KB
/
test.js
File metadata and controls
79 lines (67 loc) · 1.26 KB
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
import {default as diff, custom} from './index.js'
import test from 'tape'
test((t) => {
const a = {
speed: 4,
power: 54,
height: undefined,
level: 1,
}
const b = {
speed: 4, // unchanged
power: 22, // changed
level: undefined, // changed
weight: 10, // added
}
t.deepEqual(diff(a, b), {
power: 22,
level: undefined,
weight: 10,
})
const c = {
speed: 5, // changed
power: 54, // unchanged
level: 100, // changed
material: 'steel', // added
location: undefined, // added but undefined
}
t.deepEqual(diff(a, b, c), {
speed: 5,
power: 22,
level: 100,
weight: 10,
material: 'steel',
})
t.deepEqual(diff({}, {}), {})
t.end()
})
test('Custom equality', (t) => {
const created = '2016-04-24T10:39:23.419Z'
const now = new Date()
const a = {
created: new Date(created),
updated: new Date(created),
}
const b = {
created: new Date(created), // unchanged
updated: now, // changed
}
t.deepEqual(
diff(a, b),
{
created: new Date(created),
updated: now,
},
'expected default behavior',
)
t.deepEqual(custom(dateAwareComparator, a, b), {
updated: now,
})
t.end()
})
function dateAwareComparator(a, b) {
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime()
}
return a === b
}