Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.test.ts

726 lines18.6 KB

1import { describe, expect, it } from 'vitest'
2import { deleteNestedDataByPath, updateNestedDataByPath } from '../utils'
3​
4describe('Utils tests', () => {
5 describe('updatedNestedDataByPath', () => {
6 describe('array', () => {
7 it('should update data correctly', async () => {
8 const oldData = ['one', 'two', 'three']
9​
10 const newData = updateNestedDataByPath(oldData, ['1'], 'new')
11​
12 expect(newData).not.toBe(oldData) // should not be the same reference
13​
14 expect(oldData).toMatchInlineSnapshot(`
15 [
16 "one",
17 "two",
18 "three",
19 ]
20 `)
21 expect(newData).toMatchInlineSnapshot(`
22 [
23 "one",
24 "new",
25 "three",
26 ]
27 `)
28 })
29 })
30​
31 describe('object', () => {
32 it('should update data correctly', async () => {
33 const oldData = { title: 'Hello world', id: 1, createdAt: '2021-01-01' }
34​
35 const newData = updateNestedDataByPath(
36 oldData,
37 ['title'],
38 'Brave new world',
39 )
40​
41 expect(newData).not.toBe(oldData) // should not be the same reference
42​
43 expect(oldData).toMatchInlineSnapshot(`
44 {
45 "createdAt": "2021-01-01",
46 "id": 1,
47 "title": "Hello world",
48 }
49 `)
50 expect(newData).toMatchInlineSnapshot(`
51 {
52 "createdAt": "2021-01-01",
53 "id": 1,
54 "title": "Brave new world",
55 }
56 `)
57 })
58 })
59​
60 describe('set', () => {
61 it('should update data correctly', async () => {
62 const oldData = new Set([123, 321, 'hello', 'world'])
63​
64 const newData = updateNestedDataByPath(oldData, ['2'], 'hi')
65​
66 expect(newData).not.toBe(oldData) // should not be the same reference
67​
68 expect(oldData).toMatchInlineSnapshot(`
69 Set {
70 123,
71 321,
72 "hello",
73 "world",
74 }
75 `)
76 expect(newData).toMatchInlineSnapshot(`
77 Set {
78 123,
79 321,
80 "hi",
81 "world",
82 }
83 `)
84 })
85 })
86​
87 describe('map', () => {
88 it('should update data correctly', async () => {
89 const oldData = new Map([
90 ['en', 'hello'],
91 ['fr', 'bonjour'],
92 ])
93​
94 const newData = updateNestedDataByPath(oldData, ['fr'], 'salut')
95​
96 expect(newData).not.toBe(oldData) // should not be the same reference
97​
98 expect(oldData).toMatchInlineSnapshot(`
99 Map {
100 "en" => "hello",
101 "fr" => "bonjour",
102 }
103 `)
104 expect(newData).toMatchInlineSnapshot(`
105 Map {
106 "en" => "hello",
107 "fr" => "salut",
108 }
109 `)
110 })
111 })
112​
113 describe('nested data', () => {
114 it('should update data correctly', async () => {
115 const oldData = new Map([
116 [
117 'pumpkin-pie',
118 {
119 id: 1,
120 title: 'Pumpkin pie',
121 ingredients: new Set(['pumpkin', 'sugar', 'spices']),
122 steps: ['mix', 'bake', 'eat'],
123 translations: new Map([
124 ['en', 'Pumpkin pie'],
125 ['fr', 'Tarte à la citrouille'],
126 ]),
127 },
128 ],
129 [
130 'spaghetti-bolonese',
131 {
132 id: 2,
133 title: 'Spaghetti bolonese',
134 ingredients: new Set([
135 'spaghetti',
136 'tomato sauce',
137 'minced meat',
138 ]),
139 steps: ['cook', 'eat'],
140 translations: new Map([
141 ['en', 'Spaghetti bolonese'],
142 ['fr', 'Spaghetti bolonaise'],
143 ]),
144 },
145 ],
146 ])
147​
148 const updatedObject = updateNestedDataByPath(
149 oldData,
150 ['pumpkin-pie', 'title'],
151 'Pumpkin pie with whipped cream',
152 )
153​
154 const updatedArray = updateNestedDataByPath(
155 oldData,
156 ['spaghetti-bolonese', 'steps', '0'],
157 'prepare',
158 )
159​
160 const updatedSet = updateNestedDataByPath(
161 oldData,
162 ['pumpkin-pie', 'ingredients', '1'],
163 'honey',
164 )
165​
166 const updatedMap = updateNestedDataByPath(
167 oldData,
168 ['pumpkin-pie', 'translations', 'en'],
169 'Best pie ever',
170 )
171​
172 expect(oldData).toMatchInlineSnapshot(`
173 Map {
174 "pumpkin-pie" => {
175 "id": 1,
176 "ingredients": Set {
177 "pumpkin",
178 "sugar",
179 "spices",
180 },
181 "steps": [
182 "mix",
183 "bake",
184 "eat",
185 ],
186 "title": "Pumpkin pie",
187 "translations": Map {
188 "en" => "Pumpkin pie",
189 "fr" => "Tarte à la citrouille",
190 },
191 },
192 "spaghetti-bolonese" => {
193 "id": 2,
194 "ingredients": Set {
195 "spaghetti",
196 "tomato sauce",
197 "minced meat",
198 },
199 "steps": [
200 "cook",
201 "eat",
202 ],
203 "title": "Spaghetti bolonese",
204 "translations": Map {
205 "en" => "Spaghetti bolonese",
206 "fr" => "Spaghetti bolonaise",
207 },
208 },
209 }
210 `)
211​
212 expect(updatedObject).toMatchInlineSnapshot(`
213 Map {
214 "pumpkin-pie" => {
215 "id": 1,
216 "ingredients": Set {
217 "pumpkin",
218 "sugar",
219 "spices",
220 },
221 "steps": [
222 "mix",
223 "bake",
224 "eat",
225 ],
226 "title": "Pumpkin pie with whipped cream",
227 "translations": Map {
228 "en" => "Pumpkin pie",
229 "fr" => "Tarte à la citrouille",
230 },
231 },
232 "spaghetti-bolonese" => {
233 "id": 2,
234 "ingredients": Set {
235 "spaghetti",
236 "tomato sauce",
237 "minced meat",
238 },
239 "steps": [
240 "cook",
241 "eat",
242 ],
243 "title": "Spaghetti bolonese",
244 "translations": Map {
245 "en" => "Spaghetti bolonese",
246 "fr" => "Spaghetti bolonaise",
247 },
248 },
249 }
250 `)
251​
252 expect(updatedArray).toMatchInlineSnapshot(`
253 Map {
254 "pumpkin-pie" => {
255 "id": 1,
256 "ingredients": Set {
257 "pumpkin",
258 "sugar",
259 "spices",
260 },
261 "steps": [
262 "mix",
263 "bake",
264 "eat",
265 ],
266 "title": "Pumpkin pie",
267 "translations": Map {
268 "en" => "Pumpkin pie",
269 "fr" => "Tarte à la citrouille",
270 },
271 },
272 "spaghetti-bolonese" => {
273 "id": 2,
274 "ingredients": Set {
275 "spaghetti",
276 "tomato sauce",
277 "minced meat",
278 },
279 "steps": [
280 "prepare",
281 "eat",
282 ],
283 "title": "Spaghetti bolonese",
284 "translations": Map {
285 "en" => "Spaghetti bolonese",
286 "fr" => "Spaghetti bolonaise",
287 },
288 },
289 }
290 `)
291​
292 expect(updatedSet).toMatchInlineSnapshot(`
293 Map {
294 "pumpkin-pie" => {
295 "id": 1,
296 "ingredients": Set {
297 "pumpkin",
298 "honey",
299 "spices",
300 },
301 "steps": [
302 "mix",
303 "bake",
304 "eat",
305 ],
306 "title": "Pumpkin pie",
307 "translations": Map {
308 "en" => "Pumpkin pie",
309 "fr" => "Tarte à la citrouille",
310 },
311 },
312 "spaghetti-bolonese" => {
313 "id": 2,
314 "ingredients": Set {
315 "spaghetti",
316 "tomato sauce",
317 "minced meat",
318 },
319 "steps": [
320 "cook",
321 "eat",
322 ],
323 "title": "Spaghetti bolonese",
324 "translations": Map {
325 "en" => "Spaghetti bolonese",
326 "fr" => "Spaghetti bolonaise",
327 },
328 },
329 }
330 `)
331​
332 expect(updatedMap).toMatchInlineSnapshot(`
333 Map {
334 "pumpkin-pie" => {
335 "id": 1,
336 "ingredients": Set {
337 "pumpkin",
338 "sugar",
339 "spices",
340 },
341 "steps": [
342 "mix",
343 "bake",
344 "eat",
345 ],
346 "title": "Pumpkin pie",
347 "translations": Map {
348 "en" => "Best pie ever",
349 "fr" => "Tarte à la citrouille",
350 },
351 },
352 "spaghetti-bolonese" => {
353 "id": 2,
354 "ingredients": Set {
355 "spaghetti",
356 "tomato sauce",
357 "minced meat",
358 },
359 "steps": [
360 "cook",
361 "eat",
362 ],
363 "title": "Spaghetti bolonese",
364 "translations": Map {
365 "en" => "Spaghetti bolonese",
366 "fr" => "Spaghetti bolonaise",
367 },
368 },
369 }
370 `)
371 })
372 })
373 })
374​
375 describe('deleteNestedDataByPath', () => {
376 it('should delete item from array correctly', async () => {
377 const oldData = ['one', 'two', 'three']
378​
379 const newData = deleteNestedDataByPath(oldData, ['1'])
380​
381 expect(newData).not.toBe(oldData) // should not be the same reference
382​
383 expect(oldData).toMatchInlineSnapshot(`
384 [
385 "one",
386 "two",
387 "three",
388 ]
389 `)
390 expect(newData).toMatchInlineSnapshot(`
391 [
392 "one",
393 "three",
394 ]
395 `)
396 })
397​
398 it('should delete item from object correctly', async () => {
399 const oldData = { title: 'Hello world', id: 1, createdAt: '2021-01-01' }
400​
401 const newData = deleteNestedDataByPath(oldData, ['createdAt'])
402​
403 expect(newData).not.toBe(oldData) // should not be the same reference
404​
405 expect(oldData).toMatchInlineSnapshot(`
406 {
407 "createdAt": "2021-01-01",
408 "id": 1,
409 "title": "Hello world",
410 }
411 `)
412 expect(newData).toMatchInlineSnapshot(`
413 {
414 "id": 1,
415 "title": "Hello world",
416 }
417 `)
418 })
419​
420 it('should delete item from set', () => {
421 const oldData = new Set([123, 321, false, true])
422​
423 const newData = deleteNestedDataByPath(oldData, ['1'])
424​
425 expect(newData).not.toBe(oldData) // should not be the same reference
426​
427 expect(oldData).toMatchInlineSnapshot(`
428 Set {
429 123,
430 321,
431 false,
432 true,
433 }
434 `)
435 expect(newData).toMatchInlineSnapshot(`
436 Set {
437 123,
438 false,
439 true,
440 }
441 `)
442 })
443​
444 it('should delete item from map', () => {
445 const oldData = new Map([
446 ['123', 'one'],
447 ['hello', 'two'],
448 ['world', 'three'],
449 ])
450​
451 const newData = deleteNestedDataByPath(oldData, ['world'])
452​
453 expect(newData).not.toBe(oldData) // should not be the same reference
454​
455 expect(oldData).toMatchInlineSnapshot(`
456 Map {
457 "123" => "one",
458 "hello" => "two",
459 "world" => "three",
460 }
461 `)
462 expect(newData).toMatchInlineSnapshot(`
463 Map {
464 "123" => "one",
465 "hello" => "two",
466 }
467 `)
468 })
469​
470 describe('nested data', () => {
471 it('should delete nested items correctly', async () => {
472 const oldData = new Map([
473 [
474 'pumpkin-pie',
475 {
476 id: 1,
477 title: 'Pumpkin pie',
478 ingredients: new Set(['pumpkin', 'sugar', 'spices']),
479 steps: ['mix', 'bake', 'eat'],
480 translations: new Map([
481 ['en', 'Pumpkin pie'],
482 ['fr', 'Tarte à la citrouille'],
483 ]),
484 },
485 ],
486 [
487 'spaghetti-bolonese',
488 {
489 id: 2,
490 title: 'Spaghetti bolonese',
491 ingredients: new Set([
492 'spaghetti',
493 'tomato sauce',
494 'minced meat',
495 ]),
496 steps: ['cook', 'eat'],
497 translations: new Map([
498 ['en', 'Spaghetti bolonese'],
499 ['fr', 'Spaghetti bolonaise'],
500 ]),
501 },
502 ],
503 ])
504​
505 const deletedFromSet = deleteNestedDataByPath(oldData, [
506 'spaghetti-bolonese',
507 'ingredients',
508 '0',
509 ])
510​
511 const deletedFromArray = deleteNestedDataByPath(oldData, [
512 'pumpkin-pie',
513 'steps',
514 '1',
515 ])
516​
517 const deletedFromObject = deleteNestedDataByPath(oldData, [
518 'pumpkin-pie',
519 'title',
520 ])
521​
522 const deletedFromMap = deleteNestedDataByPath(oldData, [
523 'spaghetti-bolonese',
524 'translations',
525 'fr',
526 ])
527​
528 expect(oldData).toMatchInlineSnapshot(`
529 Map {
530 "pumpkin-pie" => {
531 "id": 1,
532 "ingredients": Set {
533 "pumpkin",
534 "sugar",
535 "spices",
536 },
537 "steps": [
538 "mix",
539 "bake",
540 "eat",
541 ],
542 "title": "Pumpkin pie",
543 "translations": Map {
544 "en" => "Pumpkin pie",
545 "fr" => "Tarte à la citrouille",
546 },
547 },
548 "spaghetti-bolonese" => {
549 "id": 2,
550 "ingredients": Set {
551 "spaghetti",
552 "tomato sauce",
553 "minced meat",
554 },
555 "steps": [
556 "cook",
557 "eat",
558 ],
559 "title": "Spaghetti bolonese",
560 "translations": Map {
561 "en" => "Spaghetti bolonese",
562 "fr" => "Spaghetti bolonaise",
563 },
564 },
565 }
566 `)
567​
568 expect(deletedFromSet).toMatchInlineSnapshot(`
569 Map {
570 "pumpkin-pie" => {
571 "id": 1,
572 "ingredients": Set {
573 "pumpkin",
574 "sugar",
575 "spices",
576 },
577 "steps": [
578 "mix",
579 "bake",
580 "eat",
581 ],
582 "title": "Pumpkin pie",
583 "translations": Map {
584 "en" => "Pumpkin pie",
585 "fr" => "Tarte à la citrouille",
586 },
587 },
588 "spaghetti-bolonese" => {
589 "id": 2,
590 "ingredients": Set {
591 "tomato sauce",
592 "minced meat",
593 },
594 "steps": [
595 "cook",
596 "eat",
597 ],
598 "title": "Spaghetti bolonese",
599 "translations": Map {
600 "en" => "Spaghetti bolonese",
601 "fr" => "Spaghetti bolonaise",
602 },
603 },
604 }
605 `)
606​
607 expect(deletedFromArray).toMatchInlineSnapshot(`
608 Map {
609 "pumpkin-pie" => {
610 "id": 1,
611 "ingredients": Set {
612 "pumpkin",
613 "sugar",
614 "spices",
615 },
616 "steps": [
617 "mix",
618 "eat",
619 ],
620 "title": "Pumpkin pie",
621 "translations": Map {
622 "en" => "Pumpkin pie",
623 "fr" => "Tarte à la citrouille",
624 },
625 },
626 "spaghetti-bolonese" => {
627 "id": 2,
628 "ingredients": Set {
629 "spaghetti",
630 "tomato sauce",
631 "minced meat",
632 },
633 "steps": [
634 "cook",
635 "eat",
636 ],
637 "title": "Spaghetti bolonese",
638 "translations": Map {
639 "en" => "Spaghetti bolonese",
640 "fr" => "Spaghetti bolonaise",
641 },
642 },
643 }
644 `)
645​
646 expect(deletedFromObject).toMatchInlineSnapshot(`
647 Map {
648 "pumpkin-pie" => {
649 "id": 1,
650 "ingredients": Set {
651 "pumpkin",
652 "sugar",
653 "spices",
654 },
655 "steps": [
656 "mix",
657 "bake",
658 "eat",
659 ],
660 "translations": Map {
661 "en" => "Pumpkin pie",
662 "fr" => "Tarte à la citrouille",
663 },
664 },
665 "spaghetti-bolonese" => {
666 "id": 2,
667 "ingredients": Set {
668 "spaghetti",
669 "tomato sauce",
670 "minced meat",
671 },
672 "steps": [
673 "cook",
674 "eat",
675 ],
676 "title": "Spaghetti bolonese",
677 "translations": Map {
678 "en" => "Spaghetti bolonese",
679 "fr" => "Spaghetti bolonaise",
680 },
681 },
682 }
683 `)
684​
685 expect(deletedFromMap).toMatchInlineSnapshot(`
686 Map {
687 "pumpkin-pie" => {
688 "id": 1,
689 "ingredients": Set {
690 "pumpkin",
691 "sugar",
692 "spices",
693 },
694 "steps": [
695 "mix",
696 "bake",
697 "eat",
698 ],
699 "title": "Pumpkin pie",
700 "translations": Map {
701 "en" => "Pumpkin pie",
702 "fr" => "Tarte à la citrouille",
703 },
704 },
705 "spaghetti-bolonese" => {
706 "id": 2,
707 "ingredients": Set {
708 "spaghetti",
709 "tomato sauce",
710 "minced meat",
711 },
712 "steps": [
713 "cook",
714 "eat",
715 ],
716 "title": "Spaghetti bolonese",
717 "translations": Map {
718 "en" => "Spaghetti bolonese",
719 },
720 },
721 }
722 `)
723 })
724 })
725 })
726})