@mic-rexjs/usecases-react 3.7.0 → 4.0.0-beta.3

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 (150) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.LICENSE.txt +2 -0
  3. package/es/components/UseCaseFragment/index.js +2 -2
  4. package/es/enums/Statuses/index.d.ts +86 -10
  5. package/es/enums/Statuses/index.js +86 -10
  6. package/es/hooks/useAlternateStatuses/index.d.ts +2 -0
  7. package/es/hooks/useAlternateStatuses/index.js +16 -0
  8. package/es/hooks/useChanged/index.d.ts +2 -0
  9. package/es/hooks/useChanged/index.js +17 -0
  10. package/es/hooks/useChanged/types.d.ts +3 -0
  11. package/es/hooks/useContext/index.d.ts +3 -4
  12. package/es/hooks/useContext/index.js +17 -15
  13. package/es/hooks/useContext/index.test.js +13 -24
  14. package/es/hooks/useContextValue/index.d.ts +5 -0
  15. package/es/hooks/useContextValue/index.js +13 -0
  16. package/es/hooks/useDepsKey/index.js +11 -9
  17. package/es/hooks/useEntity/index.d.ts +3 -2
  18. package/es/hooks/useEntity/index.js +52 -53
  19. package/es/hooks/useEntityReturn/index.d.ts +5 -0
  20. package/es/hooks/useEntityReturn/index.js +15 -0
  21. package/es/hooks/useEntityReturn/types.d.ts +7 -0
  22. package/es/hooks/useEntityRootExec/index.d.ts +5 -0
  23. package/es/hooks/useEntityRootExec/index.js +7 -0
  24. package/es/hooks/useEntityRootExec/types.d.ts +7 -0
  25. package/es/hooks/useEntityRootExec/types.js +1 -0
  26. package/es/hooks/useEntityRootReturn/index.d.ts +5 -0
  27. package/es/hooks/useEntityRootReturn/index.js +14 -0
  28. package/es/hooks/useEntityRootReturn/types.d.ts +7 -0
  29. package/es/hooks/useEntityRootReturn/types.js +1 -0
  30. package/es/hooks/useExec/index.d.ts +2 -3
  31. package/es/hooks/useExec/index.js +3 -12
  32. package/es/hooks/useExec/types.d.ts +10 -2
  33. package/es/hooks/useFullArguments/index.d.ts +2 -3
  34. package/es/hooks/useFullArguments/index.js +4 -6
  35. package/es/hooks/useFullArguments/types.d.ts +2 -2
  36. package/es/hooks/useIfExec/index.d.ts +2 -0
  37. package/es/hooks/useIfExec/index.js +20 -0
  38. package/es/hooks/useIfExec/types.d.ts +6 -0
  39. package/es/hooks/useIfExec/types.js +1 -0
  40. package/es/hooks/useIfReturn/index.d.ts +2 -0
  41. package/es/hooks/useIfReturn/index.js +22 -0
  42. package/es/hooks/useIfReturn/types.d.ts +6 -0
  43. package/es/hooks/useIfReturn/types.js +1 -0
  44. package/es/hooks/useProvider/index.d.ts +3 -5
  45. package/es/hooks/useProvider/index.js +17 -23
  46. package/es/hooks/useProviders/index.js +4 -4
  47. package/es/hooks/useReducers/index.d.ts +5 -4
  48. package/es/hooks/useReducers/index.js +45 -27
  49. package/es/hooks/useReturn/index.d.ts +2 -0
  50. package/es/hooks/useReturn/index.js +33 -0
  51. package/es/hooks/useReturn/types.d.ts +11 -0
  52. package/es/hooks/useReturn/types.js +1 -0
  53. package/es/hooks/useRootInitializing/index.d.ts +2 -0
  54. package/es/hooks/useRootInitializing/index.js +14 -0
  55. package/es/hooks/useRootReturn/index.d.ts +5 -0
  56. package/es/hooks/useRootReturn/index.js +22 -0
  57. package/es/hooks/useRootReturn/types.d.ts +14 -0
  58. package/es/hooks/useRootReturn/types.js +1 -0
  59. package/es/hooks/useStatuses/index.d.ts +4 -4
  60. package/es/hooks/useStatuses/index.js +46 -21
  61. package/es/hooks/useStore/index.d.ts +3 -3
  62. package/es/hooks/useStore/index.js +21 -49
  63. package/es/hooks/useThrottleUpdate/index.d.ts +2 -0
  64. package/es/hooks/useThrottleUpdate/index.js +31 -0
  65. package/es/hooks/useThrottleUpdate/types.d.ts +3 -0
  66. package/es/hooks/useThrottleUpdate/types.js +1 -0
  67. package/es/hooks/useUseCase/index.js +21 -24
  68. package/es/hooks/useUseCase/index.test.js +330 -98
  69. package/es/hooks/useUseCase/types.d.ts +15 -5
  70. package/es/methods/cacheCall/index.d.ts +2 -0
  71. package/es/methods/cacheCall/index.js +38 -0
  72. package/es/methods/cacheCall/types.d.ts +12 -0
  73. package/es/methods/cacheCall/types.js +1 -0
  74. package/es/methods/cacheCalls/index.d.ts +2 -0
  75. package/es/methods/cacheCalls/index.js +12 -0
  76. package/es/types.d.ts +2 -1
  77. package/es/usecases/contextUseCase/index.js +21 -38
  78. package/es/usecases/contextUseCase/types.d.ts +5 -6
  79. package/es/usecases/lifecycleEventUseCase/index.js +37 -9
  80. package/es/usecases/lifecycleEventUseCase/types.d.ts +11 -8
  81. package/lib/components/UseCaseFragment/index.js +2 -2
  82. package/lib/enums/Statuses/index.js +86 -10
  83. package/lib/hooks/useAlternateStatuses/index.js +22 -0
  84. package/lib/hooks/useChanged/index.js +23 -0
  85. package/lib/hooks/useContext/index.js +19 -17
  86. package/lib/hooks/useContext/index.test.js +13 -24
  87. package/lib/hooks/useContextValue/index.js +19 -0
  88. package/lib/hooks/useDepsKey/index.js +10 -8
  89. package/lib/hooks/useEntity/index.js +81 -54
  90. package/lib/hooks/useEntityReturn/index.js +22 -0
  91. package/lib/hooks/useEntityRootExec/index.js +13 -0
  92. package/lib/hooks/useEntityRootExec/types.js +5 -0
  93. package/lib/hooks/useEntityRootReturn/index.js +21 -0
  94. package/lib/hooks/useEntityRootReturn/types.js +5 -0
  95. package/lib/hooks/useExec/index.js +3 -12
  96. package/lib/hooks/useFullArguments/index.js +4 -6
  97. package/lib/hooks/useIfExec/index.js +27 -0
  98. package/lib/hooks/useIfExec/types.js +5 -0
  99. package/lib/hooks/useIfReturn/index.js +29 -0
  100. package/lib/hooks/useIfReturn/types.js +5 -0
  101. package/lib/hooks/useProvider/index.js +16 -22
  102. package/lib/hooks/useProviders/index.js +3 -3
  103. package/lib/hooks/useReducers/index.js +46 -29
  104. package/lib/hooks/useReturn/index.js +40 -0
  105. package/lib/hooks/useReturn/types.js +5 -0
  106. package/lib/hooks/useRootInitializing/index.js +20 -0
  107. package/lib/hooks/useRootReturn/index.js +30 -0
  108. package/lib/hooks/useRootReturn/types.js +5 -0
  109. package/lib/hooks/useStatuses/index.js +47 -21
  110. package/lib/hooks/useStore/index.js +19 -48
  111. package/lib/hooks/useThrottleUpdate/index.js +37 -0
  112. package/lib/hooks/useThrottleUpdate/types.js +5 -0
  113. package/lib/hooks/useUseCase/index.js +22 -25
  114. package/lib/hooks/useUseCase/index.test.js +360 -97
  115. package/lib/methods/cacheCall/index.js +44 -0
  116. package/lib/methods/cacheCall/types.js +5 -0
  117. package/lib/methods/cacheCalls/index.js +19 -0
  118. package/lib/usecases/contextUseCase/index.js +21 -38
  119. package/lib/usecases/lifecycleEventUseCase/index.js +48 -8
  120. package/package.json +3 -3
  121. package/es/enums/ArgumentTypes/index.d.ts +0 -4
  122. package/es/enums/ArgumentTypes/index.js +0 -5
  123. package/es/hooks/useArgumentTypes/index.d.ts +0 -3
  124. package/es/hooks/useArgumentTypes/index.js +0 -14
  125. package/es/hooks/useConstantEntityReducers/index.d.ts +0 -3
  126. package/es/hooks/useConstantEntityReducers/index.js +0 -7
  127. package/es/hooks/useConstantFn/index.d.ts +0 -1
  128. package/es/hooks/useConstantFn/index.js +0 -10
  129. package/es/hooks/useConstantReducers/index.d.ts +0 -3
  130. package/es/hooks/useConstantReducers/index.js +0 -6
  131. package/es/hooks/useContextualItem/index.d.ts +0 -4
  132. package/es/hooks/useContextualItem/index.js +0 -31
  133. package/es/hooks/useContextualItem/types.d.ts +0 -3
  134. package/es/hooks/useEntityInitializing/index.d.ts +0 -2
  135. package/es/hooks/useEntityInitializing/index.js +0 -16
  136. package/es/usecases/methodUseCase/index.d.ts +0 -2
  137. package/es/usecases/methodUseCase/index.js +0 -72
  138. package/es/usecases/methodUseCase/types.d.ts +0 -21
  139. package/lib/enums/ArgumentTypes/index.js +0 -11
  140. package/lib/hooks/useArgumentTypes/index.js +0 -22
  141. package/lib/hooks/useConstantEntityReducers/index.js +0 -13
  142. package/lib/hooks/useConstantFn/index.js +0 -17
  143. package/lib/hooks/useConstantReducers/index.js +0 -12
  144. package/lib/hooks/useContextualItem/index.js +0 -37
  145. package/lib/hooks/useEntityInitializing/index.js +0 -22
  146. package/lib/usecases/methodUseCase/index.js +0 -79
  147. /package/es/hooks/{useContextualItem → useChanged}/types.js +0 -0
  148. /package/es/{usecases/methodUseCase → hooks/useEntityReturn}/types.js +0 -0
  149. /package/lib/hooks/{useContextualItem → useChanged}/types.js +0 -0
  150. /package/lib/{usecases/methodUseCase → hooks/useEntityReturn}/types.js +0 -0
