dyo-tools 0.1.0 → 0.3.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 (184) hide show
  1. package/.c8rc.json +4 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +47 -0
  4. package/Makefile +34 -0
  5. package/README.md +0 -7
  6. package/babel.config.js +1 -0
  7. package/cucumber-report.html +48 -0
  8. package/dist/constants.d.ts +6 -0
  9. package/dist/constants.js +63 -0
  10. package/dist/constants.js.map +1 -0
  11. package/dist/core/DTAction.d.ts +10 -0
  12. package/dist/core/DTAction.js +24 -0
  13. package/dist/core/DTAction.js.map +1 -0
  14. package/dist/core/DTBunch.d.ts +12 -17
  15. package/dist/core/DTBunch.js +39 -115
  16. package/dist/core/DTBunch.js.map +1 -1
  17. package/dist/core/DTComponent.d.ts +13 -5
  18. package/dist/core/DTComponent.js +39 -1
  19. package/dist/core/DTComponent.js.map +1 -1
  20. package/dist/core/DTComponentPhysical.d.ts +12 -0
  21. package/dist/core/DTComponentPhysical.js +22 -0
  22. package/dist/core/DTComponentPhysical.js.map +1 -0
  23. package/dist/core/DTComponentWithMeta.d.ts +2 -2
  24. package/dist/core/DTComponentWithMeta.js.map +1 -1
  25. package/dist/core/DTElement.d.ts +7 -7
  26. package/dist/core/DTElement.js +12 -11
  27. package/dist/core/DTElement.js.map +1 -1
  28. package/dist/core/DTManager.d.ts +33 -0
  29. package/dist/core/DTManager.js +190 -0
  30. package/dist/core/DTManager.js.map +1 -0
  31. package/dist/core/DTMaster.d.ts +24 -0
  32. package/dist/core/DTMaster.js +133 -0
  33. package/dist/core/DTMaster.js.map +1 -0
  34. package/dist/core/DTModule.d.ts +14 -0
  35. package/dist/core/DTModule.js +39 -0
  36. package/dist/core/DTModule.js.map +1 -0
  37. package/dist/index.d.ts +7 -1
  38. package/dist/index.js +15 -3
  39. package/dist/index.js.map +1 -1
  40. package/dist/libs/DYOFinder.d.ts +10 -0
  41. package/dist/libs/DYOFinder.js +96 -0
  42. package/dist/libs/DYOFinder.js.map +1 -0
  43. package/dist/libs/player/DTPlayer.element.d.ts +5 -0
  44. package/dist/libs/player/DTPlayer.element.js +11 -0
  45. package/dist/libs/player/DTPlayer.element.js.map +1 -0
  46. package/dist/libs/player/DTPlayer.manager.d.ts +11 -0
  47. package/dist/libs/player/DTPlayer.manager.js +42 -0
  48. package/dist/libs/player/DTPlayer.manager.js.map +1 -0
  49. package/dist/tsconfig.tsbuildinfo +1 -1321
  50. package/dist/types/core.d.ts +103 -0
  51. package/dist/types/core.js +15 -0
  52. package/dist/types/core.js.map +1 -0
  53. package/dist/types/index.d.ts +2 -58
  54. package/dist/types/index.js +16 -12
  55. package/dist/types/index.js.map +1 -1
  56. package/dist/types/player.d.ts +5 -0
  57. package/dist/types/player.js +3 -0
  58. package/dist/types/player.js.map +1 -0
  59. package/docs/.nojekyll +1 -0
  60. package/docs/assets/highlight.css +29 -0
  61. package/docs/assets/main.js +59 -0
  62. package/docs/assets/navigation.js +1 -0
  63. package/docs/assets/search.js +1 -0
  64. package/docs/assets/style.css +1414 -0
  65. package/docs/classes/core_DTAction.DYOToolsAction.html +89 -0
  66. package/docs/classes/core_DTBunch.DYOToolsBunch.html +254 -0
  67. package/docs/classes/core_DTComponent.DYOToolsComponent.html +76 -0
  68. package/docs/classes/core_DTComponentPhysical.DYOToolsComponentPhysical.html +110 -0
  69. package/docs/classes/core_DTComponentWithMeta.DYOToolsComponentWithMeta.html +96 -0
  70. package/docs/classes/core_DTElement.DYOToolsElement.html +135 -0
  71. package/docs/classes/core_DTError.DYOToolsError.html +37 -0
  72. package/docs/classes/core_DTManager.DYOToolsManager.html +237 -0
  73. package/docs/classes/core_DTMaster.DYOToolsMaster.html +150 -0
  74. package/docs/classes/core_DTModule.DYOToolsModule.html +92 -0
  75. package/docs/classes/libs_DYOFinder.DYOFinder.html +34 -0
  76. package/docs/classes/libs_player_DTPlayer_element.DYOToolsPlayer.html +134 -0
  77. package/docs/classes/libs_player_DTPlayer_manager.DYOToolsPlayerManager.html +243 -0
  78. package/docs/enums/types_core.FilterOperatorType.html +10 -0
  79. package/docs/hierarchy.html +1 -0
  80. package/docs/index.html +1 -0
  81. package/docs/interfaces/types_core.DTBunchFilters.html +6 -0
  82. package/docs/interfaces/types_core.DTBunchOptions.html +19 -0
  83. package/docs/interfaces/types_core.DTBunchToObject.html +7 -0
  84. package/docs/interfaces/types_core.DTComponentOptions.html +5 -0
  85. package/docs/interfaces/types_core.DTComponentToObject.html +4 -0
  86. package/docs/interfaces/types_core.DTElementToObject.html +7 -0
  87. package/docs/interfaces/types_core.DTManagerFilters.html +6 -0
  88. package/docs/interfaces/types_core.DTManagerOptions.html +8 -0
  89. package/docs/interfaces/types_core.DTManagerToObject.html +5 -0
  90. package/docs/interfaces/types_core.DTMasterToObject.html +8 -0
  91. package/docs/interfaces/types_core.DTModuleToObject.html +6 -0
  92. package/docs/interfaces/types_core.DYOFinderConfigurationPropDefault.html +4 -0
  93. package/docs/interfaces/types_core.DYOFinderConfigurationPropObjectSearch.html +4 -0
  94. package/docs/interfaces/types_core.DYOFinderFilterOperatorAdvanced.html +5 -0
  95. package/docs/interfaces/types_core.DYOFinderFilterOperatorBase.html +5 -0
  96. package/docs/interfaces/types_player.DTPlayerManagerSimpleConfiguration.html +3 -0
  97. package/docs/modules/constants.html +6 -0
  98. package/docs/modules/core_DTAction.html +2 -0
  99. package/docs/modules/core_DTBunch.html +2 -0
  100. package/docs/modules/core_DTComponent.html +2 -0
  101. package/docs/modules/core_DTComponentPhysical.html +2 -0
  102. package/docs/modules/core_DTComponentWithMeta.html +2 -0
  103. package/docs/modules/core_DTElement.html +2 -0
  104. package/docs/modules/core_DTError.html +2 -0
  105. package/docs/modules/core_DTManager.html +2 -0
  106. package/docs/modules/core_DTMaster.html +2 -0
  107. package/docs/modules/core_DTModule.html +2 -0
  108. package/docs/modules/libs_DYOFinder.html +2 -0
  109. package/docs/modules/libs_player_DTPlayer_element.html +2 -0
  110. package/docs/modules/libs_player_DTPlayer_manager.html +2 -0
  111. package/docs/modules/types.html +29 -0
  112. package/docs/modules/types_core.html +28 -0
  113. package/docs/modules/types_player.html +2 -0
  114. package/docs/types/types_core.DTAcceptedMetaData.html +2 -0
  115. package/docs/types/types_core.DTManagerItemType.html +1 -0
  116. package/docs/types/types_core.DTManagerItemsType.html +2 -0
  117. package/docs/types/types_core.DYOFinderComponentType.html +1 -0
  118. package/docs/types/types_core.DYOFinderConfiguration.html +2 -0
  119. package/docs/types/types_core.DYOFinderConfigurationProp.html +1 -0
  120. package/docs/types/types_core.DYOFinderFilterOperator.html +1 -0
  121. package/docs/types/types_core.DYOFinderFilterOperatorArgument.html +1 -0
  122. package/docs/types/types_core.DYOFinderFilters.html +1 -0
  123. package/docs/types/types_core.StandardPrimitiveType.html +2 -0
  124. package/docs/types/types_core.StandardPrimitiveTypeWithArray.html +1 -0
  125. package/docs/variables/constants.bunchDefaultOptions.html +2 -0
  126. package/docs/variables/constants.componentBunchDefaultFinderConfiguration.html +1 -0
  127. package/docs/variables/constants.componentManagerDefaultFinderConfiguration.html +1 -0
  128. package/docs/variables/constants.componentPhysicalDefaultFinderConfiguration.html +1 -0
  129. package/docs/variables/constants.managerDefaultOptions.html +2 -0
  130. package/jest.config.js +6 -0
  131. package/package.json +32 -23
  132. package/src/constants.ts +85 -0
  133. package/src/core/DTAction.ts +52 -0
  134. package/src/core/DTBunch.ts +467 -0
  135. package/src/core/DTComponent.ts +225 -0
  136. package/src/core/DTComponentPhysical.ts +53 -0
  137. package/src/core/DTComponentWithMeta.ts +65 -0
  138. package/src/core/DTElement.ts +102 -0
  139. package/src/core/DTError.ts +78 -0
  140. package/src/core/DTManager.ts +465 -0
  141. package/src/core/DTMaster.ts +318 -0
  142. package/src/core/DTModule.ts +90 -0
  143. package/src/index.ts +17 -0
  144. package/src/libs/DYOFinder.ts +175 -0
  145. package/src/libs/player/DTPlayer.element.ts +9 -0
  146. package/src/libs/player/DTPlayer.manager.ts +84 -0
  147. package/src/types/core.ts +169 -0
  148. package/src/types/index.ts +2 -0
  149. package/src/types/player.ts +6 -0
  150. package/test/core/DTAction.double.ts +78 -0
  151. package/test/core/DTAction.spec.ts +76 -0
  152. package/test/core/DTBunch.double.ts +261 -0
  153. package/test/core/DTBunch.spec.ts +897 -0
  154. package/test/core/DTComponent.double.ts +164 -0
  155. package/test/core/DTComponent.spec.ts +295 -0
  156. package/test/core/DTComponentPhysical.double.ts +76 -0
  157. package/test/core/DTComponentPhysical.spec.ts +78 -0
  158. package/test/core/DTComponentWithMeta.double.ts +115 -0
  159. package/test/core/DTComponentWithMeta.spec.ts +124 -0
  160. package/test/core/DTElement.double.ts +163 -0
  161. package/test/core/DTElement.spec.ts +146 -0
  162. package/test/core/DTError.double.ts +92 -0
  163. package/test/core/DTError.spec.ts +89 -0
  164. package/test/core/DTManager.double.ts +216 -0
  165. package/test/core/DTManager.spec.ts +965 -0
  166. package/test/core/DTMaster.double.ts +141 -0
  167. package/test/core/DTMaster.spec.ts +584 -0
  168. package/test/core/DTModule.double.ts +80 -0
  169. package/test/core/DTModule.spec.ts +138 -0
  170. package/test/core/copy.spec.ts +243 -0
  171. package/test/libs/DYOFinder.double.ts +152 -0
  172. package/test/libs/DYOFinder.spec.ts +194 -0
  173. package/test/libs/player/DTPlayer.element.double.ts +55 -0
  174. package/test/libs/player/DTPlayer.element.spec.ts +28 -0
  175. package/test/libs/player/DTPlayer.manager.double.ts +92 -0
  176. package/test/libs/player/DTPlayer.manager.spec.ts +178 -0
  177. package/tsconfig.dev.json +22 -0
  178. package/tsconfig.json +21 -0
  179. package/dist/core/DTPlayer.d.ts +0 -8
  180. package/dist/core/DTPlayer.js +0 -30
  181. package/dist/core/DTPlayer.js.map +0 -1
  182. package/dist/utils/filters.d.ts +0 -6
  183. package/dist/utils/filters.js +0 -39
  184. package/dist/utils/filters.js.map +0 -1
