11/**
22 * Copyright 2018 Shift Devices AG
3+ * Copyright 2025 Shift Crypto AG
34 *
45 * Licensed under the Apache License, Version 2.0 (the "License");
56 * you may not use this file except in compliance with the License.
@@ -27,11 +28,20 @@ describe('equal', () => {
2728 expect ( equal ( null , null ) ) . toBeTruthy ( ) ;
2829 } ) ;
2930
31+ it ( 'compares undefined and null' , ( ) => {
32+ expect ( equal ( undefined , undefined ) ) . toBeTruthy ( ) ;
33+ expect ( equal ( undefined , null ) ) . toBeFalsy ( ) ;
34+ } ) ;
35+
3036 it ( 'compares ints' , ( ) => {
3137 expect ( equal ( 13 , 13 ) ) . toBeTruthy ( ) ;
3238 expect ( equal ( 1 , 13 ) ) . toBeFalsy ( ) ;
3339 } ) ;
3440
41+ it ( 'compares NaN' , ( ) => {
42+ expect ( equal ( NaN , NaN ) ) . toBeTruthy ( ) ;
43+ } ) ;
44+
3545 it ( 'compares strings' , ( ) => {
3646 expect ( equal ( 'foo' , 'foo' ) ) . toBeTruthy ( ) ;
3747 expect ( equal ( 'foo' , 'bar' ) ) . toBeFalsy ( ) ;
@@ -75,6 +85,13 @@ describe('equal', () => {
7585 expect ( equal ( a , b ) ) . toBeFalsy ( ) ;
7686 expect ( equal ( b , a ) ) . toBeFalsy ( ) ;
7787 } ) ;
88+
89+ it ( 'compares sparse array vs defined array' , ( ) => {
90+ // eslint-disable-next-line no-sparse-arrays
91+ expect ( equal ( [ 1 , , 3 ] , [ 1 , undefined , 3 ] ) ) . toBeFalsy ( ) ;
92+ // eslint-disable-next-line no-sparse-arrays
93+ expect ( equal ( [ 1 , , 3 ] , [ 1 , , 3 ] ) ) . toBeTruthy ( ) ;
94+ } ) ;
7895 } ) ;
7996
8097 describe ( 'objects' , ( ) => {
@@ -83,6 +100,10 @@ describe('equal', () => {
83100 expect ( equal ( null , { } ) ) . toBeFalsy ( ) ;
84101 } ) ;
85102
103+ it ( 'is false for [] and {}' , ( ) => {
104+ expect ( equal ( [ ] , { } ) ) . toBeFalsy ( ) ;
105+ } ) ;
106+
86107 it ( 'is true for same key/value pairs' , ( ) => {
87108 const a = { one : 'two' , three : 'four' } ;
88109 const b = { one : 'two' , three : 'four' } ;
@@ -114,5 +135,85 @@ describe('equal', () => {
114135 expect ( equal ( a , null ) ) . toBeFalsy ( ) ;
115136 expect ( equal ( null , a ) ) . toBeFalsy ( ) ;
116137 } ) ;
138+
139+ it ( 'doesn’t affect key order equality' , ( ) => {
140+ const a = { a : 1 , b : 2 } ;
141+ const b = { b : 2 , a : 1 } ;
142+ expect ( equal ( a , b ) ) . toBeTruthy ( ) ;
143+ } ) ;
144+
145+ it ( 'deep compares nested structures' , ( ) => {
146+ const a = { foo : [ 1 , { bar : 'baz' } ] } ;
147+ const b = { foo : [ 1 , { bar : 'baz' } ] } ;
148+ expect ( equal ( a , b ) ) . toBeTruthy ( ) ;
149+ const c = { foo : [ 1 , { bar : 'qux' } ] } ;
150+ expect ( equal ( a , c ) ) . toBeFalsy ( ) ;
151+ } ) ;
152+
153+ it ( 'fails on deep nested mismatch' , ( ) => {
154+ const a = { foo : { bar : { baz : 1 } } } ;
155+ const b = { foo : { bar : { baz : 2 } } } ;
156+ expect ( equal ( a , b ) ) . toBeFalsy ( ) ;
157+ } ) ;
158+
159+ it ( 'compares object with mixed value types' , ( ) => {
160+ const a = { num : 1 , str : 'x' , bool : true } ;
161+ const b = { num : 1 , str : 'x' , bool : true } ;
162+ expect ( equal ( a , b ) ) . toBeTruthy ( ) ;
163+ } ) ;
164+
165+ it ( 'returns false for two different Symbols with same description' , ( ) => {
166+ expect ( equal ( Symbol ( 'x' ) , Symbol ( 'x' ) ) ) . toBeFalsy ( ) ;
167+ } ) ;
168+
169+ it ( 'compares Symbols' , ( ) => {
170+ const s = Symbol ( 'x' ) ;
171+ expect ( equal ( s , s ) ) . toBeTruthy ( ) ;
172+ } ) ;
173+ } ) ;
174+
175+ describe ( 'RegExp, functions and dates' , ( ) => {
176+ it ( 'compares RegExp objects correctly' , ( ) => {
177+ expect ( equal ( / f o o / g, / f o o / g) ) . toBeTruthy ( ) ;
178+ expect ( equal ( / f o o / g, / b a r / g) ) . toBeFalsy ( ) ;
179+ } ) ;
180+
181+ it ( 'compares Date objects correctly' , ( ) => {
182+ expect ( equal ( new Date ( '2020-01-01' ) , new Date ( '2020-01-01' ) ) ) . toBeTruthy ( ) ;
183+ expect ( equal ( new Date ( '2020-01-01' ) , new Date ( '2021-01-01' ) ) ) . toBeFalsy ( ) ;
184+ } ) ;
185+
186+ it ( 'returns true only for same reference' , ( ) => {
187+ const a = ( ) => { } ;
188+ expect ( equal ( a , a ) ) . toBeTruthy ( ) ;
189+ } ) ;
190+
191+ it ( 'returns false for different functions' , ( ) => {
192+ const fn1 = ( ) => { } ;
193+ const fn2 = ( ) => { } ;
194+ expect ( equal ( fn1 , fn2 ) ) . toBeFalsy ( ) ;
195+ } ) ;
196+ } ) ;
197+ } ) ;
198+
199+ describe ( 'edge cases: array vs object structure' , ( ) => {
200+ it ( '[] vs {} is not equal' , ( ) => {
201+ expect ( equal ( [ ] , { } ) ) . toBeFalsy ( ) ;
202+ } ) ;
203+
204+ it ( 'empty array vs object with numeric key is not equal' , ( ) => {
205+ const arr : any = [ ] ;
206+ const obj = { 0 : undefined } ;
207+ expect ( equal ( arr , obj ) ) . toBeFalsy ( ) ;
208+ } ) ;
209+
210+ it ( 'array with undefined value vs object with matching key is not equal' , ( ) => {
211+ const arr = [ undefined ] ;
212+ const obj = { 0 : undefined } ;
213+ expect ( equal ( arr , obj ) ) . toBeFalsy ( ) ;
214+ } ) ;
215+
216+ it ( 'nested empty object vs array is not equal' , ( ) => {
217+ expect ( equal ( { foo : [ ] } , { foo : { } } ) ) . toBeFalsy ( ) ;
117218 } ) ;
118219} ) ;
0 commit comments