@@ -26,6 +26,7 @@ const fileUseCase = function () {
26
26
  const entityReducers = entityUseCase();
27
27
  const _options$pathPrefix = options.pathPrefix,
28
28
  pathPrefix = _options$pathPrefix === void 0 ? '' : _options$pathPrefix,
29
+ onReadFile = options.onReadFile,
29
30
  onPathChange = options.onPathChange;
30
31
  const init = function (entity) {
31
32
  let newEntity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultFile;
@@ -47,7 +48,8 @@ const fileUseCase = function () {
47
48
  onPathChange === null || onPathChange === void 0 || onPathChange(newPath, oldPath);
48
49
  };
49
50
  const readFile = async function* (entity, path) {
50
- const size = 2000;
51
+ var _onReadFile;
52
+ const size = (_onReadFile = onReadFile === null || onReadFile === void 0 ? void 0 : onReadFile(path)) !== null && _onReadFile !== void 0 ? _onReadFile : 2000;
51
53
  yield* setPath(entity, path);
52
54
  await Promise.resolve(null);
53
55
  yield oldEntity => {
@@ -946,7 +948,7 @@ describe('useUseCase', () => {
946
948
  fireEvent.click(screen.getByText(updateButtonText));
947
949
  expect(addReducer).toHaveBeenCalledTimes(3);
948
950
  });
949
- test('`options.onChange` should be trigger when entity has changed', () => {
951
+ test('`options.onChange` should be triggered when entity has changed', () => {
950
952
  const onChange = jest.fn();
951
953
  const _renderHook23 = renderHook(() => {
952
954
  return useUseCase(defaultFile, fileUseCase, {
@@ -1162,7 +1164,7 @@ describe('useUseCase', () => {
1162
1164
  expect(onPathChange2).toHaveBeenCalledTimes(1);
1163
1165
  expect(onPathChange2).toHaveBeenLastCalledWith(PATH_2, PATH_1);
1164
1166
  });
1165
- test('`options.watch` should be trigger after entity changed', () => {
1167
+ test('`options.watch` should be triggered after entity changed', () => {
1166
1168
  const onPathChange = jest.fn();
1167
1169
  const onSizeChange = jest.fn();
1168
1170
  const _renderHook30 = renderHook(() => {
@@ -1195,7 +1197,7 @@ describe('useUseCase', () => {
1195
1197
  oldValue: ''
1196
1198
  });
1197
1199
  });
1198
- test('`options.watch` should be trigger after update entity by entity initializer', () => {
1200
+ test('`options.watch` should be triggered after update entity by entity initializer', () => {
1199
1201
  const onPathChange = jest.fn();
1200
1202
  const _renderHook31 = renderHook(function () {
1201
1203
  let path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
@@ -1244,7 +1246,7 @@ describe('useUseCase', () => {
1244
1246
  oldValue: PATH_1
1245
1247
  });
1246
1248
  });
1247
- test('`options.watch` should be trigger after update entity by entity initializer at child component', () => {
1249
+ test('`options.watch` should be triggered after update entity by entity initializer at child component', () => {
1248
1250
  const onPathChange = jest.fn();
1249
1251
  const TestChild = () => {
1250
1252
  useUseCase(fileUseCase, {
@@ -1999,16 +2001,17 @@ describe('useUseCase', () => {
1999
2001
  expect(onStringMount).toHaveBeenCalledWith('hello');
2000
2002
  expect(onBooleanMount).toHaveBeenCalledWith(true);
2001
2003
  });
2002
- test('`onBind` event should be called when component mount', () => {
2004
+ test('Lifecycle `onBind` event should be called when component mount', () => {
2003
2005
  const onBind = jest.fn(entity => {
2004
2006
  void entity;
2005
2007
  });
2006
2008
  const testUseCase = () => {
2007
- const lifecycleReducers = lifecycleUseCase({
2009
+ const entityReducers = entityUseCase();
2010
+ lifecycleUseCase({
2008
2011
  onBind
2009
2012
  });
2010
2013
  return {
2011
- ...lifecycleReducers
2014
+ ...entityReducers
2012
2015
  };
2013
2016
  };
2014
2017
  renderHook(() => {
@@ -2022,17 +2025,18 @@ describe('useUseCase', () => {
2022
2025
  expect(onBind).toHaveBeenCalledTimes(1);
2023
2026
  expect(onBind).toHaveBeenCalledWith(5);
2024
2027
  });
2025
- test('`onChange` event should be called when entity change', () => {
2028
+ test('Lifecycle `onChange` event should be called when entity change', () => {
2026
2029
  const onChange = jest.fn((newEntity, oldEntity) => {
2027
2030
  void newEntity;
2028
2031
  void oldEntity;
2029
2032
  });
2030
2033
  const testUseCase = () => {
2031
- const lifecycleReducers = lifecycleUseCase({
2034
+ const entityReducers = entityUseCase();
2035
+ lifecycleUseCase({
2032
2036
  onChange
2033
2037
  });
2034
2038
  return {
2035
- ...lifecycleReducers
2039
+ ...entityReducers
2036
2040
  };
2037
2041
  };
2038
2042
  const _renderHook37 = renderHook(() => {
@@ -2049,26 +2053,177 @@ describe('useUseCase', () => {
2049
2053
  expect(onChange).toHaveBeenCalledTimes(1);
2050
2054
  expect(onChange).toHaveBeenCalledWith(100, 5);
2051
2055
  });
2052
- test('`onUpdate` event should be called when entity change', () => {
2053
- const onUpdate = jest.fn((newEntity, oldEntity) => {
2056
+ test('Lifecycle `onChange` event should support returning a boolean to control whether the component updates', () => {
2057
+ let i = 0;
2058
+ const onChange = jest.fn((newEntity, oldEntity) => {
2054
2059
  void newEntity;
2055
2060
  void oldEntity;
2061
+ return i++ === 0;
2056
2062
  });
2063
+ const onRenderHook = jest.fn();
2057
2064
  const testUseCase = () => {
2058
- const lifecycleReducers = lifecycleUseCase({
2059
- onUpdate
2065
+ const entityReducers = entityUseCase();
2066
+ lifecycleUseCase({
2067
+ onChange
2060
2068
  });
2061
2069
  return {
2062
- ...lifecycleReducers
2070
+ ...entityReducers
2063
2071
  };
2064
2072
  };
2065
2073
  const _renderHook38 = renderHook(() => {
2066
- return useUseCase(5, testUseCase);
2074
+ const core = useUseCase(5, testUseCase);
2075
+ const _core = _slicedToArray(core, 1),
2076
+ value = _core[0];
2077
+ onRenderHook(value);
2078
+ return core;
2067
2079
  }),
2068
2080
  result = _renderHook38.result;
2069
2081
  const current = result.current;
2070
2082
  const _current9 = _slicedToArray(current, 2),
2071
2083
  setEntity = _current9[1].setEntity;
2084
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2085
+ expect(onRenderHook).toHaveBeenLastCalledWith(5);
2086
+ act(() => {
2087
+ setEntity(100);
2088
+ });
2089
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2090
+ act(() => {
2091
+ setEntity(200);
2092
+ });
2093
+ expect(onRenderHook).toHaveBeenCalledTimes(2);
2094
+ expect(onRenderHook).toHaveBeenLastCalledWith(200);
2095
+ });
2096
+ test('Lifecycle `onChange` event should support returning a Promise<boolean> to control whether the component updates', async () => {
2097
+ let i = 0;
2098
+ let promise = null;
2099
+ const onChange = jest.fn((newEntity, oldEntity) => {
2100
+ void newEntity;
2101
+ void oldEntity;
2102
+ promise = new Promise(res => {
2103
+ setTimeout(() => {
2104
+ res(i++ === 0);
2105
+ }, 500);
2106
+ });
2107
+ return promise;
2108
+ });
2109
+ const onRenderHook = jest.fn();
2110
+ const testUseCase = () => {
2111
+ const entityReducers = entityUseCase();
2112
+ lifecycleUseCase({
2113
+ onChange
2114
+ });
2115
+ return {
2116
+ ...entityReducers
2117
+ };
2118
+ };
2119
+ const _renderHook39 = renderHook(() => {
2120
+ const core = useUseCase(5, testUseCase);
2121
+ const _core2 = _slicedToArray(core, 1),
2122
+ value = _core2[0];
2123
+ onRenderHook(value);
2124
+ return core;
2125
+ }),
2126
+ result = _renderHook39.result;
2127
+ const current = result.current;
2128
+ const _current0 = _slicedToArray(current, 2),
2129
+ setEntity = _current0[1].setEntity;
2130
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2131
+ expect(onRenderHook).toHaveBeenLastCalledWith(5);
2132
+ act(() => {
2133
+ setEntity(100);
2134
+ });
2135
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2136
+ await act(async () => {
2137
+ await Promise.resolve(promise);
2138
+ });
2139
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2140
+ act(() => {
2141
+ setEntity(200);
2142
+ });
2143
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2144
+ await act(async () => {
2145
+ await Promise.resolve(promise);
2146
+ });
2147
+ expect(onRenderHook).toHaveBeenCalledTimes(2);
2148
+ expect(onRenderHook).toHaveBeenLastCalledWith(200);
2149
+ });
2150
+ test('Lifecycle `onChange` event should support returning a number to control whether the component updates', async () => {
2151
+ const onChange = jest.fn((newEntity, oldEntity) => {
2152
+ void newEntity;
2153
+ void oldEntity;
2154
+ return 500;
2155
+ });
2156
+ const onRenderHook = jest.fn();
2157
+ const testUseCase = () => {
2158
+ const entityReducers = entityUseCase();
2159
+ lifecycleUseCase({
2160
+ onChange
2161
+ });
2162
+ return {
2163
+ ...entityReducers
2164
+ };
2165
+ };
2166
+ const _renderHook40 = renderHook(() => {
2167
+ const core = useUseCase(5, testUseCase);
2168
+ const _core3 = _slicedToArray(core, 1),
2169
+ value = _core3[0];
2170
+ onRenderHook(value);
2171
+ return core;
2172
+ }),
2173
+ result = _renderHook40.result;
2174
+ const current = result.current;
2175
+ const _current1 = _slicedToArray(current, 2),
2176
+ setEntity = _current1[1].setEntity;
2177
+ expect(onRenderHook).toHaveBeenCalledTimes(1);
2178
+ expect(onRenderHook).toHaveBeenLastCalledWith(5);
2179
+ act(() => {
2180
+ setEntity(100);
2181
+ });
2182
+ expect(onRenderHook).toHaveBeenCalledTimes(2);
2183
+ expect(onRenderHook).toHaveBeenLastCalledWith(100);
2184
+ await act(() => {
2185
+ return new Promise(res => {
2186
+ setEntity(200);
2187
+ setTimeout(res, 100);
2188
+ });
2189
+ });
2190
+ expect(onRenderHook).toHaveBeenCalledTimes(2);
2191
+ await act(() => {
2192
+ return new Promise(res => {
2193
+ setEntity(300);
2194
+ setTimeout(res, 100);
2195
+ });
2196
+ });
2197
+ expect(onRenderHook).toHaveBeenCalledTimes(2);
2198
+ await act(() => {
2199
+ return new Promise(res => {
2200
+ setTimeout(res, 500);
2201
+ });
2202
+ });
2203
+ expect(onRenderHook).toHaveBeenCalledTimes(3);
2204
+ expect(onRenderHook).toHaveBeenLastCalledWith(300);
2205
+ });
2206
+ test('Lifecycle `onUpdate` event should be called when entity change', () => {
2207
+ const onUpdate = jest.fn((newEntity, oldEntity) => {
2208
+ void newEntity;
2209
+ void oldEntity;
2210
+ });
2211
+ const testUseCase = () => {
2212
+ const entityReducers = entityUseCase();
2213
+ lifecycleUseCase({
2214
+ onUpdate
2215
+ });
2216
+ return {
2217
+ ...entityReducers
2218
+ };
2219
+ };
2220
+ const _renderHook41 = renderHook(() => {
2221
+ return useUseCase(5, testUseCase);
2222
+ }),
2223
+ result = _renderHook41.result;
2224
+ const current = result.current;
2225
+ const _current10 = _slicedToArray(current, 2),
2226
+ setEntity = _current10[1].setEntity;
2072
2227
  expect(onUpdate).toHaveBeenCalledTimes(0);
2073
2228
  act(() => {
2074
2229
  setEntity(100);
@@ -2076,19 +2231,20 @@ describe('useUseCase', () => {
2076
2231
  expect(onUpdate).toHaveBeenCalledTimes(1);
2077
2232
  expect(onUpdate).toHaveBeenCalledWith(100, 5);
2078
2233
  });
2079
- test('`onUnbind` event should be called when component unmount', () => {
2234
+ test('Lifecycle `onUnbind` event should be called when component unmount', () => {
2080
2235
  const onUnbind = jest.fn(entity => {
2081
2236
  void entity;
2082
2237
  });
2083
2238
  const testUseCase = () => {
2084
- const lifecycleReducers = lifecycleUseCase({
2239
+ const entityReducers = entityUseCase();
2240
+ lifecycleUseCase({
2085
2241
  onUnbind
2086
2242
  });
2087
2243
  return {
2088
- ...lifecycleReducers
2244
+ ...entityReducers
2089
2245
  };
2090
2246
  };
2091
- const _renderHook39 = renderHook(() => {
2247
+ const _renderHook42 = renderHook(() => {
2092
2248
  const _useUseCase26 = useUseCase(5, testUseCase),
2093
2249
  _useUseCase27 = _slicedToArray(_useUseCase26, 2),
2094
2250
  setEntity = _useUseCase27[1].setEntity;
@@ -2096,70 +2252,146 @@ describe('useUseCase', () => {
2096
2252
  setEntity(100);
2097
2253
  });
2098
2254
  }),
2099
- unmount = _renderHook39.unmount;
2255
+ unmount = _renderHook42.unmount;
2100
2256
  expect(onUnbind).toHaveBeenCalledTimes(0);
2101
2257
  unmount();
2102
2258
  expect(onUnbind).toHaveBeenCalledTimes(1);
2103
2259
  expect(onUnbind).toHaveBeenCalledWith(100);
2104
2260
  });
2105
- });
2106
- test('`onAdopt` event should be called when entity change', () => {
2107
- const onAdopt = jest.fn((newEntity, oldeEntity) => {
2108
- void newEntity;
2109
- void oldeEntity;
2261
+ test('Lifecycle `onAdopt` event should be called when entity change', () => {
2262
+ const onAdopt = jest.fn((newEntity, oldeEntity) => {
2263
+ void newEntity;
2264
+ void oldeEntity;
2265
+ });
2266
+ const testUseCase = () => {
2267
+ const entityReducers = entityUseCase();
2268
+ lifecycleUseCase({
2269
+ onAdopt
2270
+ });
2271
+ return {
2272
+ ...entityReducers
2273
+ };
2274
+ };
2275
+ const _renderHook43 = renderHook(() => {
2276
+ return useUseCase(5, testUseCase);
2277
+ }),
2278
+ result = _renderHook43.result;
2279
+ const current = result.current;
2280
+ const _current11 = _slicedToArray(current, 2),
2281
+ setEntity = _current11[1].setEntity;
2282
+ expect(onAdopt).toHaveBeenCalledTimes(1);
2283
+ expect(onAdopt).toHaveBeenCalledWith(5);
2284
+ act(() => {
2285
+ setEntity(100);
2286
+ });
2287
+ expect(onAdopt).toHaveBeenCalledTimes(2);
2288
+ expect(onAdopt).toHaveBeenCalledWith(100, 5);
2110
2289
  });
2111
- const testUseCase = () => {
2112
- const lifecycleReducers = lifecycleUseCase({
2113
- onAdopt
2290
+ test('Lifecycle `onDiscard` event should be called when component unmount', () => {
2291
+ const onDiscard = jest.fn(entity => {
2292
+ void entity;
2114
2293
  });
2115
- return {
2116
- ...lifecycleReducers
2294
+ const testUseCase = () => {
2295
+ const entityReducers = entityUseCase();
2296
+ lifecycleUseCase({
2297
+ onDiscard
2298
+ });
2299
+ return {
2300
+ ...entityReducers
2301
+ };
2117
2302
  };
2118
- };
2119
- const _renderHook40 = renderHook(() => {
2120
- return useUseCase(5, testUseCase);
2121
- }),
2122
- result = _renderHook40.result;
2123
- const current = result.current;
2124
- const _current0 = _slicedToArray(current, 2),
2125
- setEntity = _current0[1].setEntity;
2126
- expect(onAdopt).toHaveBeenCalledTimes(1);
2127
- expect(onAdopt).toHaveBeenCalledWith(5);
2128
- act(() => {
2129
- setEntity(100);
2130
- });
2131
- expect(onAdopt).toHaveBeenCalledTimes(2);
2132
- expect(onAdopt).toHaveBeenCalledWith(100, 5);
2133
- });
2134
- test('`onDiscard` event should be called when component unmount', () => {
2135
- const onDiscard = jest.fn(entity => {
2136
- void entity;
2303
+ const _renderHook44 = renderHook(() => {
2304
+ return useUseCase(5, testUseCase);
2305
+ }),
2306
+ result = _renderHook44.result,
2307
+ unmount = _renderHook44.unmount;
2308
+ const current = result.current;
2309
+ const _current12 = _slicedToArray(current, 2),
2310
+ setEntity = _current12[1].setEntity;
2311
+ expect(onDiscard).toHaveBeenCalledTimes(0);
2312
+ act(() => {
2313
+ setEntity(100);
2314
+ });
2315
+ expect(onDiscard).toHaveBeenCalledTimes(1);
2316
+ expect(onDiscard).toHaveBeenCalledWith(5);
2317
+ unmount();
2318
+ expect(onDiscard).toHaveBeenCalledTimes(2);
2319
+ expect(onDiscard).toHaveBeenCalledWith(100);
2137
2320
  });
2138
- const testUseCase = () => {
2139
- const lifecycleReducers = lifecycleUseCase({
2140
- onDiscard
2321
+ test('Should support call lifecycleUseCase at parent usecase and child usecase', () => {
2322
+ const onParentBind = jest.fn(entity => {
2323
+ void entity;
2141
2324
  });
2142
- return {
2143
- ...lifecycleReducers
2325
+ const onParentChange = jest.fn((newEntity, oldEntity) => {
2326
+ void newEntity;
2327
+ void oldEntity;
2328
+ });
2329
+ const onParentUnbind = jest.fn(entity => {
2330
+ void entity;
2331
+ });
2332
+ const onChildBind = jest.fn(entity => {
2333
+ void entity;
2334
+ });
2335
+ const onChildChange = jest.fn((newEntity, oldEntity) => {
2336
+ void newEntity;
2337
+ void oldEntity;
2338
+ });
2339
+ const onChildUnbind = jest.fn(entity => {
2340
+ void entity;
2341
+ });
2342
+ const parentUseCase = () => {
2343
+ const entityReducers = entityUseCase();
2344
+ lifecycleUseCase({
2345
+ onBind: onParentBind,
2346
+ onUnbind: onParentUnbind,
2347
+ onChange: onParentChange
2348
+ });
2349
+ return {
2350
+ ...entityReducers
2351
+ };
2144
2352
  };
2145
- };
2146
- const _renderHook41 = renderHook(() => {
2147
- return useUseCase(5, testUseCase);
2148
- }),
2149
- result = _renderHook41.result,
2150
- unmount = _renderHook41.unmount;
2151
- const current = result.current;
2152
- const _current1 = _slicedToArray(current, 2),
2153
- setEntity = _current1[1].setEntity;
2154
- expect(onDiscard).toHaveBeenCalledTimes(0);
2155
- act(() => {
2156
- setEntity(100);
2157
- });
2158
- expect(onDiscard).toHaveBeenCalledTimes(1);
2159
- expect(onDiscard).toHaveBeenCalledWith(5);
2160
- unmount();
2161
- expect(onDiscard).toHaveBeenCalledTimes(2);
2162
- expect(onDiscard).toHaveBeenCalledWith(100);
2353
+ const childUseCase = () => {
2354
+ const parentReducers = parentUseCase();
2355
+ lifecycleUseCase({
2356
+ onBind: onChildBind,
2357
+ onUnbind: onChildUnbind,
2358
+ onChange: onChildChange
2359
+ });
2360
+ return {
2361
+ ...parentReducers
2362
+ };
2363
+ };
2364
+ const _renderHook45 = renderHook(() => {
2365
+ return useUseCase(5, childUseCase);
2366
+ }),
2367
+ result = _renderHook45.result,
2368
+ unmount = _renderHook45.unmount;
2369
+ const current = result.current;
2370
+ const _current13 = _slicedToArray(current, 2),
2371
+ setEntity = _current13[1].setEntity;
2372
+ expect(onParentBind).toHaveBeenCalledTimes(1);
2373
+ expect(onParentBind).toHaveBeenCalledWith(5);
2374
+ expect(onParentChange).toHaveBeenCalledTimes(0);
2375
+ expect(onChildBind).toHaveBeenCalledTimes(1);
2376
+ expect(onChildBind).toHaveBeenCalledWith(5);
2377
+ expect(onChildChange).toHaveBeenCalledTimes(0);
2378
+ act(() => {
2379
+ setEntity(100);
2380
+ });
2381
+ expect(onParentBind).toHaveBeenCalledTimes(1);
2382
+ expect(onParentChange).toHaveBeenCalledTimes(1);
2383
+ expect(onParentChange).toHaveBeenCalledWith(100, 5);
2384
+ expect(onChildBind).toHaveBeenCalledTimes(1);
2385
+ expect(onChildChange).toHaveBeenCalledTimes(1);
2386
+ expect(onChildChange).toHaveBeenCalledWith(100, 5);
2387
+ expect(onParentUnbind).toHaveBeenCalledTimes(0);
2388
+ expect(onChildUnbind).toHaveBeenCalledTimes(0);
2389
+ unmount();
2390
+ expect(onParentUnbind).toHaveBeenCalledTimes(1);
2391
+ expect(onParentUnbind).toHaveBeenCalledWith(100);
2392
+ expect(onChildUnbind).toHaveBeenCalledTimes(1);
2393
+ expect(onChildUnbind).toHaveBeenCalledWith(100);
2394
+ });
2163
2395
  });
2164
2396
  describe('`useUseCase` should work the same as `useContextualCoreCollection`', () => {
2165
2397
  test('Provider should be returned in child component', () => {
@@ -2167,9 +2399,9 @@ describe('useUseCase', () => {
2167
2399
  const TestChild = () => {
2168
2400
  const core = useUseCase(fileUseCase);
2169
2401
  const length = core.length;
2170
- const _core = _slicedToArray(core, 3),
2171
- file = _core[0],
2172
- Provider = _core[2];
2402
+ const _core4 = _slicedToArray(core, 3),
2403
+ file = _core4[0],
2404
+ Provider = _core4[2];
2173
2405
  useEffect(() => {
2174
2406
  onCore(file, length, typeof Provider);
2175
2407
  });
@@ -2458,7 +2690,7 @@ describe('useUseCase', () => {
2458
2690
  fireEvent.click(screen.getByText(updateButtonText));
2459
2691
  expect(addReducer).toHaveBeenCalledTimes(3);
2460
2692
  });
2461
- test('`options.onChange` event should be trigger in orders', () => {
2693
+ test('`options.onChange` event should be triggered in orders', () => {
2462
2694
  let changeTimes = 0;
2463
2695
  const updateChangeTimes = () => {
2464
2696
  return ++changeTimes;
@@ -2559,7 +2791,7 @@ describe('useUseCase', () => {
2559
2791
  path: PATH_1
2560
2792
  });
2561
2793
  });
2562
- test('`options.watch` should be trigger at child elements', () => {
2794
+ test('`options.watch` should be triggered at child components', () => {
2563
2795
  const onExtChange = jest.fn();
2564
2796
  const firstButtonText = 'first button';
2565
2797
  const A = () => {
@@ -2602,7 +2834,7 @@ describe('useUseCase', () => {
2602
2834
  oldValue: ''
2603
2835
  });
2604
2836
  });
2605
- test('`options.watch` should be trigger in orders', () => {
2837
+ test('`options.watch` should be triggered in orders', () => {
2606
2838
  let changeTimes = 0;
2607
2839
  const updateChangeTimes = e => {
2608
2840
  void e;
@@ -2721,38 +2953,38 @@ describe('useUseCase', () => {
2721
2953
  });
2722
2954
  describe('`useUseCase` should work the same as `useReducers`', () => {
2723
2955
  test('`entity` should equal `defaultFile`', () => {
2724
- const _renderHook42 = renderHook(() => {
2956
+ const _renderHook46 = renderHook(() => {
2725
2957
  return useUseCase(mathUseCase);
2726
2958
  }),
2727
- result = _renderHook42.result;
2959
+ result = _renderHook46.result;
2728
2960
  const reducers = result.current;
2729
2961
  expect(Array.isArray(reducers)).toBe(false);
2730
2962
  });
2731
2963
  test('returned reducer should work', () => {
2732
- const _renderHook43 = renderHook(() => {
2964
+ const _renderHook47 = renderHook(() => {
2733
2965
  return useUseCase(mathUseCase);
2734
2966
  }),
2735
- result = _renderHook43.result;
2967
+ result = _renderHook47.result;
2736
2968
  const reducers = result.current;
2737
2969
  const add = reducers.add;
2738
2970
  expect(add(1, 2)).toBe(3);
2739
2971
  });
2740
2972
  test('`usecase` should be called only once if deps has not provided', () => {
2741
2973
  const mockUseCase = jest.fn(mathUseCase);
2742
- const _renderHook44 = renderHook(() => {
2974
+ const _renderHook48 = renderHook(() => {
2743
2975
  return useUseCase(mockUseCase);
2744
2976
  }),
2745
- rerender = _renderHook44.rerender;
2977
+ rerender = _renderHook48.rerender;
2746
2978
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2747
2979
  rerender();
2748
2980
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2749
2981
  });
2750
2982
  test('`usecase` should be called only once if deps has not changed', () => {
2751
2983
  const mockUseCase = jest.fn(mathUseCase);
2752
- const _renderHook45 = renderHook(() => {
2984
+ const _renderHook49 = renderHook(() => {
2753
2985
  return useUseCase(mockUseCase, {}, [1, 'x']);
2754
2986
  }),
2755
- rerender = _renderHook45.rerender;
2987
+ rerender = _renderHook49.rerender;
2756
2988
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2757
2989
  rerender();
2758
2990
  expect(mockUseCase).toHaveBeenCalledTimes(1);
@@ -2760,10 +2992,10 @@ describe('useUseCase', () => {
2760
2992
  test('`usecase` should be called if deps has changed', () => {
2761
2993
  let i = 0;
2762
2994
  const mockUseCase = jest.fn(mathUseCase);
2763
- const _renderHook46 = renderHook(() => {
2995
+ const _renderHook50 = renderHook(() => {
2764
2996
  return useUseCase(mockUseCase, {}, [i++, 'x']);
2765
2997
  }),
2766
- rerender = _renderHook46.rerender;
2998
+ rerender = _renderHook50.rerender;
2767
2999
  expect(mockUseCase).toHaveBeenCalledTimes(1);
2768
3000
  rerender();
2769
3001
  expect(mockUseCase).toHaveBeenCalledTimes(2);
@@ -2847,12 +3079,12 @@ describe('useUseCase', () => {
2847
3079
  expect(subtractionReducer).toHaveReturnedWith(4);
2848
3080
  });
2849
3081
  test('`options.extraValue` should be added after call `add` method', () => {
2850
- const _renderHook47 = renderHook(() => {
3082
+ const _renderHook51 = renderHook(() => {
2851
3083
  return useUseCase(mathUseCase, {
2852
3084
  extraValue: 5
2853
3085
  });
2854
3086
  }),
2855
- result = _renderHook47.result;
3087
+ result = _renderHook51.result;
2856
3088
  const reducers = result.current;
2857
3089
  const add = reducers.add;
2858
3090
  expect(add(1, 2)).toBe(8);
@@ -2860,7 +3092,7 @@ describe('useUseCase', () => {
2860
3092
  test('when `options` has changed, it should not trigger update', () => {
2861
3093
  const onUpdate = jest.fn();
2862
3094
  const onDeepUpdate = jest.fn();
2863
- const _renderHook48 = renderHook(() => {
3095
+ const _renderHook52 = renderHook(() => {
2864
3096
  const _useState7 = useState(0),
2865
3097
  _useState8 = _slicedToArray(_useState7, 2),
2866
3098
  extraValue = _useState8[0],
@@ -2876,7 +3108,7 @@ describe('useUseCase', () => {
2876
3108
  }, [reducers]);
2877
3109
  return setExtraValue;
2878
3110
  }),
2879
- result = _renderHook48.result;
3111
+ result = _renderHook52.result;
2880
3112
  expect(onUpdate).toHaveBeenCalledTimes(0);
2881
3113
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
2882
3114
  act(() => {
@@ -2888,7 +3120,7 @@ describe('useUseCase', () => {
2888
3120
  test('when `deps` change, it should update reducers', () => {
2889
3121
  const onUpdate = jest.fn();
2890
3122
  const onDeepUpdate = jest.fn();
2891
- const _renderHook49 = renderHook(() => {
3123
+ const _renderHook53 = renderHook(() => {
2892
3124
  const _useState9 = useState(0),
2893
3125
  _useState0 = _slicedToArray(_useState9, 2),
2894
3126
  extraValue = _useState0[0],
@@ -2904,7 +3136,7 @@ describe('useUseCase', () => {
2904
3136
  }, [reducers]);
2905
3137
  return setExtraValue;
2906
3138
  }),
2907
- result = _renderHook49.result;
3139
+ result = _renderHook53.result;
2908
3140
  expect(onUpdate).toHaveBeenCalledTimes(0);
2909
3141
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
2910
3142
  act(() => {
@@ -2916,7 +3148,7 @@ describe('useUseCase', () => {
2916
3148
  test('empty `deps` should not update reducers', () => {
2917
3149
  const onUpdate = jest.fn();
2918
3150
  const onDeepUpdate = jest.fn();
2919
- const _renderHook50 = renderHook(() => {
3151
+ const _renderHook54 = renderHook(() => {
2920
3152
  const _useState1 = useState(0),
2921
3153
  _useState10 = _slicedToArray(_useState1, 2),
2922
3154
  extraValue = _useState10[0],
@@ -2932,7 +3164,7 @@ describe('useUseCase', () => {
2932
3164
  }, [reducers]);
2933
3165
  return setExtraValue;
2934
3166
  }),
2935
- result = _renderHook50.result;
3167
+ result = _renderHook54.result;
2936
3168
  expect(onUpdate).toHaveBeenCalledTimes(0);
2937
3169
  expect(onDeepUpdate).toHaveBeenCalledTimes(0);
2938
3170
  act(() => {