@mic-rexjs/usecases-react 2.12.1 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +3 -12
  2. package/dist/index.min.js +1 -1
  3. package/es/hooks/useContext/index.d.ts +2 -2
  4. package/es/hooks/useContextualItem/index.js +0 -1
  5. package/es/hooks/useEntity/index.d.ts +2 -3
  6. package/es/hooks/useEntity/index.js +19 -72
  7. package/es/hooks/useFullArguments/types.d.ts +3 -3
  8. package/es/hooks/useIsRenderingRef/index.d.ts +1 -1
  9. package/es/hooks/useProvider/index.d.ts +2 -2
  10. package/es/hooks/useReducers/index.d.ts +6 -0
  11. package/es/hooks/useReducers/index.js +40 -0
  12. package/es/hooks/useStatuses/index.d.ts +2 -2
  13. package/es/hooks/useStatuses/index.js +3 -1
  14. package/es/hooks/useStore/index.d.ts +6 -0
  15. package/es/hooks/useStore/index.js +68 -0
  16. package/es/hooks/useUseCase/index.js +11 -47
  17. package/es/hooks/useUseCase/index.test.js +222 -107
  18. package/es/hooks/useUseCase/types.d.ts +3 -4
  19. package/es/methods/initStore/index.d.ts +3 -0
  20. package/es/methods/initStore/index.js +23 -0
  21. package/es/types.d.ts +5 -1
  22. package/es/usecases/contextUseCase/types.d.ts +12 -12
  23. package/es/usecases/entityRuntimeUseCase/index.d.ts +2 -0
  24. package/es/usecases/entityRuntimeUseCase/index.js +16 -0
  25. package/es/usecases/entityRuntimeUseCase/types.d.ts +8 -0
  26. package/es/usecases/entityRuntimeUseCase/types.js +1 -0
  27. package/es/usecases/valueUseCase/index.js +1 -1
  28. package/lib/hooks/useContextualItem/index.js +0 -1
  29. package/lib/hooks/useEntity/index.js +19 -73
  30. package/lib/hooks/useReducers/index.js +50 -0
  31. package/lib/hooks/useStatuses/index.js +3 -1
  32. package/lib/hooks/useStore/index.js +75 -0
  33. package/lib/hooks/useUseCase/index.js +12 -48
  34. package/lib/hooks/useUseCase/index.test.js +283 -126
  35. package/lib/methods/initStore/index.js +29 -0
  36. package/lib/usecases/entityRuntimeUseCase/index.js +25 -0
  37. package/lib/usecases/entityRuntimeUseCase/types.js +5 -0
  38. package/lib/usecases/valueUseCase/index.js +1 -1
  39. package/package.json +3 -3
  40. package/es/hooks/useRuntimeEntity/index.d.ts +0 -5
  41. package/es/hooks/useRuntimeEntity/index.js +0 -22
  42. package/lib/hooks/useRuntimeEntity/index.js +0 -28
@@ -3,7 +3,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { useUseCase } from '.';
4
4
  import { useDepsKey } from '../useDepsKey';
5
5
  import { beforeEach, describe, expect, jest, test } from '@jest/globals';
6
- import { entityUseCase, objectUseCase } from '@mic-rexjs/usecases';
6
+ import { entityUseCase } from '@mic-rexjs/usecases';
7
7
  import { fireEvent, screen } from '@testing-library/dom';
8
8
  import { act, render, renderHook } from '@testing-library/react';
9
9
  import { useDeepCompareEffect, useMemoizedFn, useMount, useUpdate, useUpdateEffect } from 'ahooks';
@@ -22,7 +22,7 @@ const defaultFile = {
22
22
  };
23
23
  const fileUseCase = function () {
24
24
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
25
- const entityReducers = objectUseCase();
25
+ const entityReducers = entityUseCase();
26
26
  const _options$pathPrefix = options.pathPrefix,
27
27
  pathPrefix = _options$pathPrefix === void 0 ? '' : _options$pathPrefix,
28
28
  onPathChange = options.onPathChange;
@@ -85,7 +85,7 @@ const mathUseCase = function () {
85
85
  };
86
86
  };
87
87
  const fieldPathUseCase = () => {
88
- return objectUseCase();
88
+ return entityUseCase();
89
89
  };
90
90
  const Child = _ref => {
91
91
  let _ref$textPrefix = _ref.textPrefix,
@@ -283,7 +283,7 @@ describe('useUseCase', () => {
283
283
  path: PATH_2
284
284
  });
285
285
  });
