|
| 1 | +// compareUtils.it.js |
| 2 | +import { describe, expect, it } from '@jest/globals'; |
| 3 | +import { isDeepEqual, omitDeep } from './compareUtils.js'; |
| 4 | + |
| 5 | +describe('isDeepEqual', () => { |
| 6 | + it('handles primitive values', () => { |
| 7 | + expect(isDeepEqual(1, 1)).toBe(true); |
| 8 | + expect(isDeepEqual('hello', 'hello')).toBe(true); |
| 9 | + expect(isDeepEqual(true, true)).toBe(true); |
| 10 | + expect(isDeepEqual(null, null)).toBe(true); |
| 11 | + expect(isDeepEqual(undefined, undefined)).toBe(true); |
| 12 | + |
| 13 | + expect(isDeepEqual(1, 2)).toBe(false); |
| 14 | + expect(isDeepEqual('hello', 'world')).toBe(false); |
| 15 | + expect(isDeepEqual(true, false)).toBe(false); |
| 16 | + expect(isDeepEqual(null, undefined)).toBe(false); |
| 17 | + expect(isDeepEqual(1, '1')).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + it('handles simple objects', () => { |
| 21 | + expect(isDeepEqual({}, {})).toBe(true); |
| 22 | + expect(isDeepEqual({ a: 1 }, { a: 1 })).toBe(true); |
| 23 | + expect(isDeepEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); |
| 24 | + |
| 25 | + expect(isDeepEqual({ a: 1 }, { a: 2 })).toBe(false); |
| 26 | + expect(isDeepEqual({ a: 1 }, { b: 1 })).toBe(false); |
| 27 | + expect(isDeepEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('handles arrays', () => { |
| 31 | + expect(isDeepEqual([], [])).toBe(true); |
| 32 | + expect(isDeepEqual([1, 2], [1, 2])).toBe(true); |
| 33 | + |
| 34 | + expect(isDeepEqual([1, 2], [2, 1])).toBe(false); |
| 35 | + expect(isDeepEqual([1, 2], [1, 2, 3])).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('handles nested objects', () => { |
| 39 | + expect(isDeepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } })).toBe(true); |
| 40 | + |
| 41 | + expect(isDeepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 3 } })).toBe(false); |
| 42 | + |
| 43 | + expect(isDeepEqual({ a: 1, b: { c: 2, d: 3 } }, { a: 1, b: { c: 2 } })).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('handles nested arrays', () => { |
| 47 | + expect(isDeepEqual({ a: [1, 2, { b: 3 }] }, { a: [1, 2, { b: 3 }] })).toBe(true); |
| 48 | + |
| 49 | + expect(isDeepEqual({ a: [1, 2, { b: 3 }] }, { a: [1, 2, { b: 4 }] })).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles mixed types', () => { |
| 53 | + expect(isDeepEqual({ a: 1 }, [1])).toBe(false); |
| 54 | + expect(isDeepEqual({ a: 1 }, null)).toBe(false); |
| 55 | + expect(isDeepEqual(null, { a: 1 })).toBe(false); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('omitDeep', () => { |
| 60 | + it('handles primitives', () => { |
| 61 | + expect(omitDeep(1, 'any')).toBe(1); |
| 62 | + expect(omitDeep('hello', 'any')).toBe('hello'); |
| 63 | + expect(omitDeep(null, 'any')).toBe(null); |
| 64 | + expect(omitDeep(undefined, 'any')).toBe(undefined); |
| 65 | + }); |
| 66 | + |
| 67 | + it('handles shallow objects', () => { |
| 68 | + expect(omitDeep({ a: 1, b: 2 }, 'a')).toEqual({ b: 2 }); |
| 69 | + expect(omitDeep({ a: 1, b: 2 }, 'c')).toEqual({ a: 1, b: 2 }); |
| 70 | + expect(omitDeep({ a: 1, b: 2 }, 'a', 'b')).toEqual({}); |
| 71 | + }); |
| 72 | + |
| 73 | + it('handles arrays', () => { |
| 74 | + expect( |
| 75 | + omitDeep( |
| 76 | + [ |
| 77 | + { a: 1, b: 2 }, |
| 78 | + { a: 3, b: 4 }, |
| 79 | + ], |
| 80 | + 'a' |
| 81 | + ) |
| 82 | + ).toEqual([{ b: 2 }, { b: 4 }]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('handles nested objects', () => { |
| 86 | + const input = { |
| 87 | + a: 1, |
| 88 | + b: { |
| 89 | + c: 2, |
| 90 | + d: 3, |
| 91 | + e: { |
| 92 | + f: 4, |
| 93 | + g: 5, |
| 94 | + }, |
| 95 | + }, |
| 96 | + h: 6, |
| 97 | + }; |
| 98 | + |
| 99 | + const expected = { |
| 100 | + a: 1, |
| 101 | + b: { |
| 102 | + d: 3, |
| 103 | + e: { |
| 104 | + g: 5, |
| 105 | + }, |
| 106 | + }, |
| 107 | + h: 6, |
| 108 | + }; |
| 109 | + |
| 110 | + expect(omitDeep(input, 'c', 'f')).toEqual(expected); |
| 111 | + }); |
| 112 | + |
| 113 | + it('handles deeply nested arrays', () => { |
| 114 | + const input = { |
| 115 | + items: [ |
| 116 | + { id: 1, name: 'item1', metadata: { created: '2023', readOnly: true } }, |
| 117 | + { id: 2, name: 'item2', metadata: { created: '2023', readOnly: true } }, |
| 118 | + ], |
| 119 | + }; |
| 120 | + |
| 121 | + const expected = { |
| 122 | + items: [ |
| 123 | + { id: 1, name: 'item1', metadata: { created: '2023' } }, |
| 124 | + { id: 2, name: 'item2', metadata: { created: '2023' } }, |
| 125 | + ], |
| 126 | + }; |
| 127 | + |
| 128 | + expect(omitDeep(input, 'readOnly')).toEqual(expected); |
| 129 | + }); |
| 130 | + |
| 131 | + it('handles complex schemas', () => { |
| 132 | + const schema = { |
| 133 | + type: 'object', |
| 134 | + properties: { |
| 135 | + name: { type: 'string' }, |
| 136 | + id: { type: 'string', readOnly: true }, |
| 137 | + details: { |
| 138 | + type: 'object', |
| 139 | + properties: { |
| 140 | + createdAt: { type: 'string', readOnly: true }, |
| 141 | + description: { type: 'string' }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + items: { |
| 145 | + type: 'array', |
| 146 | + items: { |
| 147 | + type: 'object', |
| 148 | + properties: { |
| 149 | + itemId: { type: 'string', readOnly: true }, |
| 150 | + itemName: { type: 'string' }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }; |
| 156 | + |
| 157 | + const expected = { |
| 158 | + type: 'object', |
| 159 | + properties: { |
| 160 | + name: { type: 'string' }, |
| 161 | + id: { type: 'string' }, |
| 162 | + details: { |
| 163 | + type: 'object', |
| 164 | + properties: { |
| 165 | + createdAt: { type: 'string' }, |
| 166 | + description: { type: 'string' }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + items: { |
| 170 | + type: 'array', |
| 171 | + items: { |
| 172 | + type: 'object', |
| 173 | + properties: { |
| 174 | + itemId: { type: 'string' }, |
| 175 | + itemName: { type: 'string' }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }; |
| 181 | + |
| 182 | + expect(omitDeep(schema, 'readOnly')).toEqual(expected); |
| 183 | + }); |
| 184 | + |
| 185 | + it('handles multiple keys to omit', () => { |
| 186 | + const input = { |
| 187 | + a: 1, |
| 188 | + b: 2, |
| 189 | + c: { |
| 190 | + d: 3, |
| 191 | + e: 4, |
| 192 | + f: { |
| 193 | + g: 5, |
| 194 | + h: 6, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }; |
| 198 | + |
| 199 | + expect(omitDeep(input, 'a', 'e', 'g')).toEqual({ |
| 200 | + b: 2, |
| 201 | + c: { |
| 202 | + d: 3, |
| 203 | + f: { |
| 204 | + h: 6, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments