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