Open History to see the last commit
1import { afterEach, describe, expect, it } from 'vitest'
2import { deepEqual, isPlainArray, replaceEqualDeep } from '../src/utils'
3β
4describe('replaceEqualDeep', () => {
5 it('should return the same object if the input objects are equal', () => {
6 const obj = { a: 1, b: 2 }
7 const result = replaceEqualDeep(obj, obj)
8 expect(result).toBe(obj)
9 })
10β
11 it('should return a new object with replaced values if the input objects are not equal', () => {
12 const obj1 = { a: 1, b: 2 }
13 const obj2 = { a: 1, b: 3 }
14 const result = replaceEqualDeep(obj1, obj2)
15 expect(result).toStrictEqual(obj2)
16 })
17β
18 it('should handle arrays correctly', () => {
19 const arr1 = [1, 2, 3]
20 const arr2 = [1, 2, 4]
21 const result = replaceEqualDeep(arr1, arr2)
22 expect(result).toStrictEqual(arr2)
23 })
24β
25 it('should handle nested objects correctly', () => {
26 const obj1 = { a: 1, b: { c: 2 } }
27 const obj2 = { a: 1, b: { c: 3 } }
28 const result = replaceEqualDeep(obj1, obj2)
29 expect(result).toStrictEqual(obj2)
30 })
31β
32 describe('symbol properties', () => {
33 it('should look at symbol properties in the object comparison', () => {
34 const propertyKey = Symbol('property')
35 const obj1 = { a: 1, [propertyKey]: 2 }
36 const obj2 = { a: 1, [propertyKey]: 3 }
37 const result = replaceEqualDeep(obj1, obj2)
38 expect(result).toStrictEqual(obj2)
39 })
40β
41 it('should copy over symbol properties when creating a new object', () => {
42 const propertyKey = Symbol('property')
43 const obj1 = { a: 1, [propertyKey]: 2 }
44 const obj2 = { a: 3, [propertyKey]: 2 }
45 const result = replaceEqualDeep(obj1, obj2)
46 expect(result).toStrictEqual(obj2)
47 })
48 })
49β
50 describe('non-enumerable properties', () => {
51 it('should treat objects with non-enumerable properties as non-plain (no need for property comparisons)', () => {
52 const obj1: { a: number; b?: number } = { a: 1 }
53 Object.defineProperty(obj1, 'b', { enumerable: false, value: 2 })
54 const obj2: { a: number; b?: number } = { a: 1 }
55 Object.defineProperty(obj2, 'b', { enumerable: false, value: 3 })
56 const result = replaceEqualDeep(obj1, obj2)
57 expect(result).toBe(obj2)
58 })
59β
60 it("should treat objects with non-enumerable properties as non-plain (copying doesn't happen)", () => {
61 const obj1: { a: number; b?: number } = { a: 1 }
62 Object.defineProperty(obj1, 'b', { enumerable: false, value: 2 })
63 const obj2: { a: number; b?: number } = { a: 3 }
64 Object.defineProperty(obj2, 'b', { enumerable: false, value: 2 })
65 const result = replaceEqualDeep(obj1, obj2)
66 expect(result).toBe(obj2)
67 })
68 })
69β
70 it('should properly handle non-existent keys', () => {
71 const obj1 = { a: 2, c: 123 }
72 const obj2 = { a: 2, c: 123, b: undefined }
73 const result = replaceEqualDeep(obj1, obj2)
74 expect(result).toStrictEqual(obj2)
75 })
76β
77 it('should correctly handle non-existent keys with the same number of fields', () => {
78 const obj1 = { a: 2, c: 123 }
79 const obj2 = { a: 2, b: undefined }
80 const result = replaceEqualDeep(obj1, obj2)
81 expect(result).toStrictEqual(obj2)
82 })
83β
84 it('should return the previous value when the next value is an equal primitive', () => {
85 expect(replaceEqualDeep(1, 1)).toBe(1)
86 expect(replaceEqualDeep('1', '1')).toBe('1')
87 expect(replaceEqualDeep(true, true)).toBe(true)
88 expect(replaceEqualDeep(false, false)).toBe(false)
89 expect(replaceEqualDeep(null, null)).toBe(null)
90 expect(replaceEqualDeep(undefined, undefined)).toBe(undefined)
91 })
92 it('should return the next value when the previous value is a different value', () => {
93 const date1 = new Date()
94 const date2 = new Date()
95 expect(replaceEqualDeep(1, 0)).toBe(0)
96 expect(replaceEqualDeep(1, 2)).toBe(2)
97 expect(replaceEqualDeep('1', '2')).toBe('2')
98 expect(replaceEqualDeep(true, false)).toBe(false)
99 expect(replaceEqualDeep(false, true)).toBe(true)
100 expect(replaceEqualDeep(date1, date2)).toBe(date2)
101 })
102β
103 it('should return the next value when the previous value is a different type', () => {
104 const array = [1]
105 const object = { a: 'a' }
106 expect(replaceEqualDeep(0, undefined)).toBe(undefined)
107 expect(replaceEqualDeep(undefined, 0)).toBe(0)
108 expect(replaceEqualDeep(2, undefined)).toBe(undefined)
109 expect(replaceEqualDeep(undefined, 2)).toBe(2)
110 expect(replaceEqualDeep(undefined, null)).toBe(null)
111 expect(replaceEqualDeep(null, undefined)).toBe(undefined)
112 expect(replaceEqualDeep({}, undefined)).toBe(undefined)
113 expect(replaceEqualDeep([], undefined)).toBe(undefined)
114 expect(replaceEqualDeep(array, object)).toBe(object)
115 expect(replaceEqualDeep(object, array)).toBe(array)
116 })
117β
118 it('should return the previous value when the next value is an equal array', () => {
119 const prev = [1, 2]
120 const next = [1, 2]
121 expect(replaceEqualDeep(prev, next)).toBe(prev)
122 })
123β
124 it('should return a copy when the previous value is a different array subset', () => {
125 const prev = [1, 2]
126 const next = [1, 2, 3]
127 const result = replaceEqualDeep(prev, next)
128 expect(result).toEqual(next)
129 expect(result).not.toBe(prev)
130 expect(result).not.toBe(next)
131 })
132β
133 it('should return a copy when the previous value is a different array superset', () => {
134 const prev = [1, 2, 3]
135 const next = [1, 2]
136 const result = replaceEqualDeep(prev, next)
137 expect(result).toEqual(next)
138 expect(result).not.toBe(prev)
139 expect(result).not.toBe(next)
140 })
141β
142 it('should return the previous value when the next value is an equal empty array', () => {
143 const prev: Array<any> = []
144 const next: Array<any> = []
145 expect(replaceEqualDeep(prev, next)).toBe(prev)
146 })
147β
148 it('should return the previous value when the next value is an equal empty object', () => {
149 const prev = {}
150 const next = {}
151 expect(replaceEqualDeep(prev, next)).toBe(prev)
152 })
153β
154 it('should return the previous value when the next value is an equal object', () => {
155 const prev = { a: 'a' }
156 const next = { a: 'a' }
157 expect(replaceEqualDeep(prev, next)).toBe(prev)
158 })
159β
160 it('should replace different values in objects', () => {
161 const prev = { a: { b: 'b' }, c: 'c' }
162 const next = { a: { b: 'b' }, c: 'd' }
163 const result = replaceEqualDeep(prev, next)
164 expect(result).toEqual(next)
165 expect(result).not.toBe(prev)
166 expect(result).not.toBe(next)
167 expect(result.a).toBe(prev.a)
168 expect(result.c).toBe(next.c)
169 })
170β
171 it('should replace different values in arrays', () => {
172 const prev = [1, { a: 'a' }, { b: { b: 'b' } }, [1]] as const
173 const next = [1, { a: 'a' }, { b: { b: 'c' } }, [1]] as const
174 const result = replaceEqualDeep(prev, next)
175 expect(result).toEqual(next)
176 expect(result).not.toBe(prev)
177 expect(result).not.toBe(next)
178 expect(result[0]).toBe(prev[0])
179 expect(result[1]).toBe(prev[1])
180 expect(result[2]).not.toBe(next[2])
181 expect(result[2].b.b).toBe(next[2].b.b)
182 expect(result[3]).toBe(prev[3])
183 })
184β
185 it('should replace different values in arrays when the next value is a subset', () => {
186 const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
187 const next = [{ a: 'a' }, { b: 'b' }]
188 const result = replaceEqualDeep(prev, next)
189 expect(result).toEqual(next)
190 expect(result).not.toBe(prev)
191 expect(result).not.toBe(next)
192 expect(result[0]).toBe(prev[0])
193 expect(result[1]).toBe(prev[1])
194 expect(result[2]).toBeUndefined()
195 })
196β
197 it('should replace different values in arrays when the next value is a superset', () => {
198 const prev = [{ a: 'a' }, { b: 'b' }]
199 const next = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
200 const result = replaceEqualDeep(prev, next)
201 expect(result).toEqual(next)
202 expect(result).not.toBe(prev)
203 expect(result).not.toBe(next)
204 expect(result[0]).toBe(prev[0])
205 expect(result[1]).toBe(prev[1])
206 expect(result[2]).toBe(next[2])
207 })
208β
209 it('should copy objects which are not arrays or objects', () => {
210 const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }, 1]
211 const next = [{ a: 'a' }, new Map(), { c: 'c' }, 2]
212 const result = replaceEqualDeep(prev, next)
213 expect(result).not.toBe(prev)
214 expect(result).not.toBe(next)
215 expect(result[0]).toBe(prev[0])
216 expect(result[1]).toBe(next[1])
217 expect(result[2]).toBe(prev[2])
218 expect(result[3]).toBe(next[3])
219 })
220β
221 it('should support equal objects which are not arrays or objects', () => {
222 const map = new Map()
223 const prev = [map, [1]]
224 const next = [map, [1]]
225 const result = replaceEqualDeep(prev, next)
226 expect(result).toBe(prev)
227 })
228β
229 it('should support non equal objects which are not arrays or objects', () => {
230 const map1 = new Map()
231 const map2 = new Map()
232 const prev = [map1, [1]]
233 const next = [map2, [1]]
234 const result = replaceEqualDeep(prev, next)
235 expect(result).not.toBe(prev)
236 expect(result).not.toBe(next)
237 expect(result[0]).toBe(next[0])
238 expect(result[1]).toBe(prev[1])
239 })
240β
241 it('should support objects which are not plain arrays', () => {
242 const prev = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
243 const next = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
244 const result = replaceEqualDeep(prev, next)
245 expect(result).toBe(next)
246 })
247β
248 it('should replace all parent objects if some nested value changes', () => {
249 const prev = {
250 todo: { id: '1', meta: { createdAt: 0 }, state: { done: false } },
251 otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
252 }
253 const next = {
254 todo: { id: '1', meta: { createdAt: 0 }, state: { done: true } },
255 otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
256 }
257 const result = replaceEqualDeep(prev, next)
258 expect(result).toEqual(next)
259 expect(result).not.toBe(prev)
260 expect(result).not.toBe(next)
261 expect(result.todo).not.toBe(prev.todo)
262 expect(result.todo).not.toBe(next.todo)
263 expect(result.todo.id).toBe(next.todo.id)
264 expect(result.todo.meta).toBe(prev.todo.meta)
265 expect(result.todo.state).not.toBe(next.todo.state)
266 expect(result.todo.state.done).toBe(next.todo.state.done)
267 expect(result.otherTodo).toBe(prev.otherTodo)
268 })
269β
270 it('should replace all parent arrays if some nested value changes', () => {
271 const prev = {
272 todos: [
273 { id: '1', meta: { createdAt: 0 }, state: { done: false } },
274 { id: '2', meta: { createdAt: 0 }, state: { done: true } },
275 ],
276 }
277 const next = {
278 todos: [
279 { id: '1', meta: { createdAt: 0 }, state: { done: true } },
280 { id: '2', meta: { createdAt: 0 }, state: { done: true } },
281 ],
282 }
283 const result = replaceEqualDeep(prev, next)
284 expect(result).toEqual(next)
285 expect(result).not.toBe(prev)
286 expect(result).not.toBe(next)
287 expect(result.todos).not.toBe(prev.todos)
288 expect(result.todos).not.toBe(next.todos)
289 expect(result.todos[0]).not.toBe(prev.todos[0])
290 expect(result.todos[0]).not.toBe(next.todos[0])
291 expect(result.todos[0]?.id).toBe(next.todos[0]?.id)
292 expect(result.todos[0]?.meta).toBe(prev.todos[0]?.meta)
293 expect(result.todos[0]?.state).not.toBe(next.todos[0]?.state)
294 expect(result.todos[0]?.state.done).toBe(next.todos[0]?.state.done)
295 expect(result.todos[1]).toBe(prev.todos[1])
296 })
297β
298 it('should be able to share values that contain undefined', () => {
299 const current = [
300 {
301 data: undefined,
302 foo: true,
303 },
304 ]
305β
306 const next = replaceEqualDeep(current, [
307 {
308 data: undefined,
309 foo: true,
310 },
311 ])
312β
313 expect(current).toBe(next)
314 })
315β
316 it('should return the previous value when both values are an array of undefined', () => {
317 const current = [undefined]
318 const next = replaceEqualDeep(current, [undefined])
319β
320 expect(next).toBe(current)
321 })
322β
323 it('should return the previous value when both values are an array that contains undefined', () => {
324 const current = [{ foo: 1 }, undefined]
325 const next = replaceEqualDeep(current, [{ foo: 1 }, undefined])
326β
327 expect(next).toBe(current)
328 })
329β
330 it('works w/ null prototype objects', () => {
331 const current = Object.create(null)
332 const next = Object.create(null)
333β
334 current.foo = 'bar'
335 next.foo = 'bar'
336 expect(replaceEqualDeep(current, next)).toBe(current)
337β
338 next.foo = 'baz'
339 expect(replaceEqualDeep(current, next)).toEqual(next)
340 })
341})
342β
343describe('isPlainArray', () => {
344 it('should return `true` for plain arrays', () => {
345 expect(isPlainArray([1, 2])).toEqual(true)
346 })
347β
348 it('should return `false` for non plain arrays', () => {
349 expect(isPlainArray(Object.assign([1, 2], { a: 'b' }))).toEqual(false)
350 })
351})
352β
353describe('deepEqual', () => {
354 describe.each([false, true])('partial = %s', (partial) => {
355 it('should return `true` for equal objects', () => {
356 const a = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
357 const b = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
358 expect(deepEqual(a, b, { partial })).toEqual(true)
359 expect(deepEqual(b, a, { partial })).toEqual(true)
360 })
361β
362 it('should return `false` for non equal objects', () => {
363 const a = { a: { b: 'b' }, c: 'c' }
364 const b = { a: { b: 'c' }, c: 'c' }
365 expect(deepEqual(a, b, { partial })).toEqual(false)
366 expect(deepEqual(b, a, { partial })).toEqual(false)
367 })
368β
369 it('should return `true` for equal objects and ignore `undefined` properties', () => {
370 const a = { a: 'a', b: undefined, c: 'c' }
371 const b = { a: 'a', c: 'c' }
372 expect(deepEqual(a, b, { partial })).toEqual(true)
373 expect(deepEqual(b, a, { partial })).toEqual(true)
374 })
375β
376 it('should return `true` for equal objects and ignore `undefined` nested properties', () => {
377 const a = { a: { b: 'b', x: undefined }, c: 'c' }
378 const b = { a: { b: 'b' }, c: 'c', d: undefined }
379 expect(deepEqual(a, b, { partial })).toEqual(true)
380 expect(deepEqual(b, a, { partial })).toEqual(true)
381 })
382β
383 it('should return `true` for equal arrays and ignore `undefined` object properties', () => {
384 const a = { a: { b: 'b' }, c: undefined }
385 const b = { a: { b: 'b' } }
386 expect(deepEqual([a], [b], { partial })).toEqual(true)
387 expect(deepEqual([b], [a], { partial })).toEqual(true)
388 })
389β
390 it('should return `true` for equal arrays and ignore nested `undefined` object properties', () => {
391 const a = { a: { b: 'b', x: undefined }, c: 'c' }
392 const b = { a: { b: 'b' }, c: 'c' }
393 expect(deepEqual([a], [b], { partial })).toEqual(true)
394 expect(deepEqual([b], [a], { partial })).toEqual(true)
395 })
396 })
397β
398 describe('ignoreUndefined = false', () => {
399 const ignoreUndefined = false
400 describe('partial = false', () => {
401 const partial = false
402 it('should return `false` for objects', () => {
403 const a = { a: { b: 'b', x: undefined }, c: 'c' }
404 const b = { a: { b: 'b' }, c: 'c', d: undefined }
405 expect(deepEqual(a, b, { partial, ignoreUndefined })).toEqual(false)
406 expect(deepEqual(b, a, { partial, ignoreUndefined })).toEqual(false)
407 })
408β
409 it('should return `false` for arrays', () => {
410 const a = { a: { b: 'b', x: undefined }, c: 'c' }
411 const b = { a: { b: 'b' }, c: 'c' }
412 expect(deepEqual([a], [b], { partial, ignoreUndefined })).toEqual(false)
413 expect(deepEqual([b], [a], { partial, ignoreUndefined })).toEqual(false)
414 })
415 })
416 describe('partial = true', () => {
417 const partial = true
418 it('should return `true` for objects', () => {
419 const a = { a: { b: 'b' }, c: 'c' }
420 const b = { a: { b: 'b' }, c: 'c', d: undefined }
421 expect(deepEqual(a, b, { partial, ignoreUndefined })).toEqual(true)
422 expect(deepEqual(b, a, { partial, ignoreUndefined })).toEqual(true)
423 })
424β
425 it('should return `true` for arrays', () => {
426 const a = { a: { b: 'b', x: undefined }, c: 'c' }
427 const b = { a: { b: 'b' }, c: 'c' }
428 expect(deepEqual([a], [b], { partial, ignoreUndefined })).toEqual(true)
429 expect(deepEqual([b], [a], { partial, ignoreUndefined })).toEqual(true)
430 })
431 })
432 })
433β
434 describe('partial comparison', () => {
435 it('correctly compares partially equal objects', () => {
436 const a = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
437 const b = { a: { b: 'b' }, c: 'c' }
438 expect(deepEqual(a, b, { partial: true })).toEqual(true)
439 expect(deepEqual(b, a, { partial: true })).toEqual(false)
440 })
441β
442 it('correctly compares partially equal objects and ignores `undefined` object properties', () => {
443 const a = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }], e: undefined }
444 const b = { a: { b: 'b' }, c: 'c', d: undefined }
445 expect(deepEqual(a, b, { partial: true })).toEqual(true)
446 expect(deepEqual(b, a, { partial: true })).toEqual(false)
447 })
448 })
449β
450 // This might not be what we want, but this test documents how things are now
451 describe('symbol and non-enumerable properties are not handled', () => {
452 it.fails(
453 'should return `false` for unequal objects with symbol properties',
454 () => {
455 const key = Symbol('foo')
456 const a = { [key]: 1 }
457 const b = { [key]: 2 }
458 expect(deepEqual(a, b)).toEqual(false)
459 },
460 )
461β
462 it.fails(
463 'should return `false` for unequal objects with non-enumerable properties',
464 () => {
465 const a = {}
466 Object.defineProperty(a, 'prop', { value: 1, enumerable: false })
467 const b = {}
468 Object.defineProperty(b, 'prop', { value: 2, enumerable: false })
469 expect(deepEqual(a, b)).toEqual(false)
470 },
471 )
472 })
473β
474 // We voluntarily fail in this case, because users should not do it, and ignoring it enables some performance improvements
475 describe('augmented object prototype fail case (no one should do this anyway)', () => {
476 it.fails(
477 'should not compare objects with augmented prototype properties',
478 () => {
479 // @ts-expect-error -- typescript is right to complain here, don't do this!
480 Object.prototype.x = 'x'
481 const a = { a: 1 }
482 const b = { a: 1 }
483 expect(deepEqual(a, b, { ignoreUndefined: false })).toEqual(true)
484 },
485 )
486β
487 afterEach(() => {
488 // it's probably not necessary to clean this up because vitest isolates tests
489 // but just in case isolation ever gets disabled, we clean the prototype to avoid disturbing other tests
490 // @ts-expect-error
491 delete Object.prototype.x
492 })
493 })
494})