286
- test('Should support entity getter', () => {
286
+ test('Should support entity initializer', () => {
287
287
  const file = defaultFile;
288
288
  const _renderHook6 = renderHook(() => {
289
289
  return useUseCase(() => {
@@ -295,26 +295,117 @@ describe('useUseCase', () => {
295
295
  currentFile = _result$current[0];
296
296
  expect(currentFile).toEqual(file);
297
297
  });
298
- test('Entity getter should provide the current entity', () => {
298
+ test('Should support entity initializer with generator', () => {
299
+ const file = defaultFile;
300
+ const onChange = jest.fn();
301
+ const _renderHook7 = renderHook(() => {
302
+ return useUseCase(function () {
303
+ let currentFile = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultFile;
304
+ return function* () {
305
+ yield {
306
+ ...currentFile,
307
+ size: 5000
308
+ };
309
+ }();
310
+ }, fileUseCase, {
311
+ onChange
312
+ });
313
+ }),
314
+ result = _renderHook7.result;
315
+ const _result$current2 = _slicedToArray(result.current, 1),
316
+ currentFile = _result$current2[0];
317
+ expect(currentFile).toEqual({
318
+ ...file,
319
+ size: 5000
320
+ });
321
+ expect(onChange).toHaveBeenCalledTimes(0);
322
+ });
323
+ test('Should support entity initializer with async callback generator', async () => {
324
+ const file = defaultFile;
325
+ const onChange = jest.fn();
326
+ const onUpate = jest.fn();
327
+ const _Promise$withResolver = Promise.withResolvers(),
328
+ promise = _Promise$withResolver.promise,
329
+ resolve = _Promise$withResolver.resolve;
330
+ const _renderHook8 = renderHook(() => {
331
+ useEffect(() => {
332
+ onUpate();
333
+ });
334
+ return useUseCase(function () {
335
+ let entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultFile;
336
+ return function* () {
337
+ const newEntity1 = yield {
338
+ ...entity,
339
+ size: 1
340
+ };
341
+ // 在异步之前,就算多次 `yield`,也不会触发 `onChange`
342
+ yield {
343
+ ...newEntity1,
344
+ size: newEntity1.size + 1
345
+ };
346
+ const newEntity2 = yield async currentEntity => {
347
+ await promise;
348
+ return {
349
+ ...currentEntity,
350
+ size: currentEntity.size + 1
351
+ };
352
+ };
353
+ yield {
354
+ ...newEntity2,
355
+ size: newEntity2.size + 1
356
+ };
357
+ }();
358
+ }, fileUseCase, {
359
+ onChange
360
+ });
361
+ }),
362
+ result = _renderHook8.result;
363
+ const _result$current3 = _slicedToArray(result.current, 1),
364
+ currentFile1 = _result$current3[0];
365
+ const newFile1 = {
366
+ ...file,
367
+ size: 2
368
+ };
369
+ expect(currentFile1).toEqual(newFile1);
370
+ expect(onChange).toHaveBeenCalledTimes(0);
371
+ expect(onUpate).toHaveBeenCalledTimes(1);
372
+ await act(async () => {
373
+ resolve();
374
+ await promise;
375
+ });
376
+ const newFile2 = {
377
+ ...file,
378
+ size: 3
379
+ };
380
+ const newFile3 = {
381
+ ...file,
382
+ size: 4
383
+ };
384
+ expect(onChange).toHaveBeenCalledTimes(2);
385
+ expect(onUpate).toHaveBeenCalledTimes(2);
386
+ expect(onChange).toHaveBeenNthCalledWith(1, newFile2, newFile1);
387
+ expect(onChange).toHaveBeenNthCalledWith(2, newFile3, newFile2);
388
+ });
389
+ test('Entity initializer should provide the current entity', () => {
299
390
  let updateIndex = 0;
300
391
  const file = defaultFile;
301
392
  const onFile = jest.fn();
302
- const _renderHook7 = renderHook(() => {
393
+ const _renderHook9 = renderHook(() => {
303
394
  return useUseCase(currentFile => {
304
395
  onFile(currentFile);
305
396
  return currentFile !== null && currentFile !== void 0 ? currentFile : file;
306
397
  }, fileUseCase, {}, [updateIndex++]);
307
398
  }),
308
- result = _renderHook7.result;
309
- const _result$current2 = _slicedToArray(result.current, 2),
310
- setPath1 = _result$current2[1].setPath;
399
+ result = _renderHook9.result;
400
+ const _result$current4 = _slicedToArray(result.current, 2),
401
+ setPath1 = _result$current4[1].setPath;
311
402
  expect(onFile).toHaveBeenCalledTimes(1);
312
403
  expect(onFile).toHaveBeenCalledWith(void 0);
313
404
  act(() => {
314
405
  setPath1(PATH_1);
315
406
  });
316
- const _result$current3 = _slicedToArray(result.current, 2),
317
- setPath2 = _result$current3[1].setPath;
407
+ const _result$current5 = _slicedToArray(result.current, 2),
408
+ setPath2 = _result$current5[1].setPath;
318
409
  expect(onFile).toHaveBeenCalledTimes(2);
319
410
  expect(onFile).toHaveBeenCalledWith({
320
411
  ...file,
@@ -331,12 +422,12 @@ describe('useUseCase', () => {
331
422
  path: PATH_2
332
423
  });
333
424
  });
334
- test('Entity getter should provide the changed deps', () => {
425
+ test('Entity initializer should provide the changed deps', () => {
335
426
  let updateIndex = 0;
336
427
  const file = defaultFile;
337
428
  const symbol = Symbol('changedDep');
338
429
  const onChangedDeps = jest.fn();
339
- const _renderHook8 = renderHook(() => {
430
+ const _renderHook0 = renderHook(() => {
340
431
  return useUseCase(function () {
341
432
  let currentFile = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : file;
342
433
  let changedDeps = arguments.length > 1 ? arguments[1] : undefined;
@@ -365,9 +456,9 @@ describe('useUseCase', () => {
365
456
  return currentFile;
366
457
  }, fileUseCase, {}, [updateIndex++, 0, updateIndex++, 'xyz', updateIndex++, false]);
367
458
  }),
368
- result = _renderHook8.result;
369
- const _result$current4 = _slicedToArray(result.current, 2),
370
- setPath = _result$current4[1].setPath;
459
+ result = _renderHook0.result;
460
+ const _result$current6 = _slicedToArray(result.current, 2),
461
+ setPath = _result$current6[1].setPath;
371
462
  expect(onChangedDeps).toHaveBeenCalledTimes(1);
372
463
  expect(onChangedDeps).toHaveBeenCalledWith([symbol, symbol, symbol, symbol, symbol, symbol]);
373
464
  act(() => {
@@ -384,8 +475,8 @@ describe('useUseCase', () => {
384
475
  return {};
385
476
  }
386
477
  };
387
- const _renderHook9 = renderHook(() => {
388
- const collection = useUseCase(data, objectUseCase);
478
+ const _renderHook1 = renderHook(() => {
479
+ const collection = useUseCase(data, entityUseCase);
389
480
  const _collection3 = _slicedToArray(collection, 1),
390
481
  obj = _collection3[0].obj;
391
482
  useMemo(() => {
@@ -394,7 +485,7 @@ describe('useUseCase', () => {
394
485
  }, [obj]);
395
486
  return collection;
396
487
  }),
397
- rerender = _renderHook9.rerender;
488
+ rerender = _renderHook1.rerender;
398
489
  expect(updateTimes).toEqual(1);
399
490
  act(() => {
400
491
  rerender();
@@ -409,8 +500,8 @@ describe('useUseCase', () => {
409
500
  return {};
410
501
  }
411
502
  };
412
- const _renderHook0 = renderHook(() => {
413
- const collection = useUseCase(data, objectUseCase);
503
+ const _renderHook10 = renderHook(() => {
504
+ const collection = useUseCase(data, entityUseCase);
414
505
  const _collection4 = _slicedToArray(collection, 1),
415
506
  obj = _collection4[0].obj;
416
507
  useMemo(() => {
@@ -419,7 +510,7 @@ describe('useUseCase', () => {
419
510
  }, [obj]);
420
511
  return collection;
421
512
  }),
422
- result = _renderHook0.result;
513
+ result = _renderHook10.result;
423
514
  const current = result.current;
424
515
  const _current = _slicedToArray(current, 2),
425
516
  setEntity = _current[1].setEntity;
@@ -431,11 +522,11 @@ describe('useUseCase', () => {
431
522
  });
432
523
  expect(updateTimes).toEqual(2);
433
524
  });
434
- test('Entity getter should provide the first argument with current entity when deps has change.', () => {
525
+ test('Entity initializer should provide the first argument with current entity when deps has change.', () => {
435
526
  let entityArg;
436
527
  let times = 0;
437
528
  const file = defaultFile;
438
- const _renderHook1 = renderHook(() => {
529
+ const _renderHook11 = renderHook(() => {
439
530
  return useUseCase(function () {
440
531
  let entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : file;
441
532
  const path = entity.path;
@@ -446,7 +537,7 @@ describe('useUseCase', () => {
446
537
  };
447
538
  }, fileUseCase, {}, [times++]);
448
539
  }),
449
- result = _renderHook1.result;
540
+ result = _renderHook11.result;
450
541
  const collection1 = result.current;
451
542
  const _collection5 = _slicedToArray(collection1, 2),
452
543
  currentFile1 = _collection5[0],
@@ -472,20 +563,20 @@ describe('useUseCase', () => {
472
563
  });
473
564
  });
474
565
  test('`reducers` should be returned as an object', () => {
475
- const _renderHook10 = renderHook(() => {
566
+ const _renderHook12 = renderHook(() => {
476
567
  return useUseCase(defaultFile, fileUseCase);
477
568
  }),
478
- result = _renderHook10.result;
569
+ result = _renderHook12.result;
479
570
  const cores = result.current;
480
571
  const _cores2 = _slicedToArray(cores, 2),
481
572
  reducers = _cores2[1];
482
573
  expect(typeof reducers).toBe('object');
483
574
  });
484
575
  test('`Provider` should be returned as a function', () => {
485
- const _renderHook11 = renderHook(() => {
576
+ const _renderHook13 = renderHook(() => {
486
577
  return useUseCase(defaultFile, fileUseCase);
487
578
  }),
488
- result = _renderHook11.result;
579
+ result = _renderHook13.result;
489
580
  const cores = result.current;
490
581
  const _cores3 = _slicedToArray(cores, 3),
491
582
  Provider = _cores3[2];
@@ -493,11 +584,11 @@ describe('useUseCase', () => {
493
584
  });
494
585
  test('`usecase` should be called only once if deps has not provided', () => {
495
586
  const mockUseCase = jest.fn(fileUseCase);
496
- const _renderHook12 = renderHook(() => {
587
+ const _renderHook14 = renderHook(() => {
497
588
  return useUseCase(defaultFile, mockUseCase);
498
589
  }),
499
- result = _renderHook12.result,
500
- rerender = _renderHook12.rerender;
590
+ result = _renderHook14.result,
591
+ rerender = _renderHook14.rerender;
501
592
  const cores = result.current;
502
593
  const _cores4 = _slicedToArray(cores, 2),
503
594
  setEntity = _cores4[1].setEntity;
@@ -512,11 +603,11 @@ describe('useUseCase', () => {
512
603
  });
513
604
  test('`usecase` should be called only once if deps has not changed', () => {
514
605
  const mockUseCase = jest.fn(fileUseCase);
515
- const _renderHook13 = renderHook(() => {
606
+ const _renderHook15 = renderHook(() => {
516
607
  return useUseCase(defaultFile, mockUseCase, {}, [1, 'x']);
517
608
  }),
518
- result = _renderHook13.result,
519
- rerender = _renderHook13.rerender;
609
+ result = _renderHook15.result,
610
+ rerender = _renderHook15.rerender;
520
611
  const cores = result.current;
521
612
  const _cores5 = _slicedToArray(cores, 2),
522
613
  setEntity = _cores5[1].setEntity;
@@ -532,11 +623,11 @@ describe('useUseCase', () => {
532
623
  test('`usecase` should be called if deps has changed', () => {
533
624
  let i = 0;
534
625
  const mockUseCase = jest.fn(fileUseCase);
535
- const _renderHook14 = renderHook(() => {
626
+ const _renderHook16 = renderHook(() => {
536
627
  return useUseCase(defaultFile, mockUseCase, {}, [i++, 'x']);
537
628
  }),
538
- result = _renderHook14.result,
539
- rerender = _renderHook14.rerender;
629
+ result = _renderHook16.result,
630
+ rerender = _renderHook16.rerender;
540
631
  const cores = result.current;
541
632
  const _cores6 = _slicedToArray(cores, 2),
542
633
  setEntity = _cores6[1].setEntity;
@@ -553,7 +644,7 @@ describe('useUseCase', () => {
553
644
  test('after call non-generator reducer, it should not trigger update', () => {
554
645
  const onUpdate = jest.fn();
555
646
  const onDeepUpdate = jest.fn();
556
- const _renderHook15 = renderHook(() => {
647
+ const _renderHook17 = renderHook(() => {
557
648
  const context = useUseCase(defaultFile, fileUseCase);
558
649
  const _context = _slicedToArray(context, 2),
559
650
  reducers = _context[1];
@@ -573,7 +664,7 @@ describe('useUseCase', () => {
573
664
  update();
574
665
  };
575
666
  }),
576
- result = _renderHook15.result;
667
+ result = _renderHook17.result;
577
668
  act(() => {
578
669
  result.current();
579
670
  });
@@ -587,13 +678,13 @@ describe('useUseCase', () => {
587
678
  });
588
679
  test('after `yield entity`, it should trigger update', () => {
589
680
  const onUpdate = jest.fn();
590
- const _renderHook16 = renderHook(() => {
681
+ const _renderHook18 = renderHook(() => {
591
682
  useUpdateEffect(() => {
592
683
  onUpdate();
593
684
  });
594
685
  return useUseCase(defaultFile, fileUseCase);
595
686
  }),
596
- result = _renderHook16.result;
687
+ result = _renderHook18.result;
597
688
  const cores = result.current;
598
689
  const _cores7 = _slicedToArray(cores, 2),
599
690
  entity = _cores7[0],
@@ -620,13 +711,13 @@ describe('useUseCase', () => {
620
711
  });
621
712
  test('after `yield entity` on async mode, it should trigger update', async () => {
622
713
  const onUpdate = jest.fn();
623
- const _renderHook17 = renderHook(() => {
714
+ const _renderHook19 = renderHook(() => {
624
715
  useUpdateEffect(() => {
625
716
  onUpdate();
626
717
  });
627
718
  return useUseCase(defaultFile, fileUseCase);
628
719
  }),
629
- result = _renderHook17.result;
720
+ result = _renderHook19.result;
630
721
  const cores = result.current;
631
722
  const _cores8 = _slicedToArray(cores, 2),
632
723
  entity = _cores8[0],
@@ -649,13 +740,13 @@ describe('useUseCase', () => {
649
740
  });
650
741
  test('after `yield entity` callbacks, it should trigger update', () => {
651
742
  const onUpdate = jest.fn();
652
- const _renderHook18 = renderHook(() => {
743
+ const _renderHook20 = renderHook(() => {
653
744
  useUpdateEffect(() => {
654
745
  onUpdate();
655
746
  });
656
747
  return useUseCase(defaultFile, fileUseCase);
657
748
  }),
658
- result = _renderHook18.result;
749
+ result = _renderHook20.result;
659
750
  const cores = result.current;
660
751
  const _cores9 = _slicedToArray(cores, 2),
661
752
  reducers = _cores9[1];
@@ -796,12 +887,12 @@ describe('useUseCase', () => {
796
887
  });
797
888
  test('`options.onChange` should be trigger when entity has changed', () => {
798
889
  const onChange = jest.fn();
799
- const _renderHook19 = renderHook(() => {
890
+ const _renderHook21 = renderHook(() => {
800
891
  return useUseCase(defaultFile, fileUseCase, {
801
892
  onChange
802
893
  });
803
894
  }),
804
- result = _renderHook19.result;
895
+ result = _renderHook21.result;
805
896
  const cores = result.current;
806
897
  const _cores0 = _slicedToArray(cores, 2),
807
898
  reducers = _cores0[1];
@@ -821,12 +912,12 @@ describe('useUseCase', () => {
821
912
  const onChange1 = jest.fn();
822
913
  const onChange2 = jest.fn();
823
914
  let onChange = onChange1;
824
- const _renderHook20 = renderHook(() => {
915
+ const _renderHook22 = renderHook(() => {
825
916
  return useUseCase(defaultFile, fileUseCase, {
826
917
  onChange
827
918
  }, []);
828
919
  }),
829
- result = _renderHook20.result;
920
+ result = _renderHook22.result;
830
921
  const cores = result.current;
831
922
  const _cores1 = _slicedToArray(cores, 2),
832
923
  reducers = _cores1[1];
@@ -859,7 +950,7 @@ describe('useUseCase', () => {
859
950
  });
860
951
  });
861
952
  test('`options.options` should override rest options', () => {
862
- const _renderHook21 = renderHook(() => {
953
+ const _renderHook23 = renderHook(() => {
863
954
  return useUseCase(defaultFile, fileUseCase, {
864
955
  pathPrefix: 'xyz/',
865
956
  options: {
@@ -867,7 +958,7 @@ describe('useUseCase', () => {
867
958
  }
868
959
  });
869
960
  }),
870
- result = _renderHook21.result;
961
+ result = _renderHook23.result;
871
962
  const cores = result.current;
872
963
  const _cores10 = _slicedToArray(cores, 2),
873
964
  path = _cores10[0].path,
@@ -880,16 +971,16 @@ describe('useUseCase', () => {
880
971
  expect(result.current[0].path).toBe('123/' + PATH_1);
881
972
  });
882
973
  test('Option functions: use `T` (not `S`) when `S extends T`', () => {
883
- const testObjectUseCase = function () {
974
+ const testUseCase = function () {
884
975
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
885
- const objectReducers = objectUseCase();
976
+ const entityReducers = entityUseCase();
886
977
  void options;
887
978
  return {
888
- ...objectReducers
979
+ ...entityReducers
889
980
  };
890
981
  };
891
982
  renderHook(() => {
892
- useUseCase(defaultFile, testObjectUseCase, {
983
+ useUseCase(defaultFile, testUseCase, {
893
984
  onUpdate(data) {
894
985
  void data;
895
986
  return Promise.reject();
@@ -902,7 +993,7 @@ describe('useUseCase', () => {
902
993
  test('when `options` has changed, it should not trigger update', () => {
903
994
  const onUpdate = jest.fn();
904
995
  const onDeepUpdate = jest.fn();
905
- const _renderHook22 = renderHook(() => {
996
+ const _renderHook24 = renderHook(() => {
906
997
  const _useState = useState('user/'),
907
998
  _useState2 = _slicedToArray(_useState, 2),
908
999
  pathPrefix = _useState2[0],
@@ -918,7 +1009,7 @@ describe('useUseCase', () => {
918
1009
  }, [context]);
919
1010
  return setPathPrefix;
920
1011
  }),
921
- result = _renderHook22.result;
1012
+ result = _renderHook24.result;
922
1013
  expect(onUpdate).toHaveBeenCalledTimes(0);
923
1014
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
924
1015
  act(() => {
@@ -930,7 +1021,7 @@ describe('useUseCase', () => {
930
1021
  test('when `deps` has changed, it should trigger update', () => {
931
1022
  const onUpdate = jest.fn();
932
1023
  const onDeepUpdate = jest.fn();
933
- const _renderHook23 = renderHook(() => {
1024
+ const _renderHook25 = renderHook(() => {
934
1025
  const _useState3 = useState('user/'),
935
1026
  _useState4 = _slicedToArray(_useState3, 2),
936
1027
  pathPrefix = _useState4[0],
@@ -946,7 +1037,7 @@ describe('useUseCase', () => {
946
1037
  }, [context]);
947
1038
  return setPathPrefix;
948
1039
  }),
949
- result = _renderHook23.result;
1040
+ result = _renderHook25.result;
950
1041
  expect(onUpdate).toHaveBeenCalledTimes(0);
951
1042
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
952
1043
  act(() => {
@@ -958,7 +1049,7 @@ describe('useUseCase', () => {
958
1049
  test('empty `deps` should not trigger update', () => {
959
1050
  const onUpdate = jest.fn();
960
1051
  const onDeepUpdate = jest.fn();
961
- const _renderHook24 = renderHook(() => {
1052
+ const _renderHook26 = renderHook(() => {
962
1053
  const _useState5 = useState('user/'),
963
1054
  _useState6 = _slicedToArray(_useState5, 2),
964
1055
  pathPrefix = _useState6[0],
@@ -974,7 +1065,7 @@ describe('useUseCase', () => {
974
1065
  }, [context]);
975
1066
  return setPathPrefix;
976
1067
  }),
977
- result = _renderHook24.result;
1068
+ result = _renderHook26.result;
978
1069
  expect(onUpdate).toHaveBeenCalledTimes(0);
979
1070
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
980
1071
  act(() => {
@@ -987,12 +1078,12 @@ describe('useUseCase', () => {
987
1078
  const onPathChange1 = jest.fn();
988
1079
  const onPathChange2 = jest.fn();
989
1080
  let onPathChange = onPathChange1;
990
- const _renderHook25 = renderHook(() => {
1081
+ const _renderHook27 = renderHook(() => {
991
1082
  return useUseCase(defaultFile, fileUseCase, {
992
1083
  onPathChange
993
1084
  });
994
1085
  }),
995
- result = _renderHook25.result;
1086
+ result = _renderHook27.result;
996
1087
  const context = result.current;
997
1088
  const _context2 = _slicedToArray(context, 2),
998
1089
  setPath = _context2[1].setPath;
@@ -1013,7 +1104,7 @@ describe('useUseCase', () => {
1013
1104
  test('`options.watch` should be trigger after entity changed', () => {
1014
1105
  const onPathChange = jest.fn();
1015
1106
  const onSizeChange = jest.fn();
1016
- const _renderHook26 = renderHook(() => {
1107
+ const _renderHook28 = renderHook(() => {
1017
1108
  return useUseCase(defaultFile, fileUseCase, {
1018
1109
  watch: {
1019
1110
  path: onPathChange,
@@ -1021,7 +1112,7 @@ describe('useUseCase', () => {
1021
1112
  }
1022
1113
  });
1023
1114
  }),
1024
- result = _renderHook26.result;
1115
+ result = _renderHook28.result;
1025
1116
  const cores = result.current;
1026
1117
  const _cores11 = _slicedToArray(cores, 2),
1027
1118
  setPath = _cores11[1].setPath;
@@ -1047,14 +1138,14 @@ describe('useUseCase', () => {
1047
1138
  const onPathChange1 = jest.fn();
1048
1139
  const onPathChange2 = jest.fn();
1049
1140
  let onPathChange = onPathChange1;
1050
- const _renderHook27 = renderHook(() => {
1141
+ const _renderHook29 = renderHook(() => {
1051
1142
  return useUseCase(defaultFile, fileUseCase, {
1052
1143
  watch: {
1053
1144
  path: onPathChange
1054
1145
  }
1055
1146
  });
1056
1147
  }),
1057
- result = _renderHook27.result;
1148
+ result = _renderHook29.result;
1058
1149
  const cores = result.current;
1059
1150
  const _cores12 = _slicedToArray(cores, 2),
1060
1151
  _cores12$ = _cores12[1],
@@ -1105,7 +1196,7 @@ describe('useUseCase', () => {
1105
1196
  const initialData = {};
1106
1197
  const onExtChange = jest.fn();
1107
1198
  const onSizeChange = jest.fn();
1108
- const _renderHook28 = renderHook(() => {
1199
+ const _renderHook30 = renderHook(() => {
1109
1200
  return useUseCase(initialData, fieldPathUseCase, {
1110
1201
  watch: {
1111
1202
  'list.ext': onExtChange,
@@ -1113,7 +1204,7 @@ describe('useUseCase', () => {
1113
1204
  }
1114
1205
  });
1115
1206
  }),
1116
- result = _renderHook28.result;
1207
+ result = _renderHook30.result;
1117
1208
  const cores = result.current;
1118
1209
  const _cores13 = _slicedToArray(cores, 2),
1119
1210
  setEntity = _cores13[1].setEntity;
@@ -1250,7 +1341,8 @@ describe('useUseCase', () => {
1250
1341
  }, {
1251
1342
  ...defaultFile,
1252
1343
  ext: EXT_1
1253
- }, defaultFile]
1344
+ }, defaultFile],
1345
+ nestedList: []
1254
1346
  },
1255
1347
  oldEntity: {
1256
1348
  list: [{
@@ -1274,7 +1366,8 @@ describe('useUseCase', () => {
1274
1366
  }, {
1275
1367
  ...defaultFile,
1276
1368
  ext: EXT_1
1277
- }, defaultFile]
1369
+ }, defaultFile],
1370
+ nestedList: []
1278
1371
  },
1279
1372
  oldEntity: {
1280
1373
  list: [{
@@ -1298,7 +1391,8 @@ describe('useUseCase', () => {
1298
1391
  }, {
1299
1392
  ...defaultFile,
1300
1393
  ext: EXT_1
1301
- }, defaultFile]
1394
+ }, defaultFile],
1395
+ nestedList: []
1302
1396
  },
1303
1397
  oldEntity: {
1304
1398
  list: [{
@@ -1322,7 +1416,8 @@ describe('useUseCase', () => {
1322
1416
  }, {
1323
1417
  ...defaultFile,
1324
1418
  ext: EXT_1
1325
- }, defaultFile]
1419
+ }, defaultFile],
1420
+ nestedList: []
1326
1421
  },
1327
1422
  oldEntity: {
1328
1423
  list: [{
@@ -1339,15 +1434,22 @@ describe('useUseCase', () => {
1339
1434
  });
1340
1435
  act(() => {
1341
1436
  setEntity(() => {
1342
- return {};
1437
+ return {
1438
+ list: []
1439
+ };
1343
1440
  });
1344
1441
  });
1345
1442
  expect(onExtChange).toHaveBeenCalledTimes(9);
1346
1443
  expect(onSizeChange).toHaveBeenCalledTimes(6);
1444
+ const newEntity1 = {
1445
+ list: [],
1446
+ nestedList: []
1447
+ };
1347
1448
  expect(onExtChange).toHaveBeenNthCalledWith(7, {
1348
1449
  fieldPaths: ['list', '0', 'ext'],
1349
- newEntity: {},
1450
+ newEntity: newEntity1,
1350
1451
  oldEntity: {
1452
+ ...newEntity1,
1351
1453
  list: [{
1352
1454
  ...defaultFile,
1353
1455
  ext: EXT_2
@@ -1361,8 +1463,9 @@ describe('useUseCase', () => {
1361
1463
  });
1362
1464
  expect(onExtChange).toHaveBeenNthCalledWith(8, {
1363
1465
  fieldPaths: ['list', '1', 'ext'],
1364
- newEntity: {},
1466
+ newEntity: newEntity1,
1365
1467
  oldEntity: {
1468
+ ...newEntity1,
1366
1469
  list: [{
1367
1470
  ...defaultFile,
1368
1471
  ext: EXT_2
@@ -1376,8 +1479,9 @@ describe('useUseCase', () => {
1376
1479
  });
1377
1480
  expect(onExtChange).toHaveBeenNthCalledWith(9, {
1378
1481
  fieldPaths: ['list', '2', 'ext'],
1379
- newEntity: {},
1482
+ newEntity: newEntity1,
1380
1483
  oldEntity: {
1484
+ ...newEntity1,
1381
1485
  list: [{
1382
1486
  ...defaultFile,
1383
1487
  ext: EXT_2
@@ -1391,8 +1495,9 @@ describe('useUseCase', () => {
1391
1495
  });
1392
1496
  expect(onSizeChange).toHaveBeenNthCalledWith(4, {
1393
1497
  fieldPaths: ['list', '0', 'size'],
1394
- newEntity: {},
1498
+ newEntity: newEntity1,
1395
1499
  oldEntity: {
1500
+ ...newEntity1,
1396
1501
  list: [{
1397
1502
  ...defaultFile,
1398
1503
  ext: EXT_2
@@ -1406,8 +1511,9 @@ describe('useUseCase', () => {
1406
1511
  });
1407
1512
  expect(onSizeChange).toHaveBeenNthCalledWith(5, {
1408
1513
  fieldPaths: ['list', '1', 'size'],
1409
- newEntity: {},
1514
+ newEntity: newEntity1,
1410
1515
  oldEntity: {
1516
+ ...newEntity1,
1411
1517
  list: [{
1412
1518
  ...defaultFile,
1413
1519
  ext: EXT_2
@@ -1421,8 +1527,9 @@ describe('useUseCase', () => {
1421
1527
  });
1422
1528
  expect(onSizeChange).toHaveBeenNthCalledWith(6, {
1423
1529
  fieldPaths: ['list', '2', 'size'],
1424
- newEntity: {},
1530
+ newEntity: newEntity1,
1425
1531
  oldEntity: {
1532
+ ...newEntity1,
1426
1533
  list: [{
1427
1534
  ...defaultFile,
1428
1535
  ext: EXT_2
@@ -1441,7 +1548,7 @@ describe('useUseCase', () => {
1441
1548
  const lengthUseCase = () => {
1442
1549
  return entityUseCase();
1443
1550
  };
1444
- const _renderHook29 = renderHook(() => {
1551
+ const _renderHook31 = renderHook(() => {
1445
1552
  return useUseCase([], lengthUseCase, {
1446
1553
  watch: {
1447
1554
  length: onEntityLengthChange,
@@ -1449,7 +1556,7 @@ describe('useUseCase', () => {
1449
1556
  }
1450
1557
  });
1451
1558
  }),
1452
- result = _renderHook29.result;
1559
+ result = _renderHook31.result;
1453
1560
  const cores = result.current;
1454
1561
  const _cores14 = _slicedToArray(cores, 2),
1455
1562
  setEntity = _cores14[1].setEntity;
@@ -1484,14 +1591,14 @@ describe('useUseCase', () => {
1484
1591
  test('`options.watch` should work with field path of nested array', () => {
1485
1592
  const initialData = {};
1486
1593
  const onChange = jest.fn();
1487
- const _renderHook30 = renderHook(() => {
1594
+ const _renderHook32 = renderHook(() => {
1488
1595
  return useUseCase(initialData, fieldPathUseCase, {
1489
1596
  watch: {
1490
1597
  'nestedList.size': onChange
1491
1598
  }
1492
1599
  });
1493
1600
  }),
1494
- result = _renderHook30.result;
1601
+ result = _renderHook32.result;
1495
1602
  const cores = result.current;
1496
1603
  const _cores15 = _slicedToArray(cores, 2),
1497
1604
  setEntity = _cores15[1].setEntity;
@@ -1591,7 +1698,7 @@ describe('useUseCase', () => {
1591
1698
  const initialData = {};
1592
1699
  const onExtChange = jest.fn();
1593
1700
  const onSizeChange = jest.fn();
1594
- const _renderHook31 = renderHook(() => {
1701
+ const _renderHook33 = renderHook(() => {
1595
1702
  return useUseCase(initialData, fieldPathUseCase, {
1596
1703
  watch: {
1597
1704
  'obj.file.ext': onExtChange,
@@ -1599,7 +1706,7 @@ describe('useUseCase', () => {
1599
1706
  }
1600
1707
  });
1601
1708
  }),
1602
- result = _renderHook31.result;
1709
+ result = _renderHook33.result;
1603
1710
  const cores = result.current;
1604
1711
  const _cores16 = _slicedToArray(cores, 2),
1605
1712
  setEntity = _cores16[1].setEntity;
@@ -1630,14 +1737,22 @@ describe('useUseCase', () => {
1630
1737
  });
1631
1738
  act(() => {
1632
1739
  setEntity(() => {
1633
- return {};
1740
+ return {
1741
+ obj: {
1742
+ file: {}
1743
+ }
1744
+ };
1634
1745
  });
1635
1746
  });
1636
1747
  expect(onExtChange).toHaveBeenCalledTimes(2);
1637
1748
  expect(onSizeChange).toHaveBeenCalledTimes(0);
1638
1749
  expect(onExtChange).toHaveBeenCalledWith({
1639
1750
  fieldPaths: ['obj', 'file', 'ext'],
1640
- newEntity: {},
1751
+ newEntity: {
1752
+ obj: {
1753
+ file: {}
1754
+ }
1755
+ },
1641
1756
  oldEntity: {
1642
1757
  obj: {
1643
1758
  file: {
@@ -2260,38 +2375,38 @@ describe('useUseCase', () => {
2260
2375
  });
2261
2376
  describe('`useUseCase` should work the same as `useReducers`', () => {
2262
2377
  test('`entity` should equal `defaultFile`', () => {
2263
- const _renderHook32 = renderHook(() => {
2378
+ const _renderHook34 = renderHook(() => {
2264
2379
  return useUseCase(mathUseCase);
2265
2380
  }),
2266
- result = _renderHook32.result;
2381
+ result = _renderHook34.result;
2267
2382
  const reducers = result.current;
2268
2383
  expect(Array.isArray(reducers)).toBe(false);
2269
2384
  });
2270
2385
  test('returned reducer should work', () => {
2271
- const _renderHook33 = renderHook(() => {
2386
+ const _renderHook35 = renderHook(() => {
2272
2387
  return useUseCase(mathUseCase);
2273
2388
  }),
2274
- result = _renderHook33.result;
2389
+ result = _renderHook35.result;
2275
2390
  const reducers = result.current;
2276
2391
  const add = reducers.add;
2277
2392
  expect(add(1, 2)).toBe(3);
2278
2393
  });
2279
2394
  test('`usecase` should be called only once if deps has not provided', () => {
2280
2395
  const mockUseCase = jest.fn(mathUseCase);
2281
- const _renderHook34 = renderHook(() => {
2396
+ const _renderHook36 = renderHook(() => {
2282
2397
  return useUseCase(mockUseCase);
2283
2398
  }),
2284
- rerender = _renderHook34.rerender;
2399
+ rerender = _renderHook36.rerender;
2285
2400
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2286
2401
  rerender();
2287
2402
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2288
2403
  });
2289
2404
  test('`usecase` should be called only once if deps has not changed', () => {
2290
2405
  const mockUseCase = jest.fn(mathUseCase);
2291
- const _renderHook35 = renderHook(() => {
2406
+ const _renderHook37 = renderHook(() => {
2292
2407
  return useUseCase(mockUseCase, {}, [1, 'x']);
2293
2408
  }),
2294
- rerender = _renderHook35.rerender;
2409
+ rerender = _renderHook37.rerender;
2295
2410
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2296
2411
  rerender();
2297
2412
  expect(mockUseCase).toHaveBeenCalledTimes(1);
@@ -2299,10 +2414,10 @@ describe('useUseCase', () => {
2299
2414
  test('`usecase` should be called if deps has changed', () => {
2300
2415
  let i = 0;
2301
2416
  const mockUseCase = jest.fn(mathUseCase);
2302
- const _renderHook36 = renderHook(() => {
2417
+ const _renderHook38 = renderHook(() => {
2303
2418
  return useUseCase(mockUseCase, {}, [i++, 'x']);
2304
2419
  }),
2305
- rerender = _renderHook36.rerender;
2420
+ rerender = _renderHook38.rerender;
2306
2421
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2307
2422
  rerender();
2308
2423
  expect(mockUseCase).toHaveBeenCalledTimes(2);
@@ -2386,12 +2501,12 @@ describe('useUseCase', () => {
2386
2501
  expect(subtractionReducer).toHaveReturnedWith(4);
2387
2502
  });
2388
2503
  test('`options.extraValue` should be added after call `add` method', () => {
2389
- const _renderHook37 = renderHook(() => {
2504
+ const _renderHook39 = renderHook(() => {
2390
2505
  return useUseCase(mathUseCase, {
2391
2506
  extraValue: 5
2392
2507
  });
2393
2508
  }),
2394
- result = _renderHook37.result;
2509
+ result = _renderHook39.result;
2395
2510
  const reducers = result.current;
2396
2511
  const add = reducers.add;
2397
2512
  expect(add(1, 2)).toBe(8);
@@ -2399,7 +2514,7 @@ describe('useUseCase', () => {
2399
2514
  test('when `options` has changed, it should not trigger update', () => {
2400
2515
  const onUpdate = jest.fn();
2401
2516
  const onDeepUpdate = jest.fn();
2402
- const _renderHook38 = renderHook(() => {
2517
+ const _renderHook40 = renderHook(() => {
2403
2518
  const _useState7 = useState(0),
2404
2519
  _useState8 = _slicedToArray(_useState7, 2),
2405
2520
  extraValue = _useState8[0],
@@ -2415,7 +2530,7 @@ describe('useUseCase', () => {
2415
2530
  }, [reducers]);
2416
2531
  return setExtraValue;
2417
2532
  }),
2418
- result = _renderHook38.result;
2533
+ result = _renderHook40.result;
2419
2534
  expect(onUpdate).toHaveBeenCalledTimes(0);
2420
2535
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
2421
2536
  act(() => {
@@ -2427,7 +2542,7 @@ describe('useUseCase', () => {
2427
2542
  test('when `deps` change, it should update reducers', () => {
2428
2543
  const onUpdate = jest.fn();
2429
2544
  const onDeepUpdate = jest.fn();
2430
- const _renderHook39 = renderHook(() => {
2545
+ const _renderHook41 = renderHook(() => {
2431
2546
  const _useState9 = useState(0),
2432
2547
  _useState0 = _slicedToArray(_useState9, 2),
2433
2548
  extraValue = _useState0[0],
@@ -2443,7 +2558,7 @@ describe('useUseCase', () => {
2443
2558
  }, [reducers]);
2444
2559
  return setExtraValue;
2445
2560
  }),
2446
- result = _renderHook39.result;
2561
+ result = _renderHook41.result;
2447
2562
  expect(onUpdate).toHaveBeenCalledTimes(0);
2448
2563
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
2449
2564
  act(() => {
@@ -2455,7 +2570,7 @@ describe('useUseCase', () => {
2455
2570
  test('empty `deps` should not update reducers', () => {
2456
2571
  const onUpdate = jest.fn();
2457
2572
  const onDeepUpdate = jest.fn();
2458
- const _renderHook40 = renderHook(() => {
2573
+ const _renderHook42 = renderHook(() => {
2459
2574
  const _useState1 = useState(0),
2460
2575
  _useState10 = _slicedToArray(_useState1, 2),
2461
2576
  extraValue = _useState10[0],
@@ -2471,7 +2586,7 @@ describe('useUseCase', () => {
2471
2586
  }, [reducers]);
2472
2587
  return setExtraValue;
2473
2588
  }),
2474
- result = _renderHook40.result;
2589
+ result = _renderHook42.result;
2475
2590
  expect(onUpdate).toHaveBeenCalledTimes(0);
2476
2591
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
2477
2592
  act(() => {