dyo-tools 0.1.0 → 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.
- package/.c8rc.json +4 -0
- package/.eslintignore +2 -0
- package/.eslintrc.json +47 -0
- package/LICENSE +21 -0
- package/Makefile +34 -0
- package/README.md +0 -7
- package/babel.config.js +1 -0
- package/cucumber-report.html +48 -0
- package/cucumber.js +9 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +63 -0
- package/dist/constants.js.map +1 -0
- package/dist/core/DTBunch.d.ts +11 -15
- package/dist/core/DTBunch.js +27 -106
- package/dist/core/DTBunch.js.map +1 -1
- package/dist/core/DTComponent.d.ts +13 -5
- package/dist/core/DTComponent.js +39 -1
- package/dist/core/DTComponent.js.map +1 -1
- package/dist/core/DTComponentPhysical.d.ts +10 -0
- package/dist/core/DTComponentPhysical.js +16 -0
- package/dist/core/DTComponentPhysical.js.map +1 -0
- package/dist/core/DTComponentWithMeta.d.ts +2 -2
- package/dist/core/DTComponentWithMeta.js.map +1 -1
- package/dist/core/DTElement.d.ts +2 -7
- package/dist/core/DTElement.js +3 -12
- package/dist/core/DTElement.js.map +1 -1
- package/dist/core/DTManager.d.ts +31 -0
- package/dist/core/DTManager.js +180 -0
- package/dist/core/DTManager.js.map +1 -0
- package/dist/core/DTPlayer.js +1 -1
- package/dist/core/DTPlayer.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/libs/DYOFinder.d.ts +10 -0
- package/dist/libs/DYOFinder.js +96 -0
- package/dist/libs/DYOFinder.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1321
- package/dist/types/index.d.ts +64 -24
- package/dist/types/index.js.map +1 -1
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +29 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/index.html +46 -0
- package/e2e/0.2.0/epic1.feature +29 -0
- package/e2e/0.2.0/epic2.feature +22 -0
- package/e2e/0.2.0/epic3.feature +25 -0
- package/e2e/0.2.0/resources/dominion.js +195 -0
- package/e2e/0.2.0/resources/utils.js +27 -0
- package/e2e/0.2.0/support/steps.js +108 -0
- package/e2e/future/epic4.feature +39 -0
- package/e2e/future/resources/dominion.js +238 -0
- package/e2e/future/resources/utils.js +27 -0
- package/jest.config.js +6 -0
- package/package.json +33 -23
- package/src/constants.ts +85 -0
- package/src/core/DTBunch.ts +461 -0
- package/src/core/DTComponent.ts +225 -0
- package/src/core/DTComponentPhysical.ts +39 -0
- package/src/core/DTComponentWithMeta.ts +65 -0
- package/src/core/DTElement.ts +69 -0
- package/src/core/DTError.ts +78 -0
- package/src/core/DTManager.ts +446 -0
- package/src/core/DTPlayer.ts +57 -0
- package/src/index.ts +9 -0
- package/src/libs/DYOFinder.ts +175 -0
- package/src/types/index.ts +162 -0
- package/test/core/DTBunch.double.ts +253 -0
- package/test/core/DTBunch.spec.ts +895 -0
- package/test/core/DTComponent.double.ts +164 -0
- package/test/core/DTComponent.spec.ts +295 -0
- package/test/core/DTComponentPhysical.double.ts +76 -0
- package/test/core/DTComponentPhysical.spec.ts +64 -0
- package/test/core/DTComponentWithMeta.double.ts +115 -0
- package/test/core/DTComponentWithMeta.spec.ts +124 -0
- package/test/core/DTElement.double.ts +147 -0
- package/test/core/DTElement.spec.ts +102 -0
- package/test/core/DTError.double.ts +92 -0
- package/test/core/DTError.spec.ts +89 -0
- package/test/core/DTManager.double.ts +192 -0
- package/test/core/DTManager.spec.ts +902 -0
- package/test/core/DTPlayer.double.ts +64 -0
- package/test/core/DTPlayer.spec.ts +80 -0
- package/test/core/copy.spec.ts +227 -0
- package/test/libs/DYOFinder.double.ts +152 -0
- package/test/libs/DYOFinder.spec.ts +194 -0
- package/tsconfig.dev.json +22 -0
- package/tsconfig.json +21 -0
- package/dist/utils/filters.d.ts +0 -6
- package/dist/utils/filters.js +0 -39
- package/dist/utils/filters.js.map +0 -1
|
@@ -0,0 +1,895 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, jest, test, } from '@jest/globals';
|
|
2
|
+
import { defaultOptions, DTBunchStub, DTBunchTest, generateMockedElements, IDTest, KeyTest, } from './DTBunch.double';
|
|
3
|
+
import {
|
|
4
|
+
DTElementStub,
|
|
5
|
+
HaileiIdTest,
|
|
6
|
+
HaileiKeyTest,
|
|
7
|
+
HaileiToObjectTest,
|
|
8
|
+
IDTest as IDElementTest,
|
|
9
|
+
IldressIdTest,
|
|
10
|
+
IldressKeyTest,
|
|
11
|
+
IldressToObjectTest,
|
|
12
|
+
KeyTest as KeyElementTest,
|
|
13
|
+
MaydenaIdTest,
|
|
14
|
+
MaydenaKeyTest,
|
|
15
|
+
MaydenaToObjectTest,
|
|
16
|
+
MeldrineIdTest,
|
|
17
|
+
MeldrineKeyTest,
|
|
18
|
+
MeldrineToObjectTest,
|
|
19
|
+
YssaliaIdTest,
|
|
20
|
+
YssaliaKeyTest,
|
|
21
|
+
YssaliaToObjectTest,
|
|
22
|
+
} from './DTElement.double';
|
|
23
|
+
import { DTBunch, DTComponentPhysical } from '../../src';
|
|
24
|
+
import DYOToolsElement from '../../src/core/DTElement';
|
|
25
|
+
import { BunchMetaData, HaileiMetaData, IMetaDataTest } from './DTComponentWithMeta.double';
|
|
26
|
+
import { DTBunchOptions, FilterOperatorType } from '../../src/types';
|
|
27
|
+
import {
|
|
28
|
+
DTPlayerStub,
|
|
29
|
+
IDTest as IDTestPlayer,
|
|
30
|
+
IDTest as IDPlayerTest,
|
|
31
|
+
KeyTest as KeyPlayerTest,
|
|
32
|
+
toStringTest as toStringPlayerTest,
|
|
33
|
+
} from './DTPlayer.double';
|
|
34
|
+
import { checkCallForMockedDTError, CodeTest as DTErrorCodeTest, DTErrorStub } from './DTError.double';
|
|
35
|
+
import DYOToolsError from '../../src/core/DTError';
|
|
36
|
+
import { mockOverriddenMethods } from './DTComponentPhysical.double';
|
|
37
|
+
import { DTManagerStub } from './DTManager.double';
|
|
38
|
+
import { componentBunchDefaultFinderConfiguration } from '../../src/constants';
|
|
39
|
+
import Mocked = jest.Mocked;
|
|
40
|
+
import MockedFunction = jest.MockedFunction;
|
|
41
|
+
|
|
42
|
+
/** ****************** MOCK DEPENDENCIES
|
|
43
|
+
* All Dependencies used by the component are mocked with Jest
|
|
44
|
+
* **** */
|
|
45
|
+
jest.mock('../../src/core/DTElement');
|
|
46
|
+
jest.mock('../../src/core/DTManager');
|
|
47
|
+
jest.mock('../../src/core/DTError');
|
|
48
|
+
jest.mock('../../src/core/DTComponent');
|
|
49
|
+
jest.mock('../../src/core/DTComponentWithMeta');
|
|
50
|
+
jest.mock('../../src/core/DTComponentPhysical');
|
|
51
|
+
jest.mock('../../src/libs/DYOFinder');
|
|
52
|
+
// Add specific mock for inherited methods to have a basic implementation
|
|
53
|
+
mockOverriddenMethods(DTComponentPhysical);
|
|
54
|
+
|
|
55
|
+
/** *********************** TESTS SUITES ****************************** */
|
|
56
|
+
describe('class DYOToolsBunch', () => {
|
|
57
|
+
let bunchTest: DTBunchTest;
|
|
58
|
+
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
bunchTest = new DTBunchTest();
|
|
61
|
+
bunchTest.th_set_options(defaultOptions);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterEach(() => {
|
|
65
|
+
jest.resetAllMocks();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('inheritance', () => {
|
|
69
|
+
test('check good inheritance for class', () => {
|
|
70
|
+
expect(DTBunch.prototype instanceof DTComponentPhysical).toBeTruthy();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('constructor()', () => {
|
|
75
|
+
let addManyImpl;
|
|
76
|
+
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
jest.resetAllMocks();
|
|
79
|
+
addManyImpl = DTBunch.prototype.addMany;
|
|
80
|
+
DTBunch.prototype.addMany = jest.fn();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
afterEach(() => {
|
|
84
|
+
DTBunch.prototype.addMany = addManyImpl;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('creation simple with key', () => {
|
|
88
|
+
const newBunch = new DTBunchTest(KeyTest);
|
|
89
|
+
const parentConstructorMock = (DTComponentPhysical.prototype.constructor as MockedFunction<(key: string, options: any) => void>).mock;
|
|
90
|
+
|
|
91
|
+
expect(parentConstructorMock.calls[0][0]).toBe(KeyTest);
|
|
92
|
+
expect(parentConstructorMock.calls[0][1]).toStrictEqual(defaultOptions);
|
|
93
|
+
expect(newBunch.th_get_items()).toStrictEqual([]);
|
|
94
|
+
|
|
95
|
+
// Finder initialization
|
|
96
|
+
expect((newBunch.th_get_finder() as any).constructor.mock.calls.length).toBe(1);
|
|
97
|
+
expect((newBunch.th_get_finder() as any).constructor.mock.calls[0][0]).toStrictEqual(newBunch);
|
|
98
|
+
expect((newBunch.th_get_finder() as any).constructor.mock.calls[0][1]).toStrictEqual(componentBunchDefaultFinderConfiguration);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('creation with items', () => {
|
|
102
|
+
new DTBunchTest(KeyTest, generateMockedElements(3));
|
|
103
|
+
const mockedAddMany = DTBunch.prototype.addMany as MockedFunction<(items: Array<Mocked<DYOToolsElement<IMetaDataTest>>>) => void>;
|
|
104
|
+
|
|
105
|
+
expect(mockedAddMany.mock.calls.length).toBe(1);
|
|
106
|
+
expect(mockedAddMany.mock.calls[0][0].length).toBe(3);
|
|
107
|
+
expect(mockedAddMany.mock.calls[0][0][0].getKey()).toBe(HaileiKeyTest);
|
|
108
|
+
expect(mockedAddMany.mock.calls[0][0][1].getKey()).toBe(MeldrineKeyTest);
|
|
109
|
+
expect(mockedAddMany.mock.calls[0][0][2].getKey()).toBe(MaydenaKeyTest);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('creations with options', () => {
|
|
113
|
+
const testOptions: Partial<DTBunchOptions> = {
|
|
114
|
+
errors: true,
|
|
115
|
+
uniqueKey: true,
|
|
116
|
+
inheritOwner: true,
|
|
117
|
+
};
|
|
118
|
+
const newBunch = new DTBunchTest(KeyTest, [], testOptions);
|
|
119
|
+
const parentConstructorMock = (DTComponentPhysical.prototype.constructor as MockedFunction<(key: string, options: any) => void>).mock;
|
|
120
|
+
|
|
121
|
+
expect(parentConstructorMock.calls[0][0]).toBe(KeyTest);
|
|
122
|
+
expect(parentConstructorMock.calls[0][1]).toStrictEqual({
|
|
123
|
+
errors: true,
|
|
124
|
+
uniqueKey: true,
|
|
125
|
+
inheritOwner: true,
|
|
126
|
+
replaceIndex: false,
|
|
127
|
+
virtualContext: false,
|
|
128
|
+
});
|
|
129
|
+
expect(newBunch.th_get_items()).toStrictEqual([]);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('_componentType', () => {
|
|
134
|
+
test('componentType must be "bunch"', () => {
|
|
135
|
+
expect(bunchTest.th_get_componentType()).toBe('bunch');
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('getFinderConfiguration()', () => {
|
|
140
|
+
const baseOperators = [
|
|
141
|
+
FilterOperatorType.EQ,
|
|
142
|
+
FilterOperatorType.IN,
|
|
143
|
+
FilterOperatorType.NIN,
|
|
144
|
+
FilterOperatorType.NE,
|
|
145
|
+
];
|
|
146
|
+
const advancedOperators = [
|
|
147
|
+
...baseOperators,
|
|
148
|
+
FilterOperatorType.GTE,
|
|
149
|
+
FilterOperatorType.LTE,
|
|
150
|
+
FilterOperatorType.CONTAINS,
|
|
151
|
+
FilterOperatorType.NCONTAINS,
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
test('check finder configuration for id attribute', () => {
|
|
155
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().id;
|
|
156
|
+
|
|
157
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(baseOperators);
|
|
158
|
+
expect(finderConfigurationToCheck.getValue(new DTElementStub())).toBe(IDElementTest);
|
|
159
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('check finder configuration for key attribute', () => {
|
|
163
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().key;
|
|
164
|
+
|
|
165
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(baseOperators);
|
|
166
|
+
expect(finderConfigurationToCheck.getValue(new DTElementStub())).toBe(KeyElementTest);
|
|
167
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(false);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('check finder configuration for owner attribute - empty owner', () => {
|
|
171
|
+
const element = new DTElementStub();
|
|
172
|
+
jest.spyOn(element, 'getOwner').mockImplementation(() => undefined);
|
|
173
|
+
|
|
174
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().owner;
|
|
175
|
+
|
|
176
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(baseOperators);
|
|
177
|
+
expect(finderConfigurationToCheck.getValue(element)).toBeNull();
|
|
178
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(false);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('check finder configuration for owner attribute - with owner', () => {
|
|
182
|
+
const element = new DTElementStub();
|
|
183
|
+
jest.spyOn(element, 'getOwner').mockImplementation(() => new DTPlayerStub());
|
|
184
|
+
|
|
185
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().owner;
|
|
186
|
+
|
|
187
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(baseOperators);
|
|
188
|
+
expect(finderConfigurationToCheck.getValue(element)).toBe(IDTestPlayer);
|
|
189
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(false);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('check finder configuration for context attribute - empty context', () => {
|
|
193
|
+
const element = new DTElementStub();
|
|
194
|
+
jest.spyOn(element, 'getContext').mockImplementation(() => undefined);
|
|
195
|
+
|
|
196
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().context;
|
|
197
|
+
|
|
198
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(baseOperators);
|
|
199
|
+
expect(finderConfigurationToCheck.getValue(element)).toBeNull();
|
|
200
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(false);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('check finder configuration for context attribute - with context', () => {
|
|
204
|
+
const element = new DTElementStub();
|
|
205
|
+
const bunch = new DTBunchStub();
|
|
206
|
+
jest.spyOn(bunch, 'getId').mockImplementation(() => `${IDTest}_other_context`);
|
|
207
|
+
jest.spyOn(element, 'getContext').mockImplementation(() => bunch);
|
|
208
|
+
|
|
209
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().context;
|
|
210
|
+
|
|
211
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(baseOperators);
|
|
212
|
+
expect(finderConfigurationToCheck.getValue(element)).toBe(`${IDTest}_other_context`);
|
|
213
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(false);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('check finder configuration for meta attribute', () => {
|
|
217
|
+
const element = new DTElementStub();
|
|
218
|
+
jest.spyOn(element, 'getManyMeta').mockImplementation(() => HaileiMetaData);
|
|
219
|
+
|
|
220
|
+
const finderConfigurationToCheck = bunchTest.getFinderConfiguration().meta;
|
|
221
|
+
|
|
222
|
+
expect(finderConfigurationToCheck.operators).toStrictEqual(advancedOperators);
|
|
223
|
+
expect(finderConfigurationToCheck.getValue(element)).toStrictEqual(HaileiMetaData);
|
|
224
|
+
expect(finderConfigurationToCheck.objectSearch).toBe(true);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe('setOwner()', () => {
|
|
229
|
+
beforeEach(() => {
|
|
230
|
+
jest.spyOn(bunchTest, 'getOwner').mockImplementation(() => new DTPlayerStub());
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('add a new owner - not updating elements owner when inheritOwner = false', () => {
|
|
234
|
+
bunchTest.th_set_options({ inheritOwner: false });
|
|
235
|
+
bunchTest.th_set_items(generateMockedElements(3));
|
|
236
|
+
|
|
237
|
+
const owner = new DTPlayerStub();
|
|
238
|
+
bunchTest.setOwner(owner);
|
|
239
|
+
|
|
240
|
+
expect(bunchTest.th_get_items()[0].setOwner.mock.calls.length).toBe(0);
|
|
241
|
+
expect(bunchTest.th_get_items()[1].setOwner.mock.calls.length).toBe(0);
|
|
242
|
+
expect(bunchTest.th_get_items()[2].setOwner.mock.calls.length).toBe(0);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('add a new owner - updating elements owner when inheritOwner = true', () => {
|
|
246
|
+
bunchTest.th_set_options({ inheritOwner: true });
|
|
247
|
+
bunchTest.th_set_items(generateMockedElements(3));
|
|
248
|
+
|
|
249
|
+
const owner = new DTPlayerStub();
|
|
250
|
+
bunchTest.setOwner(owner);
|
|
251
|
+
|
|
252
|
+
expect(bunchTest.th_get_items()[0].setOwner.mock.calls.length).toBe(1);
|
|
253
|
+
expect(bunchTest.th_get_items()[0].setOwner.mock.calls[0][0].getId()).toBe(IDPlayerTest);
|
|
254
|
+
expect(bunchTest.th_get_items()[1].setOwner.mock.calls.length).toBe(1);
|
|
255
|
+
expect(bunchTest.th_get_items()[1].setOwner.mock.calls[0][0].getId()).toBe(IDPlayerTest);
|
|
256
|
+
expect(bunchTest.th_get_items()[2].setOwner.mock.calls.length).toBe(1);
|
|
257
|
+
expect(bunchTest.th_get_items()[2].setOwner.mock.calls[0][0].getId()).toBe(IDPlayerTest);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe('removeOwner()', () => {
|
|
262
|
+
test('remove current Owner - not updating elements owner when inheritOwner = false', () => {
|
|
263
|
+
bunchTest.th_set_options({ inheritOwner: true });
|
|
264
|
+
bunchTest.th_set_items(generateMockedElements(2));
|
|
265
|
+
|
|
266
|
+
bunchTest.th_set_options({ inheritOwner: false });
|
|
267
|
+
bunchTest.removeOwner();
|
|
268
|
+
|
|
269
|
+
expect(bunchTest.th_get_items()[0].removeOwner.mock.calls.length).toBe(0);
|
|
270
|
+
expect(bunchTest.th_get_items()[1].removeOwner.mock.calls.length).toBe(0);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('remove current Owner - updating elements owner when inheritOwner = true', () => {
|
|
274
|
+
bunchTest.th_set_options({ inheritOwner: true });
|
|
275
|
+
bunchTest.th_set_items(generateMockedElements(2));
|
|
276
|
+
|
|
277
|
+
bunchTest.removeOwner();
|
|
278
|
+
|
|
279
|
+
expect(bunchTest.th_get_items()[0].removeOwner.mock.calls.length).toBe(1);
|
|
280
|
+
expect(bunchTest.th_get_items()[1].removeOwner.mock.calls.length).toBe(1);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
describe('get()', () => {
|
|
285
|
+
beforeEach(() => {
|
|
286
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test('return an item by its id', () => {
|
|
290
|
+
expect(bunchTest.get(`${MaydenaIdTest}-2`).getKey()).toBe(MaydenaKeyTest);
|
|
291
|
+
expect(bunchTest.get(`${YssaliaIdTest}-4`).getKey()).toBe(YssaliaKeyTest);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('return an item by its index', () => {
|
|
295
|
+
expect(bunchTest.get(2).getKey()).toBe(MaydenaKeyTest);
|
|
296
|
+
expect(bunchTest.get(4).getKey()).toBe(YssaliaKeyTest);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('return undefined if item not found by id', () => {
|
|
300
|
+
expect(bunchTest.get('id-123456')).toBeUndefined();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
test('return undefined if item not found by index', () => {
|
|
304
|
+
expect(bunchTest.get(11)).toBeUndefined();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test('return undefined if item not found by index - incoherent index', () => {
|
|
308
|
+
expect(bunchTest.get(-11)).toBeUndefined();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe('getAll()', () => {
|
|
313
|
+
test('return empty array if no items', () => {
|
|
314
|
+
expect(bunchTest.getAll()).toStrictEqual([]);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('return all items array', () => {
|
|
318
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
319
|
+
|
|
320
|
+
expect(bunchTest.getAll()[0].getKey()).toBe(HaileiKeyTest);
|
|
321
|
+
expect(bunchTest.getAll()[1].getKey()).toBe(MeldrineKeyTest);
|
|
322
|
+
expect(bunchTest.getAll()[2].getKey()).toBe(MaydenaKeyTest);
|
|
323
|
+
expect(bunchTest.getAll()[3].getKey()).toBe(IldressKeyTest);
|
|
324
|
+
expect(bunchTest.getAll()[4].getKey()).toBe(YssaliaKeyTest);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe('indexOf()', () => {
|
|
329
|
+
beforeEach(() => {
|
|
330
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('return index of an item by id', () => {
|
|
334
|
+
expect(bunchTest.indexOf(`${MaydenaIdTest}-2`)).toBe(2);
|
|
335
|
+
expect(bunchTest.indexOf(`${YssaliaIdTest}-4`)).toBe(4);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test('return -1 if id is not found', () => {
|
|
339
|
+
expect(bunchTest.indexOf('id-123456')).toBe(-1);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// TODO : Add test cases for updating Library from manager
|
|
344
|
+
describe('addAtIndex()', () => {
|
|
345
|
+
let objectToAdd;
|
|
346
|
+
let objectsToAdd;
|
|
347
|
+
|
|
348
|
+
beforeEach(() => {
|
|
349
|
+
jest.spyOn(bunchTest, 'getId').mockReturnValue(IDTest);
|
|
350
|
+
|
|
351
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
352
|
+
|
|
353
|
+
objectsToAdd = generateMockedElements(6);
|
|
354
|
+
objectToAdd = objectsToAdd[5];
|
|
355
|
+
jest.spyOn(bunchTest, 'find').mockReturnValue([objectToAdd]);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test('add a new item at last index - simple case', () => {
|
|
359
|
+
bunchTest.addAtIndex(objectToAdd, 5);
|
|
360
|
+
|
|
361
|
+
expect(bunchTest.th_get_items()[5].getId()).toBe(objectToAdd.getId());
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test('add a new item at specified index and reindex - default case', () => {
|
|
365
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
366
|
+
|
|
367
|
+
expect(bunchTest.th_get_items()[2].getId()).toBe(objectToAdd.getId());
|
|
368
|
+
expect(bunchTest.th_get_items()[3].getId()).toBe(`${MaydenaIdTest}-2`);
|
|
369
|
+
expect(bunchTest.th_get_items()[4].getId()).toBe(`${IldressIdTest}-3`);
|
|
370
|
+
expect(bunchTest.th_get_items()[5].getId()).toBe(`${YssaliaIdTest}-4`);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test('add a new item at specified index and replace - replace option', () => {
|
|
374
|
+
bunchTest.addAtIndex(objectToAdd, 2, { replaceIndex: true });
|
|
375
|
+
|
|
376
|
+
expect(bunchTest.th_get_items()[2].getId()).toBe(objectToAdd.getId());
|
|
377
|
+
expect(bunchTest.th_get_items()[3].getId()).toBe(`${IldressIdTest}-3`);
|
|
378
|
+
expect(bunchTest.th_get_items()[4].getId()).toBe(`${YssaliaIdTest}-4`);
|
|
379
|
+
expect(bunchTest.th_get_items()[5]).toBeUndefined();
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test('add a new item at lower index', () => {
|
|
383
|
+
bunchTest.addAtIndex(objectToAdd, -11);
|
|
384
|
+
|
|
385
|
+
expect(bunchTest.th_get_items()[0].getId()).toBe(objectToAdd.getId());
|
|
386
|
+
expect(bunchTest.th_get_items()[1].getId()).toBe(`${HaileiIdTest}-0`);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test('add a new item at greater index', () => {
|
|
390
|
+
bunchTest.addAtIndex(objectToAdd, 11);
|
|
391
|
+
|
|
392
|
+
expect(bunchTest.th_get_items()[5].getId()).toBe(objectToAdd.getId());
|
|
393
|
+
expect(bunchTest.th_get_items()[6]).toBeUndefined();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test('trigger conflict when adding two same ids - parent triggerError', () => {
|
|
397
|
+
const mockedTriggerError = DTBunch.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
|
|
398
|
+
|
|
399
|
+
bunchTest.addAtIndex(objectsToAdd[0], 2);
|
|
400
|
+
|
|
401
|
+
expect(mockedTriggerError.mock.calls.length).toBe(1);
|
|
402
|
+
checkCallForMockedDTError(
|
|
403
|
+
'id_conflict',
|
|
404
|
+
'Element with same id already exists in the bunch',
|
|
405
|
+
IDTest,
|
|
406
|
+
objectsToAdd[0].getId(),
|
|
407
|
+
);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test('no conflict when adding two same keys - default case', () => {
|
|
411
|
+
const mockedTriggerError = DTBunch.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
|
|
412
|
+
|
|
413
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
414
|
+
|
|
415
|
+
expect(mockedTriggerError.mock.calls.length).toBe(0);
|
|
416
|
+
expect((bunchTest.find as any).mock.calls.length).toBe(0);
|
|
417
|
+
expect(bunchTest.th_get_items()[0].getKey()).toBe(HaileiKeyTest);
|
|
418
|
+
expect(bunchTest.th_get_items()[2].getKey()).toBe(HaileiKeyTest);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('trigger conflict when adding two same keys - uniqueKey option and parent triggerError', () => {
|
|
422
|
+
const mockedTriggerError = DTBunch.prototype.triggerError as MockedFunction<(error: DYOToolsError) => void>;
|
|
423
|
+
|
|
424
|
+
bunchTest.addAtIndex(objectToAdd, 2, { uniqueKey: true });
|
|
425
|
+
|
|
426
|
+
expect(mockedTriggerError.mock.calls.length).toBe(1);
|
|
427
|
+
checkCallForMockedDTError(
|
|
428
|
+
'key_conflict',
|
|
429
|
+
'Element with same key already exists in the bunch',
|
|
430
|
+
IDTest,
|
|
431
|
+
objectToAdd.getId(),
|
|
432
|
+
);
|
|
433
|
+
expect((bunchTest.find as any).mock.calls.length).toBe(1);
|
|
434
|
+
expect((bunchTest.find as any).mock.calls[0][0]).toStrictEqual({ key: { $eq: objectToAdd.getKey() } });
|
|
435
|
+
expect(bunchTest.th_get_items()[2].getId()).toBe(`${MaydenaIdTest}-2`);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
test('not inherit owner when adding an item - default case', () => {
|
|
439
|
+
const owner = new DTPlayerStub();
|
|
440
|
+
bunchTest.th_set_owner(owner);
|
|
441
|
+
|
|
442
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
443
|
+
|
|
444
|
+
expect(bunchTest.th_get_owner().getId()).toBe(IDPlayerTest);
|
|
445
|
+
expect(objectToAdd.setOwner.mock.calls.length).toBe(0);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
test('inherit owner when adding an item - inheritOwner option', () => {
|
|
449
|
+
const owner = new DTPlayerStub();
|
|
450
|
+
bunchTest.th_set_owner(owner);
|
|
451
|
+
|
|
452
|
+
bunchTest.addAtIndex(objectToAdd, 2, { inheritOwner: true });
|
|
453
|
+
|
|
454
|
+
expect(bunchTest.th_get_owner().getId()).toBe(IDPlayerTest);
|
|
455
|
+
expect(objectToAdd.setOwner.mock.calls.length).toBe(1);
|
|
456
|
+
expect(objectToAdd.setOwner.mock.calls[0][0].getId()).toBe(IDPlayerTest);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
test('set context when adding an item - default case', () => {
|
|
460
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
461
|
+
|
|
462
|
+
expect(bunchTest.th_get_items()[2].setContext.mock.calls.length).toBe(1);
|
|
463
|
+
expect(bunchTest.th_get_items()[2].setContext.mock.calls[0][0].getId()).toBe(IDTest);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
test('not set context when adding an item - virtualContext option', () => {
|
|
467
|
+
bunchTest.th_set_options({ virtualContext: true });
|
|
468
|
+
|
|
469
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
470
|
+
|
|
471
|
+
expect(bunchTest.th_get_items()[2].setContext.mock.calls.length).toBe(0);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test('set context when adding an item - remove from old bunch', () => {
|
|
475
|
+
const bunchTestOld = new DTBunchTest();
|
|
476
|
+
bunchTestOld.th_set_items(generateMockedElements(6));
|
|
477
|
+
objectToAdd = bunchTestOld.th_get_items()[5];
|
|
478
|
+
objectToAdd.setContext(bunchTestOld);
|
|
479
|
+
objectToAdd.setContext.mockClear();
|
|
480
|
+
jest.spyOn(bunchTestOld, 'remove').mockImplementation(() => {});
|
|
481
|
+
jest.spyOn(bunchTestOld, 'getComponentType').mockImplementation(() => 'bunch');
|
|
482
|
+
|
|
483
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
484
|
+
|
|
485
|
+
expect(bunchTest.th_get_items()[2].setContext.mock.calls.length).toBe(1);
|
|
486
|
+
expect(bunchTest.th_get_items()[2].setContext.mock.calls[0][0].getId()).toBe(IDTest);
|
|
487
|
+
expect((bunchTestOld.remove as any).mock.calls.length).toBe(1);
|
|
488
|
+
expect((bunchTestOld.remove as any).mock.calls[0][0]).toBe(objectToAdd.getId());
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test('set context when adding an item - dont remove if virtualContext option', () => {
|
|
492
|
+
const bunchTestOld = new DTBunchTest();
|
|
493
|
+
bunchTestOld.th_set_items(generateMockedElements(6));
|
|
494
|
+
objectToAdd = bunchTestOld.th_get_items()[5];
|
|
495
|
+
jest.spyOn(bunchTestOld, 'remove').mockImplementation(() => {});
|
|
496
|
+
|
|
497
|
+
bunchTest.th_set_options({ virtualContext: true });
|
|
498
|
+
bunchTest.addAtIndex(objectToAdd, 2);
|
|
499
|
+
|
|
500
|
+
expect(bunchTest.th_get_items()[2].setContext.mock.calls.length).toBe(0);
|
|
501
|
+
expect((bunchTestOld.remove as any).mock.calls.length).toBe(0);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
test('manager context - add item into the manager library', () => {
|
|
505
|
+
const managerTest = new DTManagerStub();
|
|
506
|
+
jest.spyOn(bunchTest, 'getContext').mockImplementation(
|
|
507
|
+
(contextType) => contextType === 'manager' && managerTest,
|
|
508
|
+
);
|
|
509
|
+
jest.spyOn(managerTest, 'getLibrary').mockImplementation(() => managerTest.th_get_library());
|
|
510
|
+
jest.spyOn(managerTest.th_get_library(), 'add');
|
|
511
|
+
|
|
512
|
+
bunchTest.addAtIndex(objectToAdd, 5);
|
|
513
|
+
|
|
514
|
+
expect((managerTest.th_get_library().add as any).mock.calls.length).toBe(1);
|
|
515
|
+
expect((managerTest.th_get_library().add as any).mock.calls[0][0]).toStrictEqual(objectToAdd);
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
test('manager context - not add existing item into the manager library', () => {
|
|
519
|
+
const managerTest = new DTManagerStub();
|
|
520
|
+
jest.spyOn(bunchTest, 'getContext').mockImplementation(
|
|
521
|
+
(contextType) => contextType === 'manager' && managerTest,
|
|
522
|
+
);
|
|
523
|
+
jest.spyOn(managerTest, 'getLibrary').mockImplementation(() => managerTest.th_get_library());
|
|
524
|
+
jest.spyOn(managerTest.th_get_library(), 'add');
|
|
525
|
+
managerTest.th_get_library().th_set_items([objectToAdd]);
|
|
526
|
+
|
|
527
|
+
bunchTest.addAtIndex(objectToAdd, 5);
|
|
528
|
+
|
|
529
|
+
expect((managerTest.th_get_library().add as any).mock.calls.length).toBe(0);
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
describe('add()', () => {
|
|
534
|
+
let objectsToAdd;
|
|
535
|
+
let objectToAdd;
|
|
536
|
+
|
|
537
|
+
beforeEach(() => {
|
|
538
|
+
jest.spyOn(bunchTest, 'addAtIndex').mockImplementation(() => {});
|
|
539
|
+
|
|
540
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
541
|
+
|
|
542
|
+
objectsToAdd = generateMockedElements(6);
|
|
543
|
+
objectToAdd = objectsToAdd[5];
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
test('add as last item using addAtIndex', () => {
|
|
547
|
+
const testBunchOptions = {
|
|
548
|
+
replaceIndex: true,
|
|
549
|
+
inheritOwner: true,
|
|
550
|
+
};
|
|
551
|
+
bunchTest.add(objectToAdd, testBunchOptions);
|
|
552
|
+
|
|
553
|
+
expect((bunchTest.addAtIndex as any).mock.calls.length).toBe(1);
|
|
554
|
+
expect((bunchTest.addAtIndex as any).mock.calls[0][0].getId()).toBe(objectToAdd.getId());
|
|
555
|
+
expect((bunchTest.addAtIndex as any).mock.calls[0][1]).toBe(5);
|
|
556
|
+
expect((bunchTest.addAtIndex as any).mock.calls[0][2]).toStrictEqual(testBunchOptions);
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
describe('addManyAtIndex()', () => {
|
|
561
|
+
let itemsToAdd;
|
|
562
|
+
let itemLibrary;
|
|
563
|
+
|
|
564
|
+
beforeEach(() => {
|
|
565
|
+
jest.spyOn(bunchTest, 'addAtIndex').mockImplementation(() => {});
|
|
566
|
+
|
|
567
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
568
|
+
|
|
569
|
+
itemLibrary = generateMockedElements(8);
|
|
570
|
+
itemsToAdd = [itemLibrary[5], itemLibrary[6], itemLibrary[7]];
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
test('add many items at index using addAtIndex - simple case', () => {
|
|
574
|
+
const testBunchOptions = {
|
|
575
|
+
replaceIndex: true,
|
|
576
|
+
inheritOwner: true,
|
|
577
|
+
};
|
|
578
|
+
const indexToAdd = 2;
|
|
579
|
+
bunchTest.addManyAtIndex(itemsToAdd, indexToAdd, testBunchOptions);
|
|
580
|
+
|
|
581
|
+
expect((bunchTest.addAtIndex as any).mock.calls.length).toBe(3);
|
|
582
|
+
for (let i = 0; i < 3; i++) {
|
|
583
|
+
expect((bunchTest.addAtIndex as any).mock.calls[i][0].getId()).toBe(itemsToAdd[i].getId());
|
|
584
|
+
expect((bunchTest.addAtIndex as any).mock.calls[i][1]).toBe(i + indexToAdd);
|
|
585
|
+
expect((bunchTest.addAtIndex as any).mock.calls[i][2]).toStrictEqual(testBunchOptions);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
test('add many items at index using addAtIndex - index lower than 0', () => {
|
|
590
|
+
const indexToAdd = -11;
|
|
591
|
+
bunchTest.addManyAtIndex(itemsToAdd, indexToAdd);
|
|
592
|
+
|
|
593
|
+
expect((bunchTest.addAtIndex as any).mock.calls.length).toBe(3);
|
|
594
|
+
for (let i = 0; i < 3; i++) {
|
|
595
|
+
expect((bunchTest.addAtIndex as any).mock.calls[i][0].getId()).toBe(itemsToAdd[i].getId());
|
|
596
|
+
expect((bunchTest.addAtIndex as any).mock.calls[i][1]).toBe(i);
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
test('errors when adding many items at index - default case - add no items and throw error', () => {
|
|
601
|
+
const indexToAdd = 2;
|
|
602
|
+
let errorThrown: DTErrorStub;
|
|
603
|
+
jest.spyOn(bunchTest, 'addAtIndex').mockImplementationOnce((item, index, options) => {
|
|
604
|
+
throw new DTErrorStub();
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
try {
|
|
608
|
+
bunchTest.addManyAtIndex(itemsToAdd, indexToAdd);
|
|
609
|
+
} catch (error: unknown) {
|
|
610
|
+
errorThrown = error as DTErrorStub;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
expect(errorThrown).toBeDefined();
|
|
614
|
+
expect(DTErrorStub).toHaveBeenCalled();
|
|
615
|
+
expect(errorThrown.getCode()).toBe(DTErrorCodeTest);
|
|
616
|
+
expect((bunchTest.addAtIndex as any).mock.calls.length).toBe(1);
|
|
617
|
+
expect((bunchTest.addAtIndex as any).mock.calls[0][0].getId()).toBe(itemsToAdd[0].getId());
|
|
618
|
+
expect((bunchTest.addAtIndex as any).mock.calls[0][1]).toBe(indexToAdd);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test('errors when adding many items at index - errors case - add success items and stack errors for others', () => {
|
|
622
|
+
const indexToAdd = 2;
|
|
623
|
+
const errors = [];
|
|
624
|
+
jest.spyOn(bunchTest, 'addAtIndex')
|
|
625
|
+
.mockImplementationOnce((item, index, options) => {
|
|
626
|
+
errors.push(new DTErrorStub());
|
|
627
|
+
})
|
|
628
|
+
.mockImplementationOnce((item, index, options) => {
|
|
629
|
+
errors.push(new DTErrorStub());
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
bunchTest.addManyAtIndex(itemsToAdd, indexToAdd, { errors: true });
|
|
633
|
+
|
|
634
|
+
expect(errors.length).toBe(2);
|
|
635
|
+
expect(DTErrorStub).toHaveBeenCalled();
|
|
636
|
+
expect((bunchTest.addAtIndex as any).mock.calls.length).toBe(3);
|
|
637
|
+
expect((bunchTest.addAtIndex as any).mock.calls[0][0].getId()).toBe(itemsToAdd[0].getId());
|
|
638
|
+
expect((bunchTest.addAtIndex as any).mock.calls[1][0].getId()).toBe(itemsToAdd[1].getId());
|
|
639
|
+
expect((bunchTest.addAtIndex as any).mock.calls[2][0].getId()).toBe(itemsToAdd[2].getId());
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
describe('addMany()', () => {
|
|
644
|
+
let itemsToAdd;
|
|
645
|
+
let itemLibrary;
|
|
646
|
+
|
|
647
|
+
beforeEach(() => {
|
|
648
|
+
jest.spyOn(bunchTest, 'addManyAtIndex').mockImplementation(() => {});
|
|
649
|
+
|
|
650
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
651
|
+
|
|
652
|
+
itemLibrary = generateMockedElements(8);
|
|
653
|
+
itemsToAdd = [itemLibrary[5], itemLibrary[6], itemLibrary[7]];
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
test('add many items as last item using addManyAtIndex', () => {
|
|
657
|
+
const testBunchOptions = {
|
|
658
|
+
replaceIndex: true,
|
|
659
|
+
inheritOwner: true,
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
bunchTest.addMany(itemsToAdd, testBunchOptions);
|
|
663
|
+
|
|
664
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls.length).toBe(1);
|
|
665
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls[0][0].length).toBe(3);
|
|
666
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls[0][0][0].getId()).toBe(`${HaileiIdTest}-5`);
|
|
667
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls[0][0][1].getId()).toBe(`${MeldrineIdTest}-6`);
|
|
668
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls[0][0][2].getId()).toBe(`${MaydenaIdTest}-7`);
|
|
669
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls[0][1]).toBe(5);
|
|
670
|
+
expect((bunchTest.addManyAtIndex as any).mock.calls[0][2]).toStrictEqual(testBunchOptions);
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
describe('removeMany()', () => {
|
|
675
|
+
const checkAllItemsInBunch = (bunch: DTBunchTest, itemRemoved = 3) => {
|
|
676
|
+
const sup = itemRemoved;
|
|
677
|
+
expect(bunchTest.getAll().length).toBe(5 - sup);
|
|
678
|
+
expect(bunchTest.th_get_items()[0].getId()).toBe(`${HaileiIdTest}-0`);
|
|
679
|
+
itemRemoved < 1 && expect(bunchTest.th_get_items()[1 - sup].getId()).toBe(`${MeldrineIdTest}-1`);
|
|
680
|
+
itemRemoved < 2 && expect(bunchTest.th_get_items()[2 - sup].getId()).toBe(`${MaydenaIdTest}-2`);
|
|
681
|
+
itemRemoved < 3 && expect(bunchTest.th_get_items()[3 - sup].getId()).toBe(`${IldressIdTest}-3`);
|
|
682
|
+
expect(bunchTest.th_get_items()[4 - sup].getId()).toBe(`${YssaliaIdTest}-4`);
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
beforeEach(() => {
|
|
686
|
+
jest.spyOn(bunchTest, 'removeContext').mockImplementation(() => {});
|
|
687
|
+
|
|
688
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
test('remove many items by ids', () => {
|
|
692
|
+
bunchTest.removeMany([`${MeldrineIdTest}-1`, `${MaydenaIdTest}-2`, `${IldressIdTest}-3`]);
|
|
693
|
+
|
|
694
|
+
checkAllItemsInBunch(bunchTest);
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
test('remove many items by indexes', () => {
|
|
698
|
+
bunchTest.removeMany([1, 2, 3]);
|
|
699
|
+
|
|
700
|
+
checkAllItemsInBunch(bunchTest);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
test('remove only item with corresponding ids', () => {
|
|
704
|
+
bunchTest.removeMany([`${MeldrineIdTest}-1`, `${MaydenaIdTest}-2`, 'id-12345']);
|
|
705
|
+
|
|
706
|
+
checkAllItemsInBunch(bunchTest, 2);
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
test('remove only item with corresponding indexes', () => {
|
|
710
|
+
bunchTest.removeMany([1, 2, 11]);
|
|
711
|
+
|
|
712
|
+
checkAllItemsInBunch(bunchTest, 2);
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
test('remove only item with corresponding indexes - incoherent index', () => {
|
|
716
|
+
bunchTest.removeMany([-11, 2, 1, -13]);
|
|
717
|
+
|
|
718
|
+
checkAllItemsInBunch(bunchTest, 2);
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
test('define context at undefined for removed items - default case', () => {
|
|
722
|
+
const items = [
|
|
723
|
+
bunchTest.th_get_items()[1],
|
|
724
|
+
bunchTest.th_get_items()[2],
|
|
725
|
+
];
|
|
726
|
+
|
|
727
|
+
bunchTest.removeMany([`${MeldrineIdTest}-1`, `${MaydenaIdTest}-2`]);
|
|
728
|
+
items.push(bunchTest.th_get_items()[0]);
|
|
729
|
+
items.push(bunchTest.th_get_items()[1]);
|
|
730
|
+
bunchTest.removeMany([0, 1]);
|
|
731
|
+
|
|
732
|
+
expect(items[0].removeContext.mock.calls.length).toBe(1);
|
|
733
|
+
expect(items[1].removeContext.mock.calls.length).toBe(1);
|
|
734
|
+
expect(items[2].removeContext.mock.calls.length).toBe(1);
|
|
735
|
+
expect(items[3].removeContext.mock.calls.length).toBe(1);
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
test('not change context for removed items - virtual context option', () => {
|
|
739
|
+
const items = [
|
|
740
|
+
bunchTest.th_get_items()[1],
|
|
741
|
+
bunchTest.th_get_items()[2],
|
|
742
|
+
];
|
|
743
|
+
bunchTest.th_set_options({ virtualContext: true });
|
|
744
|
+
bunchTest.removeMany([`${MeldrineIdTest}-1`, `${MaydenaIdTest}-2`]);
|
|
745
|
+
items.push(bunchTest.th_get_items()[0]);
|
|
746
|
+
items.push(bunchTest.th_get_items()[1]);
|
|
747
|
+
bunchTest.removeMany([0, 1]);
|
|
748
|
+
|
|
749
|
+
expect(items[0].removeContext.mock.calls.length).toBe(0);
|
|
750
|
+
expect(items[1].removeContext.mock.calls.length).toBe(0);
|
|
751
|
+
expect(items[2].removeContext.mock.calls.length).toBe(0);
|
|
752
|
+
expect(items[3].removeContext.mock.calls.length).toBe(0);
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
describe('remove()', () => {
|
|
757
|
+
beforeEach(() => {
|
|
758
|
+
jest.spyOn(bunchTest, 'removeMany').mockImplementation(() => {
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
test('remove one item by id using removeMany', () => {
|
|
765
|
+
bunchTest.remove(`${MaydenaIdTest}-2`);
|
|
766
|
+
|
|
767
|
+
expect((bunchTest.removeMany as any).mock.calls.length).toBe(1);
|
|
768
|
+
expect((bunchTest.removeMany as any).mock.calls[0][0]).toStrictEqual([`${MaydenaIdTest}-2`]);
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
test('remove one item by index using removeMany', () => {
|
|
772
|
+
bunchTest.remove(2);
|
|
773
|
+
|
|
774
|
+
expect((bunchTest.removeMany as any).mock.calls.length).toBe(1);
|
|
775
|
+
expect((bunchTest.removeMany as any).mock.calls[0][0]).toStrictEqual([2]);
|
|
776
|
+
});
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
describe('removeAll()', () => {
|
|
780
|
+
beforeEach(() => {
|
|
781
|
+
jest.spyOn(bunchTest, 'removeMany').mockImplementation(() => {
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
test('remove all items using removeMany', () => {
|
|
788
|
+
bunchTest.removeAll();
|
|
789
|
+
|
|
790
|
+
expect((bunchTest.removeMany as any).mock.calls.length).toBe(1);
|
|
791
|
+
expect((bunchTest.removeMany as any).mock.calls[0][0]).toStrictEqual([0, 1, 2, 3, 4]);
|
|
792
|
+
});
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
describe('copy()', () => {
|
|
796
|
+
// @see copy.spec.ts for unit tests about copy method
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
describe('find()', () => {
|
|
800
|
+
test('find items using DYOFinder - empty case', () => {
|
|
801
|
+
bunchTest.find({});
|
|
802
|
+
|
|
803
|
+
expect((bunchTest.th_get_finder() as any).execute.mock.calls.length).toBe(1);
|
|
804
|
+
expect((bunchTest.th_get_finder() as any).execute.mock.calls[0][0]).toStrictEqual({});
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
test('find items using DYOFinder', () => {
|
|
808
|
+
const testFilters = { id: { $eq: 'id_bunch' }, key: { $ne: 'key_test' } };
|
|
809
|
+
bunchTest.find(testFilters);
|
|
810
|
+
|
|
811
|
+
expect((bunchTest.th_get_finder() as any).execute.mock.calls.length).toBe(1);
|
|
812
|
+
expect((bunchTest.th_get_finder() as any).execute.mock.calls[0][0]).toStrictEqual(testFilters);
|
|
813
|
+
});
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
describe('toObject()', () => {
|
|
817
|
+
beforeEach(() => {
|
|
818
|
+
bunchTest.th_set_id(IDTest);
|
|
819
|
+
bunchTest.th_set_key(KeyTest);
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
test('toObject output standard', () => {
|
|
823
|
+
const toObjectBunch = bunchTest.toObject();
|
|
824
|
+
|
|
825
|
+
expect(Object.keys(toObjectBunch)).toStrictEqual(['id', 'key', 'type', 'items']);
|
|
826
|
+
expect(toObjectBunch.id).toBe(IDTest);
|
|
827
|
+
expect(toObjectBunch.key).toBe(KeyTest);
|
|
828
|
+
expect(toObjectBunch.type).toBe('bunch');
|
|
829
|
+
expect(toObjectBunch.items.length).toBe(0);
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
test('toObject output standard with owner', () => {
|
|
833
|
+
bunchTest.th_set_owner(new DTPlayerStub());
|
|
834
|
+
|
|
835
|
+
const toObjectBunch = bunchTest.toObject();
|
|
836
|
+
expect(Object.keys(toObjectBunch)).toStrictEqual(['id', 'key', 'type', 'items', 'owner']);
|
|
837
|
+
expect(toObjectBunch.owner.toString()).toBe(toStringPlayerTest);
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
test('toObject output standard with owner and meta', () => {
|
|
841
|
+
jest.spyOn(bunchTest, 'getManyMeta').mockImplementation(function () {
|
|
842
|
+
return this._meta;
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
bunchTest.th_set_owner(new DTPlayerStub());
|
|
846
|
+
bunchTest.th_set_meta(BunchMetaData);
|
|
847
|
+
|
|
848
|
+
const toObjectBunch = bunchTest.toObject();
|
|
849
|
+
expect(Object.keys(toObjectBunch)).toStrictEqual(['id', 'key', 'type', 'items', 'owner', 'meta']);
|
|
850
|
+
expect(toObjectBunch.meta).toStrictEqual(BunchMetaData);
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
test('toObject output standard with items', () => {
|
|
854
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
855
|
+
const toObjectBunch = bunchTest.toObject();
|
|
856
|
+
|
|
857
|
+
expect(Object.keys(toObjectBunch)).toStrictEqual(['id', 'key', 'type', 'items']);
|
|
858
|
+
expect(toObjectBunch.items.length).toBe(5);
|
|
859
|
+
expect(toObjectBunch.items).toStrictEqual([
|
|
860
|
+
HaileiToObjectTest,
|
|
861
|
+
MeldrineToObjectTest,
|
|
862
|
+
MaydenaToObjectTest,
|
|
863
|
+
IldressToObjectTest,
|
|
864
|
+
YssaliaToObjectTest,
|
|
865
|
+
]);
|
|
866
|
+
});
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
describe('toString()', () => {
|
|
870
|
+
beforeEach(() => {
|
|
871
|
+
bunchTest.th_set_key(KeyTest);
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
test('string output standard', () => {
|
|
875
|
+
const toStringBunch = bunchTest.toString();
|
|
876
|
+
|
|
877
|
+
expect(toStringBunch).toBe(`Component ${KeyTest} - Type: Bunch - Items: 0`);
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
test('string output standard with items', () => {
|
|
881
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
882
|
+
const toStringBunch = bunchTest.toString();
|
|
883
|
+
|
|
884
|
+
expect(toStringBunch).toBe(`Component ${KeyTest} - Type: Bunch - Items: 5`);
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
test('string output standard with items and owner', () => {
|
|
888
|
+
bunchTest.th_set_items(generateMockedElements(5));
|
|
889
|
+
bunchTest.th_set_owner(new DTPlayerStub());
|
|
890
|
+
|
|
891
|
+
const toStringBunch = bunchTest.toString();
|
|
892
|
+
expect(toStringBunch).toBe(`Component ${KeyTest} - Type: Bunch - Owner: ${KeyPlayerTest} - Items: 5`);
|
|
893
|
+
});
|
|
894
|
+
});
|
|
895
|
+
});
|