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