@@ -0,0 +1,584 @@
1
+ import {afterEach, beforeEach, describe, expect, jest, test} from "@jest/globals";
2
+ import {DTAction, DTComponent, DTManager, DTMaster, DTModule} from "../../src";
3
+ import {
4
+ checkMasterActions,
5
+ checkMasterManagers, checkMasterModules,
6
+ DTMasterTest,
7
+ IDTest,
8
+ KeyTest,
9
+ populateMaster
10
+ } from "./DTMaster.double";
11
+ import {mockOverriddenMethods} from "./DTComponent.double";
12
+ import {
13
+ DTManagerStub, IDTest as IDManagerTest,
14
+ IDTest as manager1IDTest,
15
+ manager1IDTest as manager2IDTest,
16
+ manager1KeyTest as manager2KeyTest, manager1ToObject
17
+ } from "./DTManager.double";
18
+ import {
19
+ DTActionStub,
20
+ IDTest as action1IDTest,
21
+ KeyTest as action1KeyTest,
22
+ action1IDTest as action2IDTest,
23
+ action1KeyTest as action2KeyTest, action1ToObject
24
+ } from "./DTAction.double";
25
+ import {
26
+ DTModuleStub,
27
+ IDTest as module1IDTest,
28
+ KeyTest as module1KeyTest,
29
+ module1IDTest as module2IDTest,
30
+ module1KeyTest as module2KeyTest, module1ToObject
31
+ } from "./DTModule.double";
32
+ import DYOToolsError from "../../src/core/DTError";
33
+ import {checkCallForMockedDTError} from "./DTError.double";
34
+ import MockedFunction = jest.MockedFunction;
35
+ import {DTBunchStubLibrary} from "./DTBunch.double";
36
+
37
+ /** ****************** MOCK DEPENDENCIES
38
+ * Dependencies used by the component are mocked with Jest
39
+ * **** */
40
+ jest.mock('../../src/core/DTManager');
41
+ jest.mock('../../src/core/DTAction');
42
+ jest.mock('../../src/core/DTModule');
43
+ jest.mock('../../src/core/DTComponent');
44
+ jest.mock('../../src/core/DTError');
45
+ // Add specific mock for inherited methods to have a basic implementation
46
+ mockOverriddenMethods(DTComponent);
47
+
48
+ /** *********************** TESTS SUITES ****************************** */
49
+ describe('class DYOToolsMaster', () => {
50
+ let masterTest: DTMasterTest;
51
+
52
+ beforeEach(() => {
53
+ masterTest = new DTMasterTest();
54
+ masterTest.th_set_id(IDTest);
55
+ masterTest.th_set_key(KeyTest);
56
+ });
57
+
58
+ afterEach(() => {
59
+ jest.resetAllMocks();
60
+ });
61
+
62
+ describe('inheritance', () => {
63
+ test('check good inheritance for class', () => {
64
+ expect(DTMaster.prototype instanceof DTComponent).toBeTruthy();
65
+ });
66
+ });
67
+
68
+ describe('_componentType', () => {
69
+ test('componentType must be "master"', () => {
70
+ expect(masterTest.th_get_componentType()).toBe('master');
71
+ });
72
+ });
73
+
74
+ describe('constructor()', () => {
75
+ beforeEach(() => {
76
+ jest.resetAllMocks();
77
+ });
78
+
79
+ test('simple creation with key', () => {
80
+ const newMaster = new DTMasterTest(KeyTest);
81
+ const parentConstructorMock = (DTComponent.prototype.constructor as MockedFunction<(key: string, options: any) => void>).mock;
82
+
83
+ expect(parentConstructorMock.calls.length).toBe(1);
84
+ expect(parentConstructorMock.calls[0][0]).toBe(KeyTest);
85
+
86
+ expect(newMaster.th_get_managers()).toStrictEqual({});
87
+ expect(newMaster.th_get_actions()).toStrictEqual({});
88
+ expect(newMaster.th_get_modules()).toStrictEqual({});
89
+ });
90
+
91
+ });
92
+
93
+ describe('addManager()', () => {
94
+ let managerToAdd: DTManagerStub;
95
+
96
+ beforeEach(() => {
97
+ managerToAdd = new DTManagerStub();
98
+
99
+ jest.spyOn(masterTest, 'getId').mockReturnValue(IDTest);
100
+ });
101
+
102
+ test('add a new manager - standard case', () => {
103
+ masterTest.addManager(managerToAdd);
104
+
105
+ checkMasterManagers(masterTest, [manager1IDTest]);
106
+ });
107
+
108
+ test('trigger error when adding two managers with same id', () => {
109
+ const mockedTriggerError: MockedFunction<(error: DYOToolsError) => void> = DTMaster.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
110
+
111
+ masterTest.addManager(managerToAdd);
112
+ masterTest.addManager(managerToAdd);
113
+
114
+ expect(Object.keys(masterTest.th_get_managers()).length).toBe(1);
115
+ expect(mockedTriggerError.mock.calls.length).toBe(1);
116
+ checkCallForMockedDTError(
117
+ 'id_conflict',
118
+ 'Manager with same id already exists in the Master instance',
119
+ IDTest,
120
+ managerToAdd.getId(),
121
+ );
122
+ });
123
+
124
+ test('trigger error when adding two managers with same key', () => {
125
+ const mockedTriggerError: MockedFunction<(error: DYOToolsError) => void> = DTMaster.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
126
+ const managerToAdd2 = new DTManagerStub();
127
+ jest.spyOn(managerToAdd2, 'getId').mockReturnValue(manager2IDTest);
128
+
129
+ masterTest.addManager(managerToAdd);
130
+ masterTest.addManager(managerToAdd2);
131
+
132
+ expect(Object.keys(masterTest.th_get_managers()).length).toBe(1);
133
+ expect(mockedTriggerError.mock.calls.length).toBe(1);
134
+ checkCallForMockedDTError(
135
+ 'key_conflict',
136
+ 'Manager with same key already exists in the Master instance',
137
+ IDTest,
138
+ managerToAdd2.getId(),
139
+ );
140
+ });
141
+
142
+ test('set context when adding a new manager', () => {
143
+ masterTest.addManager(managerToAdd);
144
+
145
+ expect((managerToAdd.setContext as any).mock.calls.length).toBe(1);
146
+ expect((managerToAdd.setContext as any).mock.calls[0][0].th_get_id()).toBe(IDTest);
147
+ });
148
+ });
149
+
150
+ describe('getManager()', () => {
151
+ beforeEach(() => {
152
+ populateMaster(masterTest);
153
+ });
154
+
155
+ test('return a manager by its id', () => {
156
+ const manager = masterTest.getManager(manager1IDTest) as DTManagerStub;
157
+
158
+ expect(manager).toBeDefined();
159
+ expect(manager.th_get_id()).toBe(manager1IDTest);
160
+ });
161
+
162
+ test('return a manager by its key', () => {
163
+ const manager = masterTest.getManager(manager2KeyTest) as DTManagerStub;
164
+
165
+ expect(manager).toBeDefined();
166
+ expect(manager.th_get_id()).toBe(manager2IDTest);
167
+ });
168
+
169
+ test('return undefined if manager is not found', () => {
170
+ const manager = masterTest.getManager('unexisting-id');
171
+
172
+ expect(manager).toBeUndefined();
173
+ });
174
+ });
175
+
176
+ describe('removeManager()', () => {
177
+ beforeEach(() => {
178
+ populateMaster(masterTest);
179
+
180
+ const managers = masterTest.th_get_managers();
181
+ jest.spyOn(masterTest, 'getManager').mockImplementation(function(idOrKey: string): DTManager<any> | undefined {
182
+ if (idOrKey === manager2KeyTest) {
183
+ return managers[manager2IDTest];
184
+ } else if (idOrKey === 'unexisting-id') {
185
+ return undefined;
186
+ } else {
187
+ return managers[manager1IDTest];
188
+ }
189
+ });
190
+ });
191
+
192
+ test('remove a manager by its id', () => {
193
+ masterTest.removeManager(manager1IDTest);
194
+
195
+ expect(Object.keys(masterTest.th_get_managers()).length).toBe(1);
196
+ expect(Object.keys(masterTest.th_get_managers())[0]).toBe(manager2IDTest);
197
+ });
198
+
199
+ test('remove a manager by its key', () => {
200
+ masterTest.removeManager(manager2KeyTest);
201
+
202
+ expect(Object.keys(masterTest.th_get_managers()).length).toBe(1);
203
+ expect(Object.keys(masterTest.th_get_managers())[0]).toBe(manager1IDTest);
204
+ });
205
+
206
+ test('no removal if manager is not found', () => {
207
+ masterTest.removeManager('unexisting-id');
208
+
209
+ expect(Object.keys(masterTest.th_get_managers()).length).toBe(2);
210
+ expect(Object.keys(masterTest.th_get_managers())[0]).toBe(manager1IDTest);
211
+ expect(Object.keys(masterTest.th_get_managers())[1]).toBe(manager2IDTest);
212
+ });
213
+ });
214
+
215
+ describe('addAction()', () => {
216
+ let actionToAdd: DTActionStub;
217
+
218
+ beforeEach(() => {
219
+ actionToAdd = new DTActionStub();
220
+
221
+ jest.spyOn(masterTest, 'getId').mockReturnValue(IDTest);
222
+ });
223
+
224
+ test('add a new action - standard case', () => {
225
+ masterTest.addAction(actionToAdd);
226
+
227
+ checkMasterActions(masterTest, [action1IDTest]);
228
+ });
229
+
230
+ test('trigger error when adding two actions with same id', () => {
231
+ const mockedTriggerError: MockedFunction<(error: DYOToolsError) => void> = DTMaster.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
232
+
233
+ masterTest.addAction(actionToAdd);
234
+ masterTest.addAction(actionToAdd);
235
+
236
+ expect(Object.keys(masterTest.th_get_actions()).length).toBe(1);
237
+ expect(mockedTriggerError.mock.calls.length).toBe(1);
238
+ checkCallForMockedDTError(
239
+ 'id_conflict',
240
+ 'Action with same id already exists in the Master instance',
241
+ IDTest,
242
+ actionToAdd.getId(),
243
+ );
244
+ });
245
+
246
+ test('trigger error when adding two actions with same key', () => {
247
+ const mockedTriggerError: MockedFunction<(error: DYOToolsError) => void> = DTMaster.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
248
+ const actionToAdd2 = new DTActionStub();
249
+ jest.spyOn(actionToAdd2, 'getId').mockReturnValue(action2IDTest);
250
+
251
+ masterTest.addAction(actionToAdd);
252
+ masterTest.addAction(actionToAdd2);
253
+
254
+ expect(Object.keys(masterTest.th_get_actions()).length).toBe(1);
255
+ expect(mockedTriggerError.mock.calls.length).toBe(1);
256
+ checkCallForMockedDTError(
257
+ 'key_conflict',
258
+ 'Action with same key already exists in the Master instance',
259
+ IDTest,
260
+ actionToAdd2.getId(),
261
+ );
262
+ });
263
+
264
+ test('set context when adding a new action', () => {
265
+ masterTest.addAction(actionToAdd);
266
+
267
+ expect((actionToAdd.setContext as any).mock.calls.length).toBe(1);
268
+ expect((actionToAdd.setContext as any).mock.calls[0][0].th_get_id()).toBe(IDTest);
269
+ });
270
+ });
271
+
272
+ describe('getAction()', () => {
273
+ beforeEach(() => {
274
+ populateMaster(masterTest);
275
+ });
276
+
277
+ test('return an action by its id', () => {
278
+ const action = masterTest.getAction(action1IDTest) as DTActionStub;
279
+
280
+ expect(action).toBeDefined();
281
+ expect(action.th_get_id()).toBe(action1IDTest);
282
+ });
283
+
284
+ test('return an action by its key', () => {
285
+ const action = masterTest.getAction(action2KeyTest) as DTActionStub;
286
+
287
+ expect(action).toBeDefined();
288
+ expect(action.th_get_id()).toBe(action2IDTest);
289
+ });
290
+
291
+ test('return undefined if action is not found', () => {
292
+ const action = masterTest.getAction('unexisting-id');
293
+
294
+ expect(action).toBeUndefined();
295
+ });
296
+ });
297
+
298
+ describe('removeAction()', () => {
299
+ beforeEach(() => {
300
+ populateMaster(masterTest);
301
+
302
+ const actions = masterTest.th_get_actions();
303
+ jest.spyOn(masterTest, 'getAction').mockImplementation(function(idOrKey: string): DTAction | undefined {
304
+ if (idOrKey === action2KeyTest) {
305
+ return actions[action2IDTest];
306
+ } else if (idOrKey === 'unexisting-id') {
307
+ return undefined;
308
+ } else {
309
+ return actions[action1IDTest];
310
+ }
311
+ });
312
+ });
313
+
314
+ test('remove an action by its id', () => {
315
+ masterTest.removeAction(action1IDTest);
316
+
317
+ expect(Object.keys(masterTest.th_get_actions()).length).toBe(1);
318
+ expect(Object.keys(masterTest.th_get_actions())[0]).toBe(action2IDTest);
319
+ });
320
+
321
+ test('remove an action by its key', () => {
322
+ masterTest.removeAction(action2KeyTest);
323
+
324
+ expect(Object.keys(masterTest.th_get_actions()).length).toBe(1);
325
+ expect(Object.keys(masterTest.th_get_actions())[0]).toBe(action1IDTest);
326
+ });
327
+
328
+ test('no removal if action is not found', () => {
329
+ masterTest.removeAction('unexisting-id');
330
+
331
+ expect(Object.keys(masterTest.th_get_actions()).length).toBe(2);
332
+ expect(Object.keys(masterTest.th_get_actions())[0]).toBe(action1IDTest);
333
+ expect(Object.keys(masterTest.th_get_actions())[1]).toBe(action2IDTest);
334
+ });
335
+ });
336
+
337
+ describe('executeAction()', () => {
338
+ beforeEach(() => {
339
+ populateMaster(masterTest);
340
+ const actions = masterTest.th_get_actions();
341
+
342
+ jest.spyOn(masterTest, 'getAction').mockImplementation(function(idOrKey: string): DTAction | undefined {
343
+ if (idOrKey === action2KeyTest) {
344
+ return actions[action2IDTest];
345
+ } else if (idOrKey === 'unexisting-id') {
346
+ return undefined;
347
+ } else {
348
+ return actions[action1IDTest];
349
+ }
350
+ });
351
+ });
352
+
353
+ test('call execute method with payload for action by its id', async () => {
354
+ const payload = 'execute action from master by id';
355
+ const expectedPayload = `${action1KeyTest} : execute action from master by id`;
356
+
357
+ await expect(masterTest.executeAction(action1IDTest, payload)).rejects.toBe(expectedPayload);
358
+ });
359
+
360
+ test('call execute method with payload for action by its key', async () => {
361
+ const payload = 'execute action from master by key';
362
+ const expectedPayload = `${action2KeyTest} : execute action from master by key`;
363
+
364
+ await expect(masterTest.executeAction(action2KeyTest, payload)).rejects.toBe(expectedPayload);
365
+ });
366
+
367
+ test('no call of execute if action not found', async () => {
368
+ const payload = 'execute action from master';
369
+
370
+ // The assertion here only test that the execute method is not called, so there is no promise rejection
371
+ await expect(masterTest.executeAction('unexisting-id', payload)).resolves.toBeUndefined();
372
+ });
373
+ });
374
+
375
+ describe('addModule()', () => {
376
+ let moduleToAdd: DTModuleStub;
377
+
378
+ beforeEach(() => {
379
+ moduleToAdd = new DTModuleStub();
380
+
381
+ jest.spyOn(masterTest, 'getId').mockReturnValue(IDTest);
382
+ });
383
+
384
+ test('add a new module - standard case', () => {
385
+ masterTest.addModule(moduleToAdd);
386
+
387
+ checkMasterModules(masterTest, [module1IDTest]);
388
+ });
389
+
390
+ test('trigger error when adding two modules with same id', () => {
391
+ const mockedTriggerError: MockedFunction<(error: DYOToolsError) => void> = DTMaster.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
392
+
393
+ masterTest.addModule(moduleToAdd);
394
+ masterTest.addModule(moduleToAdd);
395
+
396
+ expect(Object.keys(masterTest.th_get_modules()).length).toBe(1);
397
+ expect(mockedTriggerError.mock.calls.length).toBe(1);
398
+ checkCallForMockedDTError(
399
+ 'id_conflict',
400
+ 'Module with same id already exists in the Master instance',
401
+ IDTest,
402
+ moduleToAdd.getId(),
403
+ );
404
+ });
405
+
406
+ test('trigger error when adding two modules with same key', () => {
407
+ const mockedTriggerError: MockedFunction<(error: DYOToolsError) => void> = DTMaster.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
408
+ const moduleToAdd2 = new DTModuleStub();
409
+ jest.spyOn(moduleToAdd2, 'getId').mockReturnValue(module2IDTest);
410
+
411
+ masterTest.addModule(moduleToAdd);
412
+ masterTest.addModule(moduleToAdd2);
413
+
414
+ expect(Object.keys(masterTest.th_get_modules()).length).toBe(1);
415
+ expect(mockedTriggerError.mock.calls.length).toBe(1);
416
+ checkCallForMockedDTError(
417
+ 'key_conflict',
418
+ 'Module with same key already exists in the Master instance',
419
+ IDTest,
420
+ moduleToAdd2.getId(),
421
+ );
422
+ });
423
+
424
+ test('set context when adding a new module', () => {
425
+ masterTest.addModule(moduleToAdd);
426
+
427
+ expect((moduleToAdd.setContext as any).mock.calls.length).toBe(1);
428
+ expect((moduleToAdd.setContext as any).mock.calls[0][0].th_get_id()).toBe(IDTest);
429
+ });
430
+ });
431
+
432
+ describe('getModule()', () => {
433
+ beforeEach(() => {
434
+ populateMaster(masterTest);
435
+ });
436
+
437
+ test('return a module by its id', () => {
438
+ const module = masterTest.getModule(module1IDTest) as DTModuleStub;
439
+
440
+ expect(module).toBeDefined();
441
+ expect(module.th_get_id()).toBe(module1IDTest);
442
+ });
443
+
444
+ test('return a module by its key', () => {
445
+ const module = masterTest.getModule(module2KeyTest) as DTModuleStub;
446
+
447
+ expect(module).toBeDefined();
448
+ expect(module.th_get_id()).toBe(module2IDTest);
449
+ });
450
+
451
+ test('return undefined if module is not found', () => {
452
+ const module = masterTest.getModule('unexisting-id');
453
+
454
+ expect(module).toBeUndefined();
455
+ });
456
+ });
457
+
458
+ describe('removeModule()', () => {
459
+ beforeEach(() => {
460
+ populateMaster(masterTest);
461
+
462
+ const modules = masterTest.th_get_modules();
463
+ jest.spyOn(masterTest, 'getModule').mockImplementation(function(idOrKey: string): DTModule | undefined {
464
+ if (idOrKey === module2KeyTest) {
465
+ return modules[module2IDTest];
466
+ } else if (idOrKey === 'unexisting-id') {
467
+ return undefined;
468
+ } else {
469
+ return modules[module1IDTest];
470
+ }
471
+ });
472
+ });
473
+
474
+ test('remove a module by its id', () => {
475
+ masterTest.removeModule(module1IDTest);
476
+
477
+ expect(Object.keys(masterTest.th_get_modules()).length).toBe(1);
478
+ expect(Object.keys(masterTest.th_get_modules())[0]).toBe(module2IDTest);
479
+ });
480
+
481
+ test('remove a module by its key', () => {
482
+ masterTest.removeModule(module2KeyTest);
483
+
484
+ expect(Object.keys(masterTest.th_get_modules()).length).toBe(1);
485
+ expect(Object.keys(masterTest.th_get_modules())[0]).toBe(module1IDTest);
486
+ });
487
+
488
+ test('no removal if module is not found', () => {
489
+ masterTest.removeModule('unexisting-id');
490
+
491
+ expect(Object.keys(masterTest.th_get_modules()).length).toBe(2);
492
+ expect(Object.keys(masterTest.th_get_modules())[0]).toBe(module1IDTest);
493
+ expect(Object.keys(masterTest.th_get_modules())[1]).toBe(module2IDTest);
494
+ });
495
+ });
496
+
497
+ describe('toString()', () => {
498
+ beforeEach(() => {
499
+ masterTest.th_set_key(KeyTest);
500
+ });
501
+
502
+ test('string output standard', () => {
503
+ const toStringMaster = masterTest.toString();
504
+ const expectedString = `Component ${KeyTest} - Type: Master - Managers: 0 - Actions: 0 - Modules: 0`;
505
+
506
+ expect(toStringMaster).toBe(expectedString);
507
+ });
508
+
509
+ test('string output standard with all components', () => {
510
+ populateMaster(masterTest);
511
+
512
+ const toStringMaster = masterTest.toString();
513
+ const expectedString = `Component ${KeyTest} - Type: Master - Managers: 2 - Actions: 2 - Modules: 2`;
514
+
515
+ expect(toStringMaster).toBe(expectedString);
516
+ });
517
+
518
+ test('string output without actions and modules', () => {
519
+ populateMaster(masterTest);
520
+ masterTest.th_set_actions({});
521
+ masterTest.th_set_modules({});
522
+
523
+ const toStringMaster = masterTest.toString();
524
+ const expectedString = `Component ${KeyTest} - Type: Master - Managers: 2 - Actions: 0 - Modules: 0`;
525
+
526
+ expect(toStringMaster).toBe(expectedString);
527
+ });
528
+ });
529
+
530
+ describe('toObject()', () => {
531
+ beforeEach(() => {
532
+ masterTest.th_set_id(IDTest);
533
+ masterTest.th_set_key(KeyTest);
534
+ });
535
+
536
+ test('toObject output standard', () => {
537
+ const toObjectMaster = masterTest.toObject();
538
+
539
+ expect(Object.keys(toObjectMaster)).toStrictEqual(['id', 'key', 'type', 'managers', 'actions', 'modules']);
540
+ expect(toObjectMaster.id).toBe(IDTest);
541
+ expect(toObjectMaster.key).toBe(KeyTest);
542
+ expect(toObjectMaster.type).toBe('master');
543
+ expect(toObjectMaster.managers).toStrictEqual([]);
544
+ expect(toObjectMaster.actions).toStrictEqual([]);
545
+ expect(toObjectMaster.modules).toStrictEqual([]);
546
+ });
547
+
548
+ test('toObject output with managers', () => {
549
+ const manager1 = new DTManagerStub();
550
+ jest.spyOn(manager1, 'toObject').mockImplementation(() => manager1ToObject);
551
+ masterTest.th_set_managers({ [manager1IDTest]: manager1 });
552
+
553
+ const toObjectMaster = masterTest.toObject();
554
+
555
+ expect(toObjectMaster.id).toBe(IDTest);
556
+ expect(toObjectMaster.key).toBe(KeyTest);
557
+ expect(toObjectMaster.type).toBe('master');
558
+ expect(toObjectMaster.managers).toStrictEqual([ manager1ToObject ]);
559
+ expect(toObjectMaster.actions).toStrictEqual([]);
560
+ expect(toObjectMaster.modules).toStrictEqual([]);
561
+ });
562
+
563
+ test('toObject output with managers, actions and modules', () => {
564
+ const manager1 = new DTManagerStub();
565
+ jest.spyOn(manager1, 'toObject').mockImplementation(() => manager1ToObject);
566
+ masterTest.th_set_managers({ [manager1IDTest]: manager1 });
567
+ const action1 = new DTActionStub();
568
+ jest.spyOn(action1, 'toObject').mockImplementation(() => action1ToObject);
569
+ masterTest.th_set_actions({ [action1IDTest]: action1 });
570
+ const module1 = new DTModuleStub();
571
+ jest.spyOn(module1, 'toObject').mockImplementation(() => module1ToObject);
572
+ masterTest.th_set_modules({ [module1IDTest]: module1 });
573
+
574
+ const toObjectMaster = masterTest.toObject();
575
+
576
+ expect(toObjectMaster.id).toBe(IDTest);
577
+ expect(toObjectMaster.key).toBe(KeyTest);
578
+ expect(toObjectMaster.type).toBe('master');
579
+ expect(toObjectMaster.managers).toStrictEqual([ manager1ToObject ]);
580
+ expect(toObjectMaster.actions).toStrictEqual([ action1ToObject ]);
581
+ expect(toObjectMaster.modules).toStrictEqual([ module1ToObject ]);
582
+ });
583
+ });
584
+ });
@@ -0,0 +1,80 @@
1
+ import {DTAction, DTModule} from "../../src";
2
+ import {HaileiToObjectTest} from "./DTElement.double";
3
+ import {bunch1IdTest} from "./DTBunch.double";
4
+ import {DTModuleToObject} from "../../src/types";
5
+
6
+ /** ****************** STUB PROPERTIES CONSTANTS
7
+ * Fixed properties to use with double classes, avoid auto generated and easy checking on test
8
+ * **** */
9
+ export const IDTest = 'DTModule-id-1234567';
10
+ export const KeyTest = 'DTModule-key-1234567';
11
+
12
+ // Specific Module constants
13
+ export const module1IDTest = `${IDTest}_1`;
14
+ export const module1KeyTest = `${KeyTest}_1`;
15
+
16
+ export const module1ToObject: DTModuleToObject = {
17
+ id: module1IDTest,
18
+ key: module1KeyTest,
19
+ type: 'module',
20
+ enabled: true
21
+ };
22
+
23
+ /** ****************** HELPER TEST CLASS
24
+ * Helper test class, inherits the main component
25
+ * Providing methods to property access and other facilities, in order to avoid using class methods
26
+ * **** */
27
+ export class DTModuleTest extends DTModule {
28
+ th_get_id(): string {
29
+ return this._id;
30
+ }
31
+
32
+ th_set_id(id: string): void {
33
+ this._id = id;
34
+ }
35
+
36
+ th_get_key(): string {
37
+ return this._key;
38
+ }
39
+
40
+ th_set_key(key: string): void {
41
+ this._key = key;
42
+ }
43
+
44
+ th_get_componentType(): string {
45
+ return this._componentType;
46
+ }
47
+
48
+ th_get_enabled(): boolean {
49
+ return this._enabled;
50
+ }
51
+
52
+ th_set_enabled(enabled: boolean): void {
53
+ this._enabled = enabled;
54
+ }
55
+ }
56
+
57
+
58
+ /** ****************** STUB CLASS
59
+ * Stub class, for using in other component
60
+ * **** */
61
+ export class DTModuleStub extends DTModuleTest {
62
+ constructor() {
63
+ super();
64
+ this._id = IDTest;
65
+ this._key = KeyTest;
66
+ this._errors = [];
67
+ }
68
+
69
+ getId(): string {
70
+ return IDTest;
71
+ }
72
+
73
+ getKey(): string {
74
+ return KeyTest;
75
+ }
76
+ }
77
+
78
+ /** ****************** HELPER METHODS
79
+ * Additional helper methods to use with testing
80
+ * **** */