dyo-tools 0.1.0-rc2 → 0.2.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 (93) hide show
  1. package/.c8rc.json +4 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +47 -0
  4. package/LICENSE +21 -0
  5. package/Makefile +34 -0
  6. package/README.md +0 -7
  7. package/babel.config.js +1 -0
  8. package/cucumber-report.html +48 -0
  9. package/cucumber.js +9 -0
  10. package/dist/constants.d.ts +6 -0
  11. package/dist/constants.js +63 -0
  12. package/dist/constants.js.map +1 -0
  13. package/dist/core/DTBunch.d.ts +11 -15
  14. package/dist/core/DTBunch.js +27 -106
  15. package/dist/core/DTBunch.js.map +1 -1
  16. package/dist/core/DTComponent.d.ts +13 -5
  17. package/dist/core/DTComponent.js +39 -1
  18. package/dist/core/DTComponent.js.map +1 -1
  19. package/dist/core/DTComponentPhysical.d.ts +10 -0
  20. package/dist/core/DTComponentPhysical.js +16 -0
  21. package/dist/core/DTComponentPhysical.js.map +1 -0
  22. package/dist/core/DTComponentWithMeta.d.ts +2 -2
  23. package/dist/core/DTComponentWithMeta.js.map +1 -1
  24. package/dist/core/DTElement.d.ts +2 -7
  25. package/dist/core/DTElement.js +3 -12
  26. package/dist/core/DTElement.js.map +1 -1
  27. package/dist/core/DTManager.d.ts +31 -0
  28. package/dist/core/DTManager.js +180 -0
  29. package/dist/core/DTManager.js.map +1 -0
  30. package/dist/core/DTPlayer.js +1 -1
  31. package/dist/core/DTPlayer.js.map +1 -1
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.js +5 -1
  34. package/dist/index.js.map +1 -1
  35. package/dist/libs/DYOFinder.d.ts +10 -0
  36. package/dist/libs/DYOFinder.js +96 -0
  37. package/dist/libs/DYOFinder.js.map +1 -0
  38. package/dist/tsconfig.tsbuildinfo +1 -1321
  39. package/dist/types/index.d.ts +64 -24
  40. package/dist/types/index.js.map +1 -1
  41. package/docs/.nojekyll +1 -0
  42. package/docs/assets/highlight.css +29 -0
  43. package/docs/assets/main.js +58 -0
  44. package/docs/assets/search.js +1 -0
  45. package/docs/assets/style.css +1367 -0
  46. package/docs/index.html +46 -0
  47. package/e2e/0.2.0/epic1.feature +29 -0
  48. package/e2e/0.2.0/epic2.feature +22 -0
  49. package/e2e/0.2.0/epic3.feature +25 -0
  50. package/e2e/0.2.0/resources/dominion.js +195 -0
  51. package/e2e/0.2.0/resources/utils.js +27 -0
  52. package/e2e/0.2.0/support/steps.js +108 -0
  53. package/e2e/future/epic4.feature +39 -0
  54. package/e2e/future/resources/dominion.js +238 -0
  55. package/e2e/future/resources/utils.js +27 -0
  56. package/jest.config.js +6 -0
  57. package/package.json +33 -23
  58. package/src/constants.ts +85 -0
  59. package/src/core/DTBunch.ts +461 -0
  60. package/src/core/DTComponent.ts +225 -0
  61. package/src/core/DTComponentPhysical.ts +39 -0
  62. package/src/core/DTComponentWithMeta.ts +65 -0
  63. package/src/core/DTElement.ts +69 -0
  64. package/src/core/DTError.ts +78 -0
  65. package/src/core/DTManager.ts +446 -0
  66. package/src/core/DTPlayer.ts +57 -0
  67. package/src/index.ts +9 -0
  68. package/src/libs/DYOFinder.ts +175 -0
  69. package/src/types/index.ts +162 -0
  70. package/test/core/DTBunch.double.ts +253 -0
  71. package/test/core/DTBunch.spec.ts +895 -0
  72. package/test/core/DTComponent.double.ts +164 -0
  73. package/test/core/DTComponent.spec.ts +295 -0
  74. package/test/core/DTComponentPhysical.double.ts +76 -0
  75. package/test/core/DTComponentPhysical.spec.ts +64 -0
  76. package/test/core/DTComponentWithMeta.double.ts +115 -0
  77. package/test/core/DTComponentWithMeta.spec.ts +124 -0
  78. package/test/core/DTElement.double.ts +147 -0
  79. package/test/core/DTElement.spec.ts +102 -0
  80. package/test/core/DTError.double.ts +92 -0
  81. package/test/core/DTError.spec.ts +89 -0
  82. package/test/core/DTManager.double.ts +192 -0
  83. package/test/core/DTManager.spec.ts +902 -0
  84. package/test/core/DTPlayer.double.ts +64 -0
  85. package/test/core/DTPlayer.spec.ts +80 -0
  86. package/test/core/copy.spec.ts +227 -0
  87. package/test/libs/DYOFinder.double.ts +152 -0
  88. package/test/libs/DYOFinder.spec.ts +194 -0
  89. package/tsconfig.dev.json +22 -0
  90. package/tsconfig.json +21 -0
  91. package/dist/utils/filters.d.ts +0 -6
  92. package/dist/utils/filters.js +0 -39
  93. package/dist/utils/filters.js.map +0 -1
