Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import {
3 addToEnd,
4 addToStart,
5 isPlainArray,
6 isPlainObject,
7 matchMutation,
8 partialMatchKey,
9 replaceEqualDeep,
10 scheduleMicrotask,
11 sleep,
12} from '../utils'
13import { Mutation } from '../mutation'
14import { createQueryClient } from './utils'
15β
16describe('core/utils', () => {
17 describe('isPlainObject', () => {
18 it('should return `true` for a plain object', () => {
19 expect(isPlainObject({})).toEqual(true)
20 })
21β
22 it('should return `false` for an array', () => {
23 expect(isPlainObject([])).toEqual(false)
24 })
25β
26 it('should return `false` for null', () => {
27 expect(isPlainObject(null)).toEqual(false)
28 })
29β
30 it('should return `false` for undefined', () => {
31 expect(isPlainObject(undefined)).toEqual(false)
32 })
33β
34 it('should return `true` for object with an undefined constructor', () => {
35 expect(isPlainObject(Object.create(null))).toBeTruthy()
36 })
37β
38 it('should return `false` if constructor does not have an Object-specific method', () => {
39 class Foo {
40 abc: any
41 constructor() {
42 this.abc = {}
43 }
44 }
45 expect(isPlainObject(new Foo())).toBeFalsy()
46 })
47β
48 it('should return `false` if the object has a modified prototype', () => {
49 function Graph(this: any) {
50 this.vertices = []
51 this.edges = []
52 }
53β
54 Graph.prototype.addVertex = function (v: any) {
55 this.vertices.push(v)
56 }
57β
58 expect(isPlainObject(Object.create(Graph))).toBeFalsy()
59 })
60 })
61β
62 describe('isPlainArray', () => {
63 it('should return `true` for plain arrays', () => {
64 expect(isPlainArray([1, 2])).toEqual(true)
65 })
66β
67 it('should return `false` for non plain arrays', () => {
68 expect(isPlainArray(Object.assign([1, 2], { a: 'b' }))).toEqual(false)
69 })
70 })
71β
72 describe('partialMatchKey', () => {
73 it('should return `true` if a includes b', () => {
74 const a = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
75 const b = [{ a: { b: 'b' }, c: 'c', d: [] }]
76 expect(partialMatchKey(a, b)).toEqual(true)
77 })
78β
79 it('should return `false` if a does not include b', () => {
80 const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
81 const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
82 expect(partialMatchKey(a, b)).toEqual(false)
83 })
84β
85 it('should return `true` if array a includes array b', () => {
86 const a = [1, 2, 3]
87 const b = [1, 2]
88 expect(partialMatchKey(a, b)).toEqual(true)
89 })
90β
91 it('should return `false` if a is null and b is not', () => {
92 const a = [null]
93 const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
94 expect(partialMatchKey(a, b)).toEqual(false)
95 })
96β
97 it('should return `false` if a contains null and b is not', () => {
98 const a = [{ a: null, c: 'c', d: [] }]
99 const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
100 expect(partialMatchKey(a, b)).toEqual(false)
101 })
102β
103 it('should return `false` if b is null and a is not', () => {
104 const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
105 const b = [null]
106 expect(partialMatchKey(a, b)).toEqual(false)
107 })
108β
109 it('should return `false` if b contains null and a is not', () => {
110 const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
111 const b = [{ a: null, c: 'c', d: [{ d: 'd ' }] }]
112 expect(partialMatchKey(a, b)).toEqual(false)
113 })
114 })
115β
116 describe('replaceEqualDeep', () => {
117 it('should return the previous value when the next value is an equal primitive', () => {
118 expect(replaceEqualDeep(1, 1)).toBe(1)
119 expect(replaceEqualDeep('1', '1')).toBe('1')
120 expect(replaceEqualDeep(true, true)).toBe(true)
121 expect(replaceEqualDeep(false, false)).toBe(false)
122 expect(replaceEqualDeep(null, null)).toBe(null)
123 expect(replaceEqualDeep(undefined, undefined)).toBe(undefined)
124 })
125 it('should return the next value when the previous value is a different value', () => {
126 const date1 = new Date()
127 const date2 = new Date()
128 expect(replaceEqualDeep(1, 0)).toBe(0)
129 expect(replaceEqualDeep(1, 2)).toBe(2)
130 expect(replaceEqualDeep('1', '2')).toBe('2')
131 expect(replaceEqualDeep(true, false)).toBe(false)
132 expect(replaceEqualDeep(false, true)).toBe(true)
133 expect(replaceEqualDeep(date1, date2)).toBe(date2)
134 })
135β
136 it('should return the next value when the previous value is a different type', () => {
137 const array = [1]
138 const object = { a: 'a' }
139 expect(replaceEqualDeep(0, undefined)).toBe(undefined)
140 expect(replaceEqualDeep(undefined, 0)).toBe(0)
141 expect(replaceEqualDeep(2, undefined)).toBe(undefined)
142 expect(replaceEqualDeep(undefined, 2)).toBe(2)
143 expect(replaceEqualDeep(undefined, null)).toBe(null)
144 expect(replaceEqualDeep(null, undefined)).toBe(undefined)
145 expect(replaceEqualDeep({}, undefined)).toBe(undefined)
146 expect(replaceEqualDeep([], undefined)).toBe(undefined)
147 expect(replaceEqualDeep(array, object)).toBe(object)
148 expect(replaceEqualDeep(object, array)).toBe(array)
149 })
150β
151 it('should return the previous value when the next value is an equal array', () => {
152 const prev = [1, 2]
153 const next = [1, 2]
154 expect(replaceEqualDeep(prev, next)).toBe(prev)
155 })
156β
157 it('should return a copy when the previous value is a different array subset', () => {
158 const prev = [1, 2]
159 const next = [1, 2, 3]
160 const result = replaceEqualDeep(prev, next)
161 expect(result).toEqual(next)
162 expect(result).not.toBe(prev)
163 expect(result).not.toBe(next)
164 })
165β
166 it('should return a copy when the previous value is a different array superset', () => {
167 const prev = [1, 2, 3]
168 const next = [1, 2]
169 const result = replaceEqualDeep(prev, next)
170 expect(result).toEqual(next)
171 expect(result).not.toBe(prev)
172 expect(result).not.toBe(next)
173 })
174β
175 it('should return the previous value when the next value is an equal empty array', () => {
176 const prev: Array<any> = []
177 const next: Array<any> = []
178 expect(replaceEqualDeep(prev, next)).toBe(prev)
179 })
180β
181 it('should return the previous value when the next value is an equal empty object', () => {
182 const prev = {}
183 const next = {}
184 expect(replaceEqualDeep(prev, next)).toBe(prev)
185 })
186β
187 it('should return the previous value when the next value is an equal object', () => {
188 const prev = { a: 'a' }
189 const next = { a: 'a' }
190 expect(replaceEqualDeep(prev, next)).toBe(prev)
191 })
192β
193 it('should replace different values in objects', () => {
194 const prev = { a: { b: 'b' }, c: 'c' }
195 const next = { a: { b: 'b' }, c: 'd' }
196 const result = replaceEqualDeep(prev, next)
197 expect(result).toEqual(next)
198 expect(result).not.toBe(prev)
199 expect(result).not.toBe(next)
200 expect(result.a).toBe(prev.a)
201 expect(result.c).toBe(next.c)
202 })
203β
204 it('should replace different values in arrays', () => {
205 const prev = [1, { a: 'a' }, { b: { b: 'b' } }, [1]] as const
206 const next = [1, { a: 'a' }, { b: { b: 'c' } }, [1]] as const
207 const result = replaceEqualDeep(prev, next)
208 expect(result).toEqual(next)
209 expect(result).not.toBe(prev)
210 expect(result).not.toBe(next)
211 expect(result[0]).toBe(prev[0])
212 expect(result[1]).toBe(prev[1])
213 expect(result[2]).not.toBe(next[2])
214 expect(result[2].b.b).toBe(next[2].b.b)
215 expect(result[3]).toBe(prev[3])
216 })
217β
218 it('should replace different values in arrays when the next value is a subset', () => {
219 const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
220 const next = [{ a: 'a' }, { b: 'b' }]
221 const result = replaceEqualDeep(prev, next)
222 expect(result).toEqual(next)
223 expect(result).not.toBe(prev)
224 expect(result).not.toBe(next)
225 expect(result[0]).toBe(prev[0])
226 expect(result[1]).toBe(prev[1])
227 expect(result[2]).toBeUndefined()
228 })
229β
230 it('should replace different values in arrays when the next value is a superset', () => {
231 const prev = [{ a: 'a' }, { b: 'b' }]
232 const next = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
233 const result = replaceEqualDeep(prev, next)
234 expect(result).toEqual(next)
235 expect(result).not.toBe(prev)
236 expect(result).not.toBe(next)
237 expect(result[0]).toBe(prev[0])
238 expect(result[1]).toBe(prev[1])
239 expect(result[2]).toBe(next[2])
240 })
241β
242 it('should copy objects which are not arrays or objects', () => {
243 const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }, 1]
244 const next = [{ a: 'a' }, new Map(), { c: 'c' }, 2]
245 const result = replaceEqualDeep(prev, next)
246 expect(result).not.toBe(prev)
247 expect(result).not.toBe(next)
248 expect(result[0]).toBe(prev[0])
249 expect(result[1]).toBe(next[1])
250 expect(result[2]).toBe(prev[2])
251 expect(result[3]).toBe(next[3])
252 })
253β
254 it('should support equal objects which are not arrays or objects', () => {
255 const map = new Map()
256 const prev = [map, [1]]
257 const next = [map, [1]]
258 const result = replaceEqualDeep(prev, next)
259 expect(result).toBe(prev)
260 })
261β
262 it('should support non equal objects which are not arrays or objects', () => {
263 const map1 = new Map()
264 const map2 = new Map()
265 const prev = [map1, [1]]
266 const next = [map2, [1]]
267 const result = replaceEqualDeep(prev, next)
268 expect(result).not.toBe(prev)
269 expect(result).not.toBe(next)
270 expect(result[0]).toBe(next[0])
271 expect(result[1]).toBe(prev[1])
272 })
273β
274 it('should support objects which are not plain arrays', () => {
275 const prev = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
276 const next = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
277 const result = replaceEqualDeep(prev, next)
278 expect(result).toBe(next)
279 })
280β
281 it('should replace all parent objects if some nested value changes', () => {
282 const prev = {
283 todo: { id: '1', meta: { createdAt: 0 }, state: { done: false } },
284 otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
285 }
286 const next = {
287 todo: { id: '1', meta: { createdAt: 0 }, state: { done: true } },
288 otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
289 }
290 const result = replaceEqualDeep(prev, next)
291 expect(result).toEqual(next)
292 expect(result).not.toBe(prev)
293 expect(result).not.toBe(next)
294 expect(result.todo).not.toBe(prev.todo)
295 expect(result.todo).not.toBe(next.todo)
296 expect(result.todo.id).toBe(next.todo.id)
297 expect(result.todo.meta).toBe(prev.todo.meta)
298 expect(result.todo.state).not.toBe(next.todo.state)
299 expect(result.todo.state.done).toBe(next.todo.state.done)
300 expect(result.otherTodo).toBe(prev.otherTodo)
301 })
302β
303 it('should replace all parent arrays if some nested value changes', () => {
304 const prev = {
305 todos: [
306 { id: '1', meta: { createdAt: 0 }, state: { done: false } },
307 { id: '2', meta: { createdAt: 0 }, state: { done: true } },
308 ],
309 }
310 const next = {
311 todos: [
312 { id: '1', meta: { createdAt: 0 }, state: { done: true } },
313 { id: '2', meta: { createdAt: 0 }, state: { done: true } },
314 ],
315 }
316 const result = replaceEqualDeep(prev, next)
317 expect(result).toEqual(next)
318 expect(result).not.toBe(prev)
319 expect(result).not.toBe(next)
320 expect(result.todos).not.toBe(prev.todos)
321 expect(result.todos).not.toBe(next.todos)
322 expect(result.todos[0]).not.toBe(prev.todos[0])
323 expect(result.todos[0]).not.toBe(next.todos[0])
324 expect(result.todos[0]?.id).toBe(next.todos[0]?.id)
325 expect(result.todos[0]?.meta).toBe(prev.todos[0]?.meta)
326 expect(result.todos[0]?.state).not.toBe(next.todos[0]?.state)
327 expect(result.todos[0]?.state.done).toBe(next.todos[0]?.state.done)
328 expect(result.todos[1]).toBe(prev.todos[1])
329 })
330 })
331β
332 describe('matchMutation', () => {
333 it('should return false if mutationKey options is undefined', () => {
334 const filters = { mutationKey: ['key1'] }
335 const queryClient = createQueryClient()
336 const mutation = new Mutation({
337 mutationId: 1,
338 mutationCache: queryClient.getMutationCache(),
339 options: {},
340 })
341 expect(matchMutation(filters, mutation)).toBeFalsy()
342 })
343 })
344β
345 describe('scheduleMicrotask', () => {
346 it('should defer execution of callback', async () => {
347 const callback = vi.fn()
348β
349 scheduleMicrotask(callback)
350 expect(callback).not.toHaveBeenCalled()
351 await sleep(0)
352 expect(callback).toHaveBeenCalledTimes(1)
353 })
354 })
355β
356 describe('addToEnd', () => {
357 it('should add item to the end of the array', () => {
358 const items = [1, 2, 3]
359 const newItems = addToEnd(items, 4)
360 expect(newItems).toEqual([1, 2, 3, 4])
361 })
362β
363 it('should not exceed max if provided', () => {
364 const items = [1, 2, 3]
365 const newItems = addToEnd(items, 4, 3)
366 expect(newItems).toEqual([2, 3, 4])
367 })
368β
369 it('should add item to the end of the array when max = 0', () => {
370 const items = [1, 2, 3]
371 const item = 4
372 const max = 0
373 expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
374 })
375β
376 it('should add item to the end of the array when max is undefined', () => {
377 const items = [1, 2, 3]
378 const item = 4
379 const max = undefined
380 expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
381 })
382 })
383β
384 describe('addToStart', () => {
385 it('should add an item to the start of the array', () => {
386 const items = [1, 2, 3]
387 const item = 4
388 const newItems = addToStart(items, item)
389 expect(newItems).toEqual([4, 1, 2, 3])
390 })
391β
392 it('should respect the max argument', () => {
393 const items = [1, 2, 3]
394 const item = 4
395 const max = 2
396 const newItems = addToStart(items, item, max)
397 expect(newItems).toEqual([4, 1, 2])
398 })
399β
400 it('should not remove any items if max = 0', () => {
401 const items = [1, 2, 3]
402 const item = 4
403 const max = 0
404 const newItems = addToStart(items, item, max)
405 expect(newItems).toEqual([4, 1, 2, 3])
406 })
407β
408 it('should not remove any items if max is undefined', () => {
409 const items = [1, 2, 3]
410 const item = 4
411 const max = 0
412 const newItems = addToStart(items, item, max)
413 expect(newItems).toEqual([4, 1, 2, 3])
414 })
415 })
416})