1
1
/**
2
2
* Copyright 2018 Shift Devices AG
3
+ * Copyright 2025 Shift Crypto AG
3
4
*
4
5
* Licensed under the Apache License, Version 2.0 (the "License");
5
6
* you may not use this file except in compliance with the License.
@@ -27,11 +28,20 @@ describe('equal', () => {
27
28
expect ( equal ( null , null ) ) . toBeTruthy ( ) ;
28
29
} ) ;
29
30
31
+ it ( 'compares undefined and null' , ( ) => {
32
+ expect ( equal ( undefined , undefined ) ) . toBeTruthy ( ) ;
33
+ expect ( equal ( undefined , null ) ) . toBeFalsy ( ) ;
34
+ } ) ;
35
+
30
36
it ( 'compares ints' , ( ) => {
31
37
expect ( equal ( 13 , 13 ) ) . toBeTruthy ( ) ;
32
38
expect ( equal ( 1 , 13 ) ) . toBeFalsy ( ) ;
33
39
} ) ;
34
40
41
+ it ( 'compares NaN' , ( ) => {
42
+ expect ( equal ( NaN , NaN ) ) . toBeTruthy ( ) ;
43
+ } ) ;
44
+
35
45
it ( 'compares strings' , ( ) => {
36
46
expect ( equal ( 'foo' , 'foo' ) ) . toBeTruthy ( ) ;
37
47
expect ( equal ( 'foo' , 'bar' ) ) . toBeFalsy ( ) ;
@@ -83,6 +93,10 @@ describe('equal', () => {
83
93
expect ( equal ( null , { } ) ) . toBeFalsy ( ) ;
84
94
} ) ;
85
95
96
+ it ( 'is false for [] and {}' , ( ) => {
97
+ expect ( equal ( [ ] , { } ) ) . toBeFalsy ( ) ;
98
+ } ) ;
99
+
86
100
it ( 'is true for same key/value pairs' , ( ) => {
87
101
const a = { one : 'two' , three : 'four' } ;
88
102
const b = { one : 'two' , three : 'four' } ;
@@ -115,16 +129,43 @@ describe('equal', () => {
115
129
expect ( equal ( null , a ) ) . toBeFalsy ( ) ;
116
130
} ) ;
117
131
132
+ it ( 'doesn’t affect key order equality' , ( ) => {
133
+ const a = { a : 1 , b : 2 } ;
134
+ const b = { b : 2 , a : 1 } ;
135
+ expect ( equal ( a , b ) ) . toBeTruthy ( ) ;
136
+ } ) ;
137
+
118
138
it ( 'deep compares nested structures' , ( ) => {
119
139
const a = { foo : [ 1 , { bar : 'baz' } ] } ;
120
140
const b = { foo : [ 1 , { bar : 'baz' } ] } ;
121
141
expect ( equal ( a , b ) ) . toBeTruthy ( ) ;
142
+ const c = { foo : [ 1 , { bar : 'qux' } ] } ;
143
+ expect ( equal ( a , c ) ) . toBeFalsy ( ) ;
122
144
} ) ;
123
145
124
- } ) ;
146
+ it ( 'fails on deep nested mismatch' , ( ) => {
147
+ const a = { foo : { bar : { baz : 1 } } } ;
148
+ const b = { foo : { bar : { baz : 2 } } } ;
149
+ expect ( equal ( a , b ) ) . toBeFalsy ( ) ;
150
+ } ) ;
151
+
152
+ it ( 'compares object with mixed value types' , ( ) => {
153
+ const a = { num : 1 , str : 'x' , bool : true } ;
154
+ const b = { num : 1 , str : 'x' , bool : true } ;
155
+ expect ( equal ( a , b ) ) . toBeTruthy ( ) ;
156
+ } ) ;
125
157
126
- describe ( 'RegExp, functions and dates are currently not supported' , ( ) => {
158
+ it ( 'returns false for two different Symbols with same description' , ( ) => {
159
+ expect ( equal ( Symbol ( 'x' ) , Symbol ( 'x' ) ) ) . toBeFalsy ( ) ;
160
+ } ) ;
161
+
162
+ it ( 'compares Symbols' , ( ) => {
163
+ const s = Symbol ( 'x' ) ;
164
+ expect ( equal ( s , s ) ) . toBeTruthy ( ) ;
165
+ } ) ;
166
+ } ) ;
127
167
168
+ describe ( 'RegExp, functions and dates' , ( ) => {
128
169
it ( 'compares RegExp objects correctly' , ( ) => {
129
170
expect ( equal ( / f o o / g, / f o o / g) ) . toBeTruthy ( ) ;
130
171
expect ( equal ( / f o o / g, / b a r / g) ) . toBeFalsy ( ) ;
@@ -135,12 +176,37 @@ describe('equal', () => {
135
176
expect ( equal ( new Date ( '2020-01-01' ) , new Date ( '2021-01-01' ) ) ) . toBeFalsy ( ) ;
136
177
} ) ;
137
178
138
- it ( 'does not consider functions equal ' , ( ) => {
179
+ it ( 'returns true only for same reference ' , ( ) => {
139
180
const a = ( ) => { } ;
140
- const b = ( ) => { } ;
141
- expect ( equal ( a , b ) ) . toBeFalsy ( ) ;
181
+ expect ( equal ( a , a ) ) . toBeTruthy ( ) ;
142
182
} ) ;
143
183
184
+ it ( 'returns false for different functions' , ( ) => {
185
+ const fn1 = ( ) => { } ;
186
+ const fn2 = ( ) => { } ;
187
+ expect ( equal ( fn1 , fn2 ) ) . toBeFalsy ( ) ;
188
+ } ) ;
144
189
} ) ;
145
-
146
190
} ) ;
191
+
192
+ describe ( 'edge cases: array vs object structure' , ( ) => {
193
+ it ( '[] vs {} is not equal' , ( ) => {
194
+ expect ( equal ( [ ] , { } ) ) . toBeFalsy ( ) ;
195
+ } ) ;
196
+
197
+ it ( 'empty array vs object with numeric key is not equal' , ( ) => {
198
+ const arr : any = [ ] ;
199
+ const obj = { 0 : undefined } ;
200
+ expect ( equal ( arr , obj ) ) . toBeFalsy ( ) ;
201
+ } ) ;
202
+
203
+ it ( 'array with undefined value vs object with matching key is not equal' , ( ) => {
204
+ const arr = [ undefined ] ;
205
+ const obj = { 0 : undefined } ;
206
+ expect ( equal ( arr , obj ) ) . toBeFalsy ( ) ;
207
+ } ) ;
208
+
209
+ it ( 'nested empty object vs array is not equal' , ( ) => {
210
+ expect ( equal ( { foo : [ ] } , { foo : { } } ) ) . toBeFalsy ( ) ;
211
+ } ) ;
212
+ } ) ;
0 commit comments