@@ -0,0 +1,57 @@
1
+ import DYOToolsComponentWithMeta from './DTComponentWithMeta';
2
+ import { DTAcceptedMetaData, DTPlayerToObject } from '../types';
3
+
4
+ export default class DYOToolsPlayer<IComponentMeta extends DTAcceptedMetaData> extends DYOToolsComponentWithMeta<IComponentMeta> {
5
+ /**
6
+ * Defining component type to "player".
7
+ */
8
+ protected _componentType = 'player';
9
+
10
+ /**
11
+ * Create and return a new DTPlayer instance by applying from current instance :
12
+ * - Copy _key property
13
+ * - Copy _meta property
14
+ *
15
+ * @returns New DTPlayer instance copied.
16
+ */
17
+ copy(): DYOToolsPlayer<IComponentMeta> {
18
+ const copyElement = new DYOToolsPlayer<IComponentMeta>(this._key, this._options);
19
+ copyElement.setManyMeta({ ...this.getManyMeta() });
20
+
21
+ return copyElement;
22
+ }
23
+
24
+ /**
25
+ * Return JSON Object representation of the Player instance.
26
+ *
27
+ * JSON Object returned has the following structure :
28
+ * * **id** : _id property of the Player.
29
+ * * **key** : _key property of the Player.
30
+ * * **type** : _componentType property of the Player.
31
+ * * **meta** : JSON Object of all current metadata in _meta property of the Player (only if not empty).
32
+ *
33
+ * @returns JSON Object representation of the Player.
34
+ */
35
+ toObject(): DTPlayerToObject<IComponentMeta> {
36
+ const objectPlayer: DTPlayerToObject<IComponentMeta> = {
37
+ id: this._id,
38
+ key: this._key,
39
+ type: this._componentType,
40
+ };
41
+
42
+ if (this._meta && Object.keys(this._meta).length > 0) {
43
+ objectPlayer.meta = { ...this.getManyMeta() };
44
+ }
45
+
46
+ return objectPlayer;
47
+ }
48
+
49
+ /**
50
+ * Return String representation of the Player instance.
51
+ *
52
+ * @returns String representation of the Player.
53
+ */
54
+ toString(): string {
55
+ return `Component ${this._key} - Type: Player`;
56
+ }
57
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ // All exports
2
+ export { default as DTComponent } from './core/DTComponent';
3
+ export { default as DTComponentWithMeta } from './core/DTComponentWithMeta';
4
+ export { default as DTComponentPhysical } from './core/DTComponentPhysical';
5
+ export { default as DTElement } from './core/DTElement';
6
+ export { default as DTBunch } from './core/DTBunch';
7
+ export { default as DTManager } from './core/DTManager';
8
+ export { default as DTPlayer } from './core/DTPlayer';
9
+ export { default as DTError } from './core/DTError';
@@ -0,0 +1,175 @@
1
+ import {
2
+ DYOFinderComponentType,
3
+ DYOFinderConfiguration,
4
+ DYOFinderFilterOperator,
5
+ DYOFinderFilters,
6
+ FilterOperatorType,
7
+ StandardPrimitiveType,
8
+ StandardPrimitiveTypeWithArray,
9
+ } from '../types';
10
+
11
+ export default class DYOFinder {
12
+ /**
13
+ * Current DTComponent associated to the Finder.
14
+ */
15
+ protected _component: DYOFinderComponentType;
16
+
17
+ /**
18
+ * Current DYOFinder configuration applied.
19
+ */
20
+ protected _configuration: DYOFinderConfiguration;
21
+
22
+ /**
23
+ * Set _component and _configuration properties.
24
+ *
25
+ * @param component
26
+ * @param configuration
27
+ */
28
+ constructor(component: DYOFinderComponentType, configuration: DYOFinderConfiguration) {
29
+ this._component = component;
30
+ this._configuration = configuration;
31
+ }
32
+
33
+ /**
34
+ * Getter for _component property.
35
+ */
36
+ getComponent(): DYOFinderComponentType {
37
+ return this._component;
38
+ }
39
+
40
+ /**
41
+ * Return an array of DTComponent from *_component* **_items** property filtered with a **filters** argument.
42
+ *
43
+ * Search filters can be applied on properties depending on the current _configuration provided.
44
+ *
45
+ * For each search filter provided, an object of specific operators is applied :
46
+ * * **BASIC OPERATORS**
47
+ * * **$eq** : The property must be strict equal to the filter value.
48
+ * * **$in** : The property must be included into the filter array.
49
+ * * **$nin** : The property must not be included into the filter array.
50
+ * * **$ne** : The property must be different to the filter value.
51
+ * * **EXTENDED OPERATORS** (meta only)
52
+ * * **$lte** : Number property only. The property must be lower or equal than the filter value.
53
+ * * **$gte** : Number property only. The property must be higher or equal than the filter array.
54
+ * * **$contains** : Array property only. The property must contain the filter value.
55
+ * * **$ncontains** : Array property only. The property must not contain the filter value.
56
+ *
57
+ * If many operators and / or many properties are passed into the **filters** argument, the logic operator applied is
58
+ * **AND**.
59
+ *
60
+ * @param filters Filters Object. The format is :
61
+ * { [property_1] : { [operator_1] : filter_value, [operator_2] : filter_value_2, ... }, [property_2] : { ... }, ... }
62
+ *
63
+ * For **objectSearch** properties, you have to pass the object key before the operator :
64
+ * { [property_1]: { [object_key1] : { [operator_1] : filter_value_1, ... }, [object_key2] : { ... }, ... }, ... }
65
+ * @returns Array of DTComponent instance corresponding to the filters. Empty if no filter or invalid ones are passed.
66
+ */
67
+ execute<ITEM>(filters: DYOFinderFilters): ITEM[] {
68
+ const items = this._component.getAll();
69
+ const filteredItems = [];
70
+
71
+ for (const item of items) {
72
+ let validItem = !!(Object.keys(filters).length);
73
+
74
+ for (const [propKey, configProp] of Object.entries(this._configuration)) {
75
+ if (filters[propKey]) {
76
+ if (configProp.objectSearch) {
77
+ const objectValue = configProp.getValue(item, this.getComponent());
78
+ if (objectValue) {
79
+ for (const [filterK, filterV] of Object.entries(filters[propKey])) {
80
+ const metaValue = Object.prototype.hasOwnProperty.call(objectValue, filterK) ? objectValue[filterK] : undefined;
81
+ if (!this.checkAllValidFiltersForProp(metaValue, filterV, configProp.operators)) {
82
+ validItem = false;
83
+ break;
84
+ }
85
+ }
86
+ } else {
87
+ validItem = false;
88
+ }
89
+ } else if (!this.checkAllValidFiltersForProp(
90
+ (configProp.getValue(item, this.getComponent()) as StandardPrimitiveType),
91
+ filters[propKey],
92
+ configProp.operators,
93
+ )) {
94
+ validItem = false;
95
+ }
96
+ }
97
+ }
98
+
99
+ if (validItem) {
100
+ filteredItems.push(item);
101
+ }
102
+ }
103
+
104
+ return filteredItems;
105
+ }
106
+
107
+ private checkAllValidFiltersForProp = (
108
+ itemProp: StandardPrimitiveTypeWithArray,
109
+ operators: Partial<DYOFinderFilterOperator>,
110
+ validOperators: FilterOperatorType[],
111
+ ) => {
112
+ if (Object.keys(operators).length) {
113
+ for (const operator of Object.keys(operators)) {
114
+ if (!validOperators.includes(operator as FilterOperatorType)
115
+ || !this.validFiltersForItem(itemProp, operators[operator], operator as FilterOperatorType)) {
116
+ return false;
117
+ }
118
+ }
119
+ return true;
120
+ }
121
+ return false;
122
+ };
123
+
124
+ private validFiltersForItem(
125
+ itemProp: StandardPrimitiveTypeWithArray,
126
+ filter: StandardPrimitiveTypeWithArray,
127
+ operator: FilterOperatorType,
128
+ ): boolean {
129
+ // $eq Filter
130
+ if (operator === FilterOperatorType.EQ) {
131
+ return itemProp === filter;
132
+ }
133
+ // $in Filter
134
+ if (operator === FilterOperatorType.IN) {
135
+ return filter ? (filter as Array<StandardPrimitiveType>).includes(itemProp as StandardPrimitiveType) : false;
136
+ }
137
+ // $nin Filter
138
+ if (operator === FilterOperatorType.NIN) {
139
+ return filter ? !(filter as Array<StandardPrimitiveType>).includes(itemProp as StandardPrimitiveType) : false;
140
+ }
141
+ // $ne Filter
142
+ /* c8 ignore next */
143
+ if (operator === FilterOperatorType.NE) {
144
+ return itemProp !== filter;
145
+ }
146
+ // $lte Filter
147
+ if (operator === FilterOperatorType.LTE) {
148
+ if (typeof itemProp === 'number' && typeof filter === 'number') {
149
+ return itemProp <= filter;
150
+ }
151
+ return false;
152
+ }
153
+ // $gte Filter
154
+ if (operator === FilterOperatorType.GTE) {
155
+ if (typeof itemProp === 'number' && typeof filter === 'number') {
156
+ return itemProp >= filter;
157
+ }
158
+ return false;
159
+ }
160
+ // $contains Filter
161
+ if (operator === FilterOperatorType.CONTAINS) {
162
+ return (itemProp && Array.isArray(itemProp))
163
+ ? (itemProp as Array<StandardPrimitiveType>).includes(filter as StandardPrimitiveType)
164
+ : false;
165
+ }
166
+ // $ncontains Filter
167
+ if (operator === FilterOperatorType.NCONTAINS) {
168
+ return (itemProp && Array.isArray(itemProp))
169
+ ? !(itemProp as Array<StandardPrimitiveType>).includes(filter as StandardPrimitiveType)
170
+ : false;
171
+ }
172
+
173
+ return false;
174
+ }
175
+ }
@@ -0,0 +1,162 @@
1
+ import { DTBunch, DTComponent } from '../index';
2
+ import DYOToolsElement from '../core/DTElement';
3
+
4
+ /** Constants Enum * */
5
+ export enum FilterOperatorType {
6
+ EQ = '$eq',
7
+ IN = '$in',
8
+ NIN = '$nin',
9
+ NE = '$ne',
10
+ LTE = '$lte',
11
+ GTE = '$gte',
12
+ CONTAINS = '$contains',
13
+ NCONTAINS = '$ncontains',
14
+ }
15
+
16
+ /** Common Types * */
17
+ export type StandardPrimitiveType = string | number | boolean | null | undefined;
18
+ export type StandardPrimitiveTypeWithArray = string | number | boolean | Array<string | number | boolean> | null | undefined;
19
+
20
+ /** DYO Finder interfaces * */
21
+ export type DYOFinderConfiguration = Record<string, DYOFinderConfigurationProp>;
22
+ export type DYOFinderComponentType = DTComponent & {
23
+ getAll: () => DTComponent[],
24
+ };
25
+
26
+ export interface DYOFinderConfigurationPropDefault {
27
+ operators: FilterOperatorType[],
28
+ getValue: (item: DTComponent, ctx?: DTComponent) => StandardPrimitiveType,
29
+ objectSearch: false,
30
+ }
31
+
32
+ export interface DYOFinderConfigurationPropObjectSearch {
33
+ operators: FilterOperatorType[],
34
+ getValue: (item: DTComponent, ctx?: DTComponent) => Record<string, StandardPrimitiveTypeWithArray>,
35
+ objectSearch: true,
36
+ }
37
+ export type DYOFinderConfigurationProp = DYOFinderConfigurationPropDefault | DYOFinderConfigurationPropObjectSearch;
38
+
39
+ export interface DYOFinderFilterOperatorBase {
40
+ [FilterOperatorType.EQ]: StandardPrimitiveType
41
+ [FilterOperatorType.IN]: Array<StandardPrimitiveType>
42
+ [FilterOperatorType.NIN]: Array<StandardPrimitiveType>
43
+ [FilterOperatorType.NE]: StandardPrimitiveType
44
+ }
45
+ export interface DYOFinderFilterOperatorAdvanced {
46
+ [FilterOperatorType.LTE]: number
47
+ [FilterOperatorType.GTE]: number
48
+ [FilterOperatorType.CONTAINS]: StandardPrimitiveType
49
+ [FilterOperatorType.NCONTAINS]: StandardPrimitiveType
50
+ }
51
+
52
+ export type DYOFinderFilterOperator = DYOFinderFilterOperatorBase & DYOFinderFilterOperatorAdvanced;
53
+ export type DYOFinderFilterOperatorArgument = Partial<DYOFinderFilterOperator | Record<string, Partial<DYOFinderFilterOperator>>>;
54
+ export type DYOFinderFilters = Record<string, DYOFinderFilterOperatorArgument>;
55
+
56
+ /** DTComponent interfaces * */
57
+ /**
58
+ * DTComponent default options configuration.
59
+ */
60
+ export interface DTComponentOptions {
61
+ /**
62
+ * Default *false*. If *true*, no exception is thrown when an error occurred, a new DTError instance is
63
+ * added to the _errors property array instead. If *false*, throw the exception with a DTError instance.
64
+ */
65
+ errors: boolean
66
+ }
67
+
68
+ export interface DTComponentToObject {
69
+ id: string
70
+ key: string
71
+ type: string
72
+ }
73
+
74
+ /** DTComponentWithMeta interfaces * */
75
+ export type DTAcceptedMetaData = Record<
76
+ string,
77
+ StandardPrimitiveTypeWithArray
78
+ >;
79
+
80
+ /** DTElement interfaces * */
81
+ export interface DTElementToObject<IComponentMeta> extends DTComponentToObject {
82
+ owner?: string
83
+ meta?: Partial<IComponentMeta>
84
+ }
85
+
86
+ /** DTBunch interfaces * */
87
+ /**
88
+ * DTBunch option configuration.
89
+ */
90
+ export interface DTBunchOptions extends DTComponentOptions {
91
+ /**
92
+ * Default *false*. If *true*, an error occurred when adding a new DTElement with the same key of an
93
+ * existing element into the bunch.
94
+ */
95
+ uniqueKey: boolean
96
+ /**
97
+ * Default *false*. If *true*, when a new DTElement is added, the owner of this element becomes
98
+ * automatically the current bunch owner.
99
+ */
100
+ replaceIndex: boolean
101
+ /**
102
+ * Default *false*. If *true*, the context is not changed when a new DTElement is added.
103
+ * If *false*, when a new DTElement is added, the context of this element becomes automatically the current bunch instance
104
+ * and the element is removed from the old context Component (if defined).
105
+ */
106
+ inheritOwner: boolean
107
+ /**
108
+ * Default *false*. If *true*, when a new DTElement is added at existing index (using **addAtIndex**
109
+ * or **addManyAtIndex** method), this component replaces the old one. If *false*, this component is added at the specified
110
+ * index and other existing component are reindexed with the following index.
111
+ */
112
+ virtualContext: boolean
113
+ }
114
+
115
+ export interface DTBunchToObject<IComponentMeta> extends DTComponentToObject {
116
+ items: Array<DTElementToObject<DTAcceptedMetaData>>
117
+ owner?: string
118
+ meta?: Partial<IComponentMeta>
119
+ }
120
+
121
+ export interface DTBunchFilters {
122
+ id: Partial<DYOFinderFilterOperatorBase>
123
+ key: Partial<DYOFinderFilterOperatorBase>
124
+ context: Partial<DYOFinderFilterOperatorBase>
125
+ owner: Partial<DYOFinderFilterOperatorBase>
126
+ meta: Record<string, Partial<DYOFinderFilterOperatorAdvanced>>
127
+ }
128
+
129
+ /** DTPlayer interfaces * */
130
+ export interface DTPlayerToObject<IComponentMeta> extends DTComponentToObject {
131
+ meta?: Partial<IComponentMeta>
132
+ }
133
+
134
+ /** DTManager interfaces * */
135
+ export type DTManagerItemsType<IBunchItem extends DYOToolsElement<DTAcceptedMetaData>> = Record<string, DTManagerItemType<IBunchItem>>;
136
+ export type DTManagerItemType<IBunchItem extends DYOToolsElement<DTAcceptedMetaData>> = {
137
+ scope: string,
138
+ item: DTBunch<IBunchItem>,
139
+ };
140
+
141
+ export interface DTManagerFilters extends DYOFinderFilters {
142
+ id: Partial<DYOFinderFilterOperatorBase>
143
+ key: Partial<DYOFinderFilterOperatorBase>
144
+ owner: Partial<DYOFinderFilterOperatorBase>
145
+ scope: Partial<DYOFinderFilterOperatorBase>
146
+ meta: Record<string, Partial<DYOFinderFilterOperatorAdvanced>>
147
+ }
148
+
149
+ /**
150
+ * DTManager option configuration.
151
+ */
152
+ export interface DTManagerOptions extends DTComponentOptions {
153
+ /**
154
+ * Default *false*. If *true*, when a bunch instance is removed from the Manager _items, the process performs also
155
+ * a removal from the Manager Library of all DTElement instances of the bunch.
156
+ */
157
+ libraryDeletion: boolean,
158
+ }
159
+
160
+ export interface DTManagerToObject extends DTComponentToObject {
161
+ items: Array<DTBunchToObject<DTAcceptedMetaData> & { scope: string }>
162
+ }
@@ -0,0 +1,253 @@
1
+ import { DTBunch, DTComponent } from '../../src';
2
+ import { bunchDefaultOptions as DTBunchDefaultOptions } from '../../src/constants';
3
+ import {
4
+ HaileiMetaData,
5
+ IldressMetaData,
6
+ IMetaDataTest,
7
+ MaydenaMetaData,
8
+ MeldrineMetaData,
9
+ YssaliaMetaData,
10
+ } from './DTComponentWithMeta.double';
11
+ import DYOToolsElement from '../../src/core/DTElement';
12
+ import { DTBunchOptions, DTElementToObject } from '../../src/types';
13
+ import Mocked = jest.Mocked;
14
+ import {
15
+ HaileiIdTest,
16
+ HaileiKeyTest,
17
+ HaileiToObjectTest,
18
+ IldressIdTest,
19
+ IldressKeyTest,
20
+ IldressToObjectTest,
21
+ MaydenaIdTest,
22
+ MaydenaKeyTest,
23
+ MaydenaToObjectTest,
24
+ MeldrineIdTest,
25
+ MeldrineKeyTest,
26
+ MeldrineToObjectTest, YssaliaIdTest, YssaliaKeyTest, YssaliaToObjectTest,
27
+ } from './DTElement.double';
28
+ import { DTErrorStub } from './DTError.double';
29
+ import { DTPlayerStub } from './DTPlayer.double';
30
+ import DYOFinder from '../../src/libs/DYOFinder';
31
+
32
+ /** ****************** STUB PROPERTIES CONSTANTS
33
+ * Fixed properties to use with double classes, avoid auto generated and easy checking on test
34
+ * **** */
35
+ // Global Bunch constants
36
+ export const IDTest = 'DTBunch-id-1234567';
37
+ export const IDTestLibrary = 'DTBunch-id-library-1234567';
38
+ export const KeyTest = 'DTBunch-key-1234567';
39
+ export const defaultOptions: DTBunchOptions = DTBunchDefaultOptions;
40
+
41
+ // Specific Bunch constants
42
+ export const bunch1IdTest = `${IDTest}_1`;
43
+ export const bunch1toObjectTest = {
44
+ id: bunch1IdTest, key: 'bunch1-key-test', type: 'bunch', items: [HaileiToObjectTest],
45
+ };
46
+ export const bunch2IdTest = `${IDTest}_2`;
47
+ export const bunch2toObjectTest = {
48
+ id: bunch2IdTest, key: 'bunch2-key-test', type: 'bunch', items: [],
49
+ };
50
+ export const bunch3IdTest = `${IDTest}_3`;
51
+ export const bunch3toObjectTest = {
52
+ id: bunch3IdTest, key: 'bunch3-key-test', type: 'bunch', items: [],
53
+ };
54
+
55
+ /** ****************** HELPER TEST CLASS
56
+ * Helper test class, inherits the main component
57
+ * Providing methods to property access and other facilities, in order to avoid using class methods
58
+ * **** */
59
+ export class DTBunchTest extends DTBunch<Mocked<DYOToolsElement<IMetaDataTest>>, IMetaDataTest> {
60
+ th_get_id(): string {
61
+ return this._id;
62
+ }
63
+
64
+ th_set_id(id: string): void {
65
+ this._id = id;
66
+ }
67
+
68
+ th_get_key(): string {
69
+ return this._key;
70
+ }
71
+
72
+ th_set_key(key: string): void {
73
+ this._key = key;
74
+ }
75
+
76
+ th_get_componentType(): string {
77
+ return this._componentType;
78
+ }
79
+
80
+ th_get_context(): DTComponent | undefined {
81
+ return this._context;
82
+ }
83
+
84
+ th_set_context(context: DTComponent): void {
85
+ this._context = context;
86
+ }
87
+
88
+ th_get_items(): Mocked<DYOToolsElement<IMetaDataTest>>[] {
89
+ return this._items;
90
+ }
91
+
92
+ th_set_items(items: Mocked<DYOToolsElement<IMetaDataTest>>[]): void {
93
+ this._items = items;
94
+ }
95
+
96
+ th_set_errors(errors: Array<DTErrorStub>): void {
97
+ this._errors = errors;
98
+ }
99
+
100
+ th_get_options(): any {
101
+ return this._options;
102
+ }
103
+
104
+ th_set_options(options: any): void {
105
+ this._options = {
106
+ ...this._options,
107
+ ...options,
108
+ };
109
+ }
110
+
111
+ th_get_owner(): DTPlayerStub {
112
+ return this._owner;
113
+ }
114
+
115
+ th_set_owner(owner: DTPlayerStub): void {
116
+ this._owner = owner;
117
+ }
118
+
119
+ th_set_meta(meta: IMetaDataTest): void {
120
+ this._meta = meta;
121
+ }
122
+
123
+ th_get_finder(): DYOFinder {
124
+ return this._finder;
125
+ }
126
+ }
127
+
128
+ /** ****************** STUB CLASS
129
+ * Stub class, for using in other component
130
+ * **** */
131
+ export class DTBunchStub extends DTBunchTest {
132
+ constructor(items: Array<Mocked<DYOToolsElement<IMetaDataTest>>> = []) {
133
+ super();
134
+ this._id = IDTest;
135
+ this._key = KeyTest;
136
+ this._errors = [];
137
+ this._items = items;
138
+ this._options = DTBunchDefaultOptions;
139
+ }
140
+
141
+ getId(): string {
142
+ return IDTest;
143
+ }
144
+
145
+ getKey(): string {
146
+ return KeyTest;
147
+ }
148
+
149
+ getAll(): Array<Mocked<DYOToolsElement<IMetaDataTest>>> {
150
+ return this._items;
151
+ }
152
+
153
+ getAllKeys(): string[] {
154
+ return this._items.map((item: DYOToolsElement<IMetaDataTest>) => item.getKey());
155
+ }
156
+ }
157
+
158
+ // Specific Stub Library for Manager
159
+ export class DTBunchStubLibrary extends DTBunchStub {
160
+ constructor(items: Array<Mocked<DYOToolsElement<IMetaDataTest>>> = []) {
161
+ super(items);
162
+ this._id = IDTestLibrary;
163
+ this._key = 'library';
164
+ this._errors = [];
165
+ this._items = items;
166
+ this._options = {
167
+ ...DTBunchDefaultOptions,
168
+ virtualContext: true,
169
+ };
170
+ }
171
+
172
+ getId(): string {
173
+ return IDTestLibrary;
174
+ }
175
+
176
+ getKey(): string {
177
+ return 'library';
178
+ }
179
+
180
+ get(id: string): Mocked<DYOToolsElement<IMetaDataTest>> | undefined {
181
+ const itemFiltered = this._items.filter((item: Mocked<DYOToolsElement<IMetaDataTest>>) => item.getId() === id);
182
+ return itemFiltered.length > 0 ? itemFiltered[0] : undefined;
183
+ }
184
+ }
185
+
186
+ /** ****************** HELPER METHODS
187
+ * Additional helper methods to use with testing
188
+ * **** */
189
+
190
+ // Function to generate Mocked DTElement collection
191
+ // Warning : DYOToolsElement class must be MOCKED to use this !
192
+ export const generateMockedElements = (numberElements: number): Array<Mocked<DYOToolsElement<IMetaDataTest>>> => {
193
+ const mockedElements = [];
194
+ const mockedData: Array<{ id: string, key: string, meta: IMetaDataTest, toObject: DTElementToObject<IMetaDataTest> }> = [
195
+ {
196
+ id: HaileiIdTest,
197
+ key: HaileiKeyTest,
198
+ meta: HaileiMetaData,
199
+ toObject: HaileiToObjectTest,
200
+ },
201
+ {
202
+ id: MeldrineIdTest,
203
+ key: MeldrineKeyTest,
204
+ meta: MeldrineMetaData,
205
+ toObject: MeldrineToObjectTest,
206
+ },
207
+ {
208
+ id: MaydenaIdTest,
209
+ key: MaydenaKeyTest,
210
+ meta: MaydenaMetaData,
211
+ toObject: MaydenaToObjectTest,
212
+ },
213
+ {
214
+ id: IldressIdTest,
215
+ key: IldressKeyTest,
216
+ meta: IldressMetaData,
217
+ toObject: IldressToObjectTest,
218
+ },
219
+ {
220
+ id: YssaliaIdTest,
221
+ key: YssaliaKeyTest,
222
+ meta: YssaliaMetaData,
223
+ toObject: YssaliaToObjectTest,
224
+ },
225
+ ];
226
+
227
+ for (let i = 0; i < numberElements; i++) {
228
+ const mockedElement = new DYOToolsElement() as Mocked<DYOToolsElement<IMetaDataTest>>;
229
+ const selectedMock = i % 5;
230
+
231
+ mockedElement.getId.mockReturnValue(`${mockedData[selectedMock].id}-${i}`);
232
+ mockedElement.getKey.mockReturnValue(mockedData[selectedMock].key);
233
+ mockedElement.getManyMeta.mockReturnValue(mockedData[selectedMock].meta);
234
+ mockedElement.setContext.mockImplementation(function (context) {
235
+ this._context = context;
236
+ });
237
+ mockedElement.getContext.mockImplementation(function () {
238
+ return this._context;
239
+ });
240
+ mockedElement.setOwner.mockImplementation(function (owner) {
241
+ this._owner = owner;
242
+ });
243
+ mockedElement.getOwner.mockImplementation(function () {
244
+ return this._owner;
245
+ });
246
+ mockedElement.copy.mockReturnValue(mockedElement);
247
+ mockedElement.toObject.mockReturnValue(mockedData[selectedMock].toObject);
248
+
249
+ mockedElements.push(mockedElement);
250
+ }
251
+
252
+ return mockedElements;
253
+ };