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