|
| 1 | +import test, { expect } from '@playwright/test' |
| 2 | +import { objectsAreEqual } from '../../packages/core/src/objectUtils' |
| 3 | + |
| 4 | +test.describe('objectUtils.ts', () => { |
| 5 | + test.describe('objectsAreEqual', () => { |
| 6 | + test.describe('basic equality', () => { |
| 7 | + test('returns true for identical objects', () => { |
| 8 | + const obj1 = { a: 1, b: 2, c: 3 } |
| 9 | + const obj2 = { a: 1, b: 2, c: 3 } |
| 10 | + |
| 11 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 12 | + }) |
| 13 | + |
| 14 | + test('returns true for same reference', () => { |
| 15 | + const obj = { a: 1, b: 2 } |
| 16 | + |
| 17 | + expect(objectsAreEqual(obj, obj, [])).toBe(true) |
| 18 | + }) |
| 19 | + |
| 20 | + test('returns true for empty objects', () => { |
| 21 | + expect(objectsAreEqual({}, {}, [])).toBe(true) |
| 22 | + }) |
| 23 | + |
| 24 | + test('returns false for objects with different values', () => { |
| 25 | + const obj1 = { a: 1, b: 2 } |
| 26 | + const obj2 = { a: 1, b: 3 } |
| 27 | + |
| 28 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + test.describe('asymmetric objects', () => { |
| 33 | + test('returns false when second object has additional keys', () => { |
| 34 | + const obj1 = { a: 1, b: 2 } |
| 35 | + const obj2 = { a: 1, b: 2, c: 3 } |
| 36 | + |
| 37 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 38 | + }) |
| 39 | + |
| 40 | + test('returns false when first object has additional keys', () => { |
| 41 | + const obj1 = { a: 1, b: 2, c: 3 } |
| 42 | + const obj2 = { a: 1, b: 2 } |
| 43 | + |
| 44 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 45 | + }) |
| 46 | + |
| 47 | + test('handles objects with overlapping and unique keys', () => { |
| 48 | + const obj1 = { |
| 49 | + name: 'test', |
| 50 | + type: 'example', |
| 51 | + config: { option: 'value' }, |
| 52 | + } |
| 53 | + |
| 54 | + const obj2 = { |
| 55 | + name: 'test', |
| 56 | + type: 'example', |
| 57 | + config: { option: 'value' }, |
| 58 | + extra: ['additional'], |
| 59 | + } |
| 60 | + |
| 61 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + test.describe('key exclusion', () => { |
| 66 | + test('ignores excluded keys', () => { |
| 67 | + const obj1 = { a: 1, b: 2, ignore: 'foo' } |
| 68 | + const obj2 = { a: 1, b: 2, ignore: 'bar' } |
| 69 | + |
| 70 | + expect(objectsAreEqual(obj1, obj2, ['ignore'])).toBe(true) |
| 71 | + }) |
| 72 | + |
| 73 | + test('does not ignore non-excluded keys', () => { |
| 74 | + const obj1 = { a: 1, b: 2, keep: 'foo' } |
| 75 | + const obj2 = { a: 1, b: 2, keep: 'bar' } |
| 76 | + |
| 77 | + // @ts-expect-error - Ignore key doesn't exist in obj1 |
| 78 | + expect(objectsAreEqual(obj1, obj2, ['ignore'])).toBe(false) |
| 79 | + }) |
| 80 | + |
| 81 | + test('handles asymmetric objects with excluded keys', () => { |
| 82 | + const obj1 = { a: 1, b: 2 } |
| 83 | + const obj2 = { a: 1, b: 2, ignore: 'value' } |
| 84 | + |
| 85 | + // Should be true because the extra key is excluded |
| 86 | + // @ts-expect-error - Ignore key doesn't exist in obj1 |
| 87 | + expect(objectsAreEqual(obj1, obj2, ['ignore'])).toBe(true) |
| 88 | + }) |
| 89 | + |
| 90 | + test('handles asymmetric objects with non-excluded keys', () => { |
| 91 | + const obj1 = { a: 1, b: 2 } |
| 92 | + const obj2 = { a: 1, b: 2, keep: 'value' } |
| 93 | + |
| 94 | + // Should be false because the extra key is not excluded |
| 95 | + // @ts-expect-error - Keep key doesn't exist in obj1 |
| 96 | + expect(objectsAreEqual(obj1, obj2, ['ignore'])).toBe(false) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + test.describe('value types', () => { |
| 101 | + test('handles undefined values', () => { |
| 102 | + const obj1 = { a: 1, b: undefined } |
| 103 | + const obj2 = { a: 1, b: undefined } |
| 104 | + |
| 105 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 106 | + }) |
| 107 | + |
| 108 | + test('handles null values', () => { |
| 109 | + const obj1 = { a: 1, b: null } |
| 110 | + const obj2 = { a: 1, b: null } |
| 111 | + |
| 112 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 113 | + }) |
| 114 | + |
| 115 | + test('distinguishes between undefined and null', () => { |
| 116 | + const obj1 = { a: 1, b: undefined } |
| 117 | + const obj2 = { a: 1, b: null } |
| 118 | + |
| 119 | + // @ts-expect-error - Different types for key 'b' |
| 120 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 121 | + }) |
| 122 | + |
| 123 | + test('handles boolean values', () => { |
| 124 | + const obj1 = { a: true, b: false } |
| 125 | + const obj2 = { a: true, b: false } |
| 126 | + |
| 127 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 128 | + }) |
| 129 | + |
| 130 | + test('handles string values', () => { |
| 131 | + const obj1 = { a: 'hello', b: 'world' } |
| 132 | + const obj2 = { a: 'hello', b: 'world' } |
| 133 | + |
| 134 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 135 | + }) |
| 136 | + |
| 137 | + test('handles array values', () => { |
| 138 | + const obj1 = { a: [1, 2, 3], b: ['x', 'y'] } |
| 139 | + const obj2 = { a: [1, 2, 3], b: ['x', 'y'] } |
| 140 | + |
| 141 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 142 | + }) |
| 143 | + |
| 144 | + test('detects different array values', () => { |
| 145 | + const obj1 = { a: [1, 2, 3] } |
| 146 | + const obj2 = { a: [1, 2, 4] } |
| 147 | + |
| 148 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 149 | + }) |
| 150 | + |
| 151 | + test('handles nested objects', () => { |
| 152 | + const obj1 = { a: 1, nested: { x: 1, y: 2 } } |
| 153 | + const obj2 = { a: 1, nested: { x: 1, y: 2 } } |
| 154 | + |
| 155 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 156 | + }) |
| 157 | + |
| 158 | + test('detects different nested objects', () => { |
| 159 | + const obj1 = { a: 1, nested: { x: 1, y: 2 } } |
| 160 | + const obj2 = { a: 1, nested: { x: 1, y: 3 } } |
| 161 | + |
| 162 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 163 | + }) |
| 164 | + |
| 165 | + test('handles functions', () => { |
| 166 | + const fn = () => 'test' |
| 167 | + const obj1 = { a: 1, fn } |
| 168 | + const obj2 = { a: 1, fn } |
| 169 | + |
| 170 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(true) |
| 171 | + }) |
| 172 | + |
| 173 | + test('detects different functions', () => { |
| 174 | + const obj1 = { a: 1, fn: () => 'test1' } |
| 175 | + const obj2 = { a: 1, fn: () => 'test2' } |
| 176 | + |
| 177 | + expect(objectsAreEqual(obj1, obj2, [])).toBe(false) |
| 178 | + }) |
| 179 | + }) |
| 180 | + }) |
| 181 | +}) |
0 commit comments