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