Skip to content

Commit f5935ce

Browse files
committed
frontend: update equal tests
Added various tests with some llm help.
1 parent b642aef commit f5935ce

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

frontends/web/src/utils/equal.test.tsx

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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();
@@ -83,6 +93,10 @@ describe('equal', () => {
8393
expect(equal(null, {})).toBeFalsy();
8494
});
8595

96+
it('is false for [] and {}', () => {
97+
expect(equal([], {})).toBeFalsy();
98+
});
99+
86100
it('is true for same key/value pairs', () => {
87101
const a = { one: 'two', three: 'four' };
88102
const b = { one: 'two', three: 'four' };
@@ -115,16 +129,43 @@ describe('equal', () => {
115129
expect(equal(null, a)).toBeFalsy();
116130
});
117131

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+
118138
it('deep compares nested structures', () => {
119139
const a = { foo: [1, { bar: 'baz' }] };
120140
const b = { foo: [1, { bar: 'baz' }] };
121141
expect(equal(a, b)).toBeTruthy();
142+
const c = { foo: [1, { bar: 'qux' }] };
143+
expect(equal(a, c)).toBeFalsy();
122144
});
123145

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+
});
125157

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+
});
127167

168+
describe('RegExp, functions and dates', () => {
128169
it('compares RegExp objects correctly', () => {
129170
expect(equal(/foo/g, /foo/g)).toBeTruthy();
130171
expect(equal(/foo/g, /bar/g)).toBeFalsy();
@@ -135,12 +176,37 @@ describe('equal', () => {
135176
expect(equal(new Date('2020-01-01'), new Date('2021-01-01'))).toBeFalsy();
136177
});
137178

138-
it('does not consider functions equal', () => {
179+
it('returns true only for same reference', () => {
139180
const a = () => {};
140-
const b = () => {};
141-
expect(equal(a, b)).toBeFalsy();
181+
expect(equal(a, a)).toBeTruthy();
142182
});
143183

184+
it('returns false for different functions', () => {
185+
const fn1 = () => {};
186+
const fn2 = () => {};
187+
expect(equal(fn1, fn2)).toBeFalsy();
188+
});
144189
});
145-
146190
});
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

Comments
 (0)