-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
133 lines (108 loc) · 3.46 KB
/
index.ts
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
131
132
133
// Copied from https://github.com/facebook/fbjs/blob/7da8335b78d669cba263760872f0a45ed16b4d12/packages/fbjs/src/core/__tests__/shallowEqual-test.js
import test = require('tape');
import { shallowEqualExplain } from '../src/index';
import {
Explanation,
ObjectDifferentExplanation,
PropertyExplanation,
} from '../src/types';
test('returns false if either argument is null', t => {
t.deepEqual(
shallowEqualExplain(null, {}),
Explanation.ObjectDifferent({
explanation: ObjectDifferentExplanation.NotObjectOrNull({}),
}),
);
t.deepEqual(
shallowEqualExplain({}, null),
Explanation.ObjectDifferent({
explanation: ObjectDifferentExplanation.NotObjectOrNull({}),
}),
);
t.end();
});
test('returns true if both arguments are null or undefined', t => {
t.deepEqual(shallowEqualExplain(null, null), Explanation.ObjectSame({}));
t.deepEqual(
shallowEqualExplain(undefined, undefined),
Explanation.ObjectSame({}),
);
t.end();
});
test('returns true if arguments are not objects and are equal', t => {
t.deepEqual(shallowEqualExplain(1, 1), Explanation.ObjectSame({}));
t.end();
});
test('returns true if arguments are objects and are equal', t => {
const objA = {};
const objB = objA;
t.deepEqual(shallowEqualExplain(objA, objB), Explanation.ObjectSame({}));
t.end();
});
test('returns true if arguments are shallow equal', t => {
t.deepEqual(
shallowEqualExplain({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }),
Explanation.PropertiesSame({}),
);
t.end();
});
test('returns true when comparing NaN', t => {
t.deepEqual(shallowEqualExplain(NaN, NaN), Explanation.ObjectSame({}));
t.deepEqual(
shallowEqualExplain(
{ a: 1, b: 2, c: 3, d: NaN },
{ a: 1, b: 2, c: 3, d: NaN },
),
Explanation.PropertiesSame({}),
);
t.end();
});
test('returns false if arguments are not objects and not equal', t => {
t.deepEqual(
shallowEqualExplain(1, 2),
Explanation.ObjectDifferent({
explanation: ObjectDifferentExplanation.NotObjectOrNull({}),
}),
);
t.end();
});
test('returns false if only one argument is not an object', t => {
t.deepEqual(
shallowEqualExplain(1, {}),
Explanation.ObjectDifferent({
explanation: ObjectDifferentExplanation.NotObjectOrNull({}),
}),
);
t.end();
});
test('returns false if first argument has too many keys', t => {
t.deepEqual(
shallowEqualExplain({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 }),
Explanation.ObjectDifferent({
explanation: ObjectDifferentExplanation.NonMatchingKeys({}),
}),
);
t.end();
});
test('returns false if second argument has too many keys', t => {
t.deepEqual(
shallowEqualExplain({ a: 1, b: 2 }, { a: 1, b: 2, c: 3 }),
Explanation.ObjectDifferent({
explanation: ObjectDifferentExplanation.NonMatchingKeys({}),
}),
);
t.end();
});
test('returns false if arguments are not shallow equal', t => {
t.deepEqual(
shallowEqualExplain({ a: 1, b: 2, c: {} }, { a: 1, b: 2, c: {} }),
Explanation.PropertiesDifferent({
explanation: {
a: PropertyExplanation.Same({}),
b: PropertyExplanation.Same({}),
c: PropertyExplanation.Different({}),
},
}),
);
t.end();
});