dyo-tools 0.1.0-rc1

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 (78) hide show
  1. package/.c8rc.json +4 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +41 -0
  4. package/Makefile +34 -0
  5. package/README.md +0 -0
  6. package/TODO.md +18 -0
  7. package/babel.config.js +1 -0
  8. package/dist/core/DTBunch.d.ts +32 -0
  9. package/dist/core/DTBunch.js +283 -0
  10. package/dist/core/DTBunch.js.map +1 -0
  11. package/dist/core/DTComponent.d.ts +20 -0
  12. package/dist/core/DTComponent.js +41 -0
  13. package/dist/core/DTComponent.js.map +1 -0
  14. package/dist/core/DTComponentWithMeta.d.ts +9 -0
  15. package/dist/core/DTComponentWithMeta.js +32 -0
  16. package/dist/core/DTComponentWithMeta.js.map +1 -0
  17. package/dist/core/DTElement.d.ts +13 -0
  18. package/dist/core/DTElement.js +46 -0
  19. package/dist/core/DTElement.js.map +1 -0
  20. package/dist/core/DTError.d.ts +13 -0
  21. package/dist/core/DTError.js +28 -0
  22. package/dist/core/DTError.js.map +1 -0
  23. package/dist/core/DTPlayer.d.ts +8 -0
  24. package/dist/core/DTPlayer.js +30 -0
  25. package/dist/core/DTPlayer.js.map +1 -0
  26. package/dist/index.d.ts +6 -0
  27. package/dist/index.js +16 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/tsconfig.tsbuildinfo +1321 -0
  30. package/dist/types/index.d.ts +58 -0
  31. package/dist/types/index.js +15 -0
  32. package/dist/types/index.js.map +1 -0
  33. package/dist/utils/filters.d.ts +6 -0
  34. package/dist/utils/filters.js +39 -0
  35. package/dist/utils/filters.js.map +1 -0
  36. package/docs/.nojekyll +1 -0
  37. package/docs/assets/highlight.css +22 -0
  38. package/docs/assets/icons.css +1043 -0
  39. package/docs/assets/icons.png +0 -0
  40. package/docs/assets/icons@2x.png +0 -0
  41. package/docs/assets/main.js +52 -0
  42. package/docs/assets/search.js +1 -0
  43. package/docs/assets/style.css +1388 -0
  44. package/docs/assets/widgets.png +0 -0
  45. package/docs/assets/widgets@2x.png +0 -0
  46. package/docs/classes/DTBunch.html +265 -0
  47. package/docs/classes/DTComponent.html +49 -0
  48. package/docs/classes/DTComponentWithMeta.html +73 -0
  49. package/docs/classes/DTElement.html +95 -0
  50. package/docs/classes/DTError.html +32 -0
  51. package/docs/classes/DTPlayer.html +86 -0
  52. package/docs/index.html +1 -0
  53. package/docs/modules.html +1 -0
  54. package/jest.config.js +6 -0
  55. package/package.json +41 -0
  56. package/src/core/DTBunch.ts +600 -0
  57. package/src/core/DTComponent.ts +135 -0
  58. package/src/core/DTComponentWithMeta.ts +62 -0
  59. package/src/core/DTElement.ts +96 -0
  60. package/src/core/DTError.ts +78 -0
  61. package/src/core/DTPlayer.ts +57 -0
  62. package/src/index.ts +7 -0
  63. package/src/types/index.ts +76 -0
  64. package/src/utils/filters.ts +64 -0
  65. package/test/core/DTBunch.double.ts +150 -0
  66. package/test/core/DTBunch.spec.ts +1374 -0
  67. package/test/core/DTComponent.double.ts +69 -0
  68. package/test/core/DTComponent.spec.ts +182 -0
  69. package/test/core/DTComponentWithMeta.double.ts +88 -0
  70. package/test/core/DTComponentWithMeta.spec.ts +112 -0
  71. package/test/core/DTElement.double.ts +27 -0
  72. package/test/core/DTElement.spec.ts +181 -0
  73. package/test/core/DTError.double.ts +43 -0
  74. package/test/core/DTError.spec.ts +106 -0
  75. package/test/core/DTPlayer.double.ts +49 -0
  76. package/test/core/DTPlayer.spec.ts +102 -0
  77. package/test/utils/filters.spec.ts +109 -0
  78. package/tsconfig.json +21 -0
@@ -0,0 +1,69 @@
1
+ import {DTComponent} from "../../src";
2
+
3
+ // Global test variables
4
+ export const IDTest = "DTComponent-id-1234567";
5
+ export const KeyTest = "DTComponent-key-1234567";
6
+ export const ComponentTypeTest = "DTComponent-componentType-test";
7
+ export const ToObjectTest = { type: "DTComponent-test-toObject" };
8
+ export const ToStringTest = "DTComponent-test-toString";
9
+ export const DomainTest = "DTComponent-domain-test";
10
+ export const SubKindTest = "DTComponent-subkind-test";
11
+
12
+ // Test Child for DTComponent class (mocked abstract methods)
13
+ export class DTComponentTest extends DTComponent {
14
+ protected _componentType: string = ComponentTypeTest;
15
+
16
+ copy(): DTComponent {
17
+ return this;
18
+ }
19
+
20
+ toObject(): any {
21
+ return ToObjectTest;
22
+ }
23
+
24
+ toString(): string {
25
+ return ToStringTest;
26
+ }
27
+ }
28
+
29
+ // Mock constructor for DTComponentTest class (getter tests)
30
+ interface IDTComponentTestMockProps {
31
+ componentType?: string
32
+ domain?: string
33
+ subKind?: string
34
+ context?: DTComponent
35
+ }
36
+
37
+ export class DTComponentTestMock extends DTComponentTest {
38
+ constructor(props?: IDTComponentTestMockProps) {
39
+ super();
40
+ this._id = IDTest;
41
+ this._key = KeyTest;
42
+ if (props) {
43
+ this._componentType = props.componentType || ComponentTypeTest;
44
+ this._domain = props.domain || DomainTest;
45
+ this._subKind = props.subKind || SubKindTest;
46
+ this._context = props.context || undefined;
47
+ } else {
48
+ this._componentType = this._componentType || undefined;
49
+ this._domain = undefined;
50
+ this._subKind = undefined;
51
+ this._context = undefined;
52
+ }
53
+
54
+ }
55
+ }
56
+
57
+ // Stub for Component (used for other tests)
58
+ export class DTComponentStub extends DTComponentTest {
59
+ private readonly stubId: string;
60
+
61
+ constructor(idSuffix: string = "") {
62
+ super();
63
+ this.stubId = IDTest + (idSuffix && `-${idSuffix}`);
64
+ }
65
+
66
+ getId() {
67
+ return this.stubId;
68
+ }
69
+ }
@@ -0,0 +1,182 @@
1
+ import {
2
+ jest, describe, expect, test, beforeEach, afterEach,
3
+ } from '@jest/globals';
4
+ import {
5
+ ComponentTypeTest,
6
+ DomainTest,
7
+ DTComponentTest,
8
+ DTComponentTestMock,
9
+ IDTest,
10
+ KeyTest,
11
+ SubKindTest,
12
+ } from './DTComponent.double';
13
+
14
+ describe('class DYOToolsComponent', () => {
15
+ let componentMock: DTComponentTestMock;
16
+
17
+ beforeEach(() => {
18
+ componentMock = new DTComponentTestMock();
19
+ });
20
+
21
+ afterEach(() => {
22
+ jest.restoreAllMocks();
23
+ });
24
+
25
+ describe('constructor()', () => {
26
+ test('creation without key', () => {
27
+ const component = new DTComponentTest();
28
+ jest.spyOn(component, 'getId').mockImplementation(function () {
29
+ return this._id;
30
+ });
31
+ jest.spyOn(component, 'getKey').mockImplementation(function () {
32
+ return this._key;
33
+ });
34
+
35
+ expect(component.getKey()).toBe(component.getId());
36
+ });
37
+
38
+ test('creation with key', () => {
39
+ const component = new DTComponentTest(KeyTest);
40
+ jest.spyOn(component, 'getId').mockImplementation(function () {
41
+ return this._id;
42
+ });
43
+ jest.spyOn(component, 'getKey').mockImplementation(function () {
44
+ return this._key;
45
+ });
46
+
47
+ expect(component.getKey()).toBe(KeyTest);
48
+ expect(component.getId() !== component.getKey()).toBeTruthy();
49
+ });
50
+
51
+ test('creations have unique ids', () => {
52
+ const component = new DTComponentTest(KeyTest);
53
+ const component2 = new DTComponentTest(KeyTest);
54
+ jest.spyOn(component, 'getId').mockImplementation(function () {
55
+ return this._id;
56
+ });
57
+ jest.spyOn(component2, 'getId').mockImplementation(function () {
58
+ return this._key;
59
+ });
60
+
61
+ expect(component.getId() !== component2.getId()).toBeTruthy();
62
+ });
63
+ });
64
+
65
+ describe('getId()', () => {
66
+ test('return id', () => {
67
+ expect(componentMock.getId()).toBe(IDTest);
68
+ });
69
+ });
70
+
71
+ describe('getKey()', () => {
72
+ test('return key', () => {
73
+ expect(componentMock.getKey()).toBe(KeyTest);
74
+ });
75
+ });
76
+
77
+ describe('getComponentType()', () => {
78
+ test('return componentType', () => {
79
+ const componentMockSet = new DTComponentTestMock({ componentType: `${ComponentTypeTest}-set` });
80
+
81
+ expect(componentMock.getComponentType()).toBe(ComponentTypeTest);
82
+ expect(componentMockSet.getComponentType()).toBe(`${ComponentTypeTest}-set`);
83
+ });
84
+ });
85
+
86
+ describe('getDomain()', () => {
87
+ test('return domain', () => {
88
+ const componentMockSet = new DTComponentTestMock({ domain: DomainTest });
89
+
90
+ expect(componentMock.getDomain()).toBeUndefined();
91
+ expect(componentMockSet.getDomain()).toBe(DomainTest);
92
+ });
93
+ });
94
+
95
+ describe('getSubKind()', () => {
96
+ test('return subkind', () => {
97
+ const componentMockSet = new DTComponentTestMock({ subKind: SubKindTest });
98
+
99
+ expect(componentMock.getSubKind()).toBeUndefined();
100
+ expect(componentMockSet.getSubKind()).toBe(SubKindTest);
101
+ });
102
+ });
103
+
104
+ describe('getContext()', () => {
105
+ test('return context', () => {
106
+ const component = new DTComponentTest();
107
+ const componentMockSet = new DTComponentTestMock({ context: component });
108
+
109
+ expect(componentMock.getContext()).toBeUndefined();
110
+ expect(componentMockSet.getContext()).toStrictEqual(component);
111
+ });
112
+
113
+ test('return context with Hierarchy - simple case', () => {
114
+ const componentRank1 = new DTComponentTestMock({ componentType: 'rank1' });
115
+ const componentRank2 = new DTComponentTestMock({ componentType: 'rank2', context: componentRank1 });
116
+ const componentRank3 = new DTComponentTestMock({ componentType: 'rank3', context: componentRank2 });
117
+ jest.spyOn(componentRank1, 'getComponentType').mockImplementation(function () {
118
+ return this._componentType;
119
+ });
120
+ jest.spyOn(componentRank2, 'getComponentType').mockImplementation(function () {
121
+ return this._componentType;
122
+ });
123
+ jest.spyOn(componentRank3, 'getComponentType').mockImplementation(function () {
124
+ return this._componentType;
125
+ });
126
+
127
+ expect(componentRank3.getContext()).toStrictEqual(componentRank2);
128
+ expect(componentRank3.getContext('rank2')).toStrictEqual(componentRank2);
129
+ expect(componentRank3.getContext('rank1')).toStrictEqual(componentRank1);
130
+ expect(componentRank3.getContext('rank0')).toBeUndefined();
131
+ });
132
+
133
+ test('return context with Hierarchy - same type case so return first one', () => {
134
+ const componentRank0 = new DTComponentTestMock({ componentType: 'rank1' });
135
+ const componentRank1 = new DTComponentTestMock({ componentType: 'rank1', context: componentRank0 });
136
+ const componentRank2 = new DTComponentTestMock({ componentType: 'rank2', context: componentRank1 });
137
+ const componentRank3 = new DTComponentTestMock({ componentType: 'rank3', context: componentRank2 });
138
+ jest.spyOn(componentRank0, 'getComponentType').mockImplementation(function () {
139
+ return this._componentType;
140
+ });
141
+ jest.spyOn(componentRank1, 'getComponentType').mockImplementation(function () {
142
+ return this._componentType;
143
+ });
144
+ jest.spyOn(componentRank2, 'getComponentType').mockImplementation(function () {
145
+ return this._componentType;
146
+ });
147
+ jest.spyOn(componentRank3, 'getComponentType').mockImplementation(function () {
148
+ return this._componentType;
149
+ });
150
+
151
+ expect(componentRank3.getContext('rank1')).toStrictEqual(componentRank1);
152
+ });
153
+ });
154
+
155
+ describe('setContext()', () => {
156
+ test('change context', () => {
157
+ const componentRank1 = new DTComponentTestMock({ componentType: 'rank1' });
158
+ jest.spyOn(componentMock, 'getContext').mockImplementation(function () {
159
+ return this._context;
160
+ });
161
+ componentMock.setContext(componentRank1);
162
+
163
+ expect(componentMock.getContext()).toBe(componentRank1);
164
+ });
165
+ });
166
+
167
+ describe('removeOwner()', () => {
168
+ test('remove the current Context', () => {
169
+ const componentRankSup = new DTComponentTestMock();
170
+ jest.spyOn(componentMock, 'setContext').mockImplementation(function (context) {
171
+ this._context = context;
172
+ });
173
+ jest.spyOn(componentMock, 'getContext').mockImplementation(function () {
174
+ return this._context;
175
+ });
176
+ componentMock.setContext(componentRankSup);
177
+
178
+ componentMock.removeContext();
179
+ expect(componentMock.getContext()).toBeUndefined();
180
+ });
181
+ });
182
+ });
@@ -0,0 +1,88 @@
1
+ import { DTComponentWithMeta } from '../../src';
2
+ import {DTAcceptedMetaData} from "../../src/types";
3
+
4
+ // Global test variables
5
+ export const ComponentTypeTest = 'DTComponentWithMeta-componentType-test';
6
+
7
+ // Test meta data interface
8
+ export interface IMetaDataTest extends DTAcceptedMetaData {
9
+ name: string,
10
+ queen: boolean,
11
+ kd: number[],
12
+ rank?: number,
13
+ tribes?: string[]
14
+ }
15
+
16
+ // Elementary pentacle default values
17
+ export const HaileiMetaData: IMetaDataTest = {
18
+ name: 'Hailei Dorcan Kazan',
19
+ queen: true,
20
+ kd: [47, 1],
21
+ rank: 1,
22
+ tribes: ['Peuple de Salta', 'Fils de Salta', 'Peuple Kanti'],
23
+ };
24
+ export const MeldrineMetaData: IMetaDataTest = {
25
+ name: 'Meldrine Goldmane',
26
+ queen: false,
27
+ kd: [53, 0],
28
+ rank: 2,
29
+ tribes: ['Lodaniens'],
30
+ };
31
+ export const MaydenaMetaData: IMetaDataTest = {
32
+ name: "Maydena 'Intan Kazan",
33
+ queen: true,
34
+ kd: [29, 0],
35
+ tribes: ['Exil rouge', 'Désolation'],
36
+ };
37
+ export const IldressMetaData: IMetaDataTest = {
38
+ name: 'Electel Ildress',
39
+ queen: false,
40
+ kd: [19, 1],
41
+ rank: 3,
42
+ };
43
+ export const YssaliaMetaData: IMetaDataTest = {
44
+ name: 'Yssalia du Gillit',
45
+ queen: true,
46
+ kd: [23, 0],
47
+ };
48
+
49
+ // Player default value
50
+ export const PlayerMetaData: IMetaDataTest = {
51
+ name: 'Llhôonn',
52
+ queen: true,
53
+ kd: [0, 0],
54
+ }
55
+
56
+ // Bunch default value
57
+ export const BunchMetaData: IMetaDataTest = {
58
+ name: 'Elementary Pentacle',
59
+ queen: true,
60
+ kd: [117, 3],
61
+ };
62
+
63
+ // Test Child for DTComponentWithMeta class (mocked abstract methods)
64
+ export class DTComponentWithMetaTest extends DTComponentWithMeta<IMetaDataTest> {
65
+ protected _componentType: string = ComponentTypeTest;
66
+
67
+ copy(): DTComponentWithMeta<IMetaDataTest> {
68
+ return this;
69
+ }
70
+
71
+ toObject(): any {
72
+ return {};
73
+ }
74
+
75
+ toString(): string {
76
+ return '';
77
+ }
78
+ }
79
+
80
+ // Mock constructor for DTComponentTest class (getter tests)
81
+ export class DTComponentWithMetaTestMock extends DTComponentWithMetaTest {
82
+ constructor(defaultMeta?: IMetaDataTest) {
83
+ super();
84
+ if (defaultMeta) {
85
+ this._meta = { ...defaultMeta };
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,112 @@
1
+ import {
2
+ jest, describe, expect, test, beforeEach, afterEach,
3
+ } from '@jest/globals';
4
+ import {
5
+ DTComponentWithMetaTestMock, HaileiMetaData, MeldrineMetaData, IMetaDataTest, YssaliaMetaData,
6
+ } from './DTComponentWithMeta.double';
7
+
8
+ describe('class DYOToolsComponent', () => {
9
+ let componentMock: DTComponentWithMetaTestMock;
10
+
11
+ beforeEach(() => {
12
+ componentMock = new DTComponentWithMetaTestMock(HaileiMetaData);
13
+ });
14
+
15
+ afterEach(() => {
16
+ jest.restoreAllMocks();
17
+ });
18
+
19
+ describe('getMeta()', () => {
20
+ test('return one meta by key', () => {
21
+ expect(componentMock.getMeta('name')).toBe(HaileiMetaData.name);
22
+ });
23
+
24
+ test('return undefined if meta is empty', () => {
25
+ componentMock = new DTComponentWithMetaTestMock();
26
+
27
+ expect(componentMock.getMeta('name')).toBeUndefined();
28
+ });
29
+ });
30
+
31
+ describe('setMeta()', () => {
32
+ test('set a new value for a meta', () => {
33
+ jest.spyOn(componentMock, 'getManyMeta').mockImplementation(function () {
34
+ return this._meta;
35
+ });
36
+
37
+ componentMock.setMeta('name', MeldrineMetaData.name);
38
+ const newMeta = componentMock.getManyMeta();
39
+
40
+ expect(newMeta.name === HaileiMetaData.name).toBeFalsy();
41
+ expect(newMeta.rank === HaileiMetaData.rank).toBeTruthy();
42
+ expect(newMeta.queen === HaileiMetaData.queen).toBeTruthy();
43
+ expect(newMeta.kd[0] === HaileiMetaData.kd[0]).toBeTruthy();
44
+ expect(newMeta.tribes[0] === HaileiMetaData.tribes[0]).toBeTruthy();
45
+ });
46
+ });
47
+
48
+ describe('getManyMeta()', () => {
49
+ test('return all meta', () => {
50
+ expect(componentMock.getManyMeta()).toStrictEqual(HaileiMetaData);
51
+ });
52
+
53
+ test('return meta by keys', () => {
54
+ const keys: Array<keyof IMetaDataTest> = ['name', 'kd', 'queen'];
55
+
56
+ expect(componentMock.getManyMeta(keys)).toStrictEqual({
57
+ name: HaileiMetaData.name,
58
+ kd: HaileiMetaData.kd,
59
+ queen: HaileiMetaData.queen,
60
+ });
61
+ });
62
+
63
+ test('return meta by keys - unexisting keys', () => {
64
+ componentMock = new DTComponentWithMetaTestMock(YssaliaMetaData);
65
+ const keys: Array<keyof IMetaDataTest> = ['name', 'rank', 'tribes'];
66
+
67
+ expect(componentMock.getManyMeta(keys)).toStrictEqual({
68
+ name: YssaliaMetaData.name,
69
+ });
70
+ });
71
+
72
+ test('return empty object if meta is empty', () => {
73
+ componentMock = new DTComponentWithMetaTestMock();
74
+ const keys: Array<keyof IMetaDataTest> = ['name', 'rank', 'tribes'];
75
+
76
+ expect(componentMock.getManyMeta(keys)).toStrictEqual({});
77
+ });
78
+ });
79
+
80
+ describe('setManyMeta()', () => {
81
+ test('set new values', () => {
82
+ jest.spyOn(componentMock, 'getManyMeta').mockImplementation(function () {
83
+ return this._meta;
84
+ });
85
+ const setValues: Partial<IMetaDataTest> = {
86
+ name: MeldrineMetaData.name,
87
+ queen: MeldrineMetaData.queen,
88
+ rank: MeldrineMetaData.rank,
89
+ };
90
+
91
+ componentMock.setManyMeta(setValues);
92
+ const newMeta = componentMock.getManyMeta();
93
+
94
+ expect(newMeta.name === HaileiMetaData.name).toBeFalsy();
95
+ expect(newMeta.queen === HaileiMetaData.queen).toBeFalsy();
96
+ expect(newMeta.kd[0] === HaileiMetaData.kd[0]).toBeTruthy();
97
+ expect(newMeta.rank === HaileiMetaData.rank).toBeFalsy();
98
+ expect(newMeta.tribes[0] === HaileiMetaData.tribes[0]).toBeTruthy();
99
+ });
100
+
101
+ test('set no new values', () => {
102
+ jest.spyOn(componentMock, 'getManyMeta').mockImplementation(function () {
103
+ return this._meta;
104
+ });
105
+
106
+ componentMock.setManyMeta({});
107
+ const newMeta = componentMock.getManyMeta();
108
+
109
+ expect(newMeta).toStrictEqual(HaileiMetaData);
110
+ });
111
+ });
112
+ });
@@ -0,0 +1,27 @@
1
+ import { DTElement } from '../../src';
2
+ import { IMetaDataTest, MeldrineMetaData } from './DTComponentWithMeta.double';
3
+
4
+ // Global Variables
5
+ export const IDTest = 'DTElement-id-1234567';
6
+ export const KeyTest = 'DTElement-key-1234567';
7
+
8
+ // Mock Constructor and parent methods for DTElement
9
+ export class DTElementMock extends DTElement<IMetaDataTest> {
10
+ constructor() {
11
+ super();
12
+ this._id = IDTest;
13
+ this._key = KeyTest;
14
+ }
15
+
16
+ getComponentType(): string {
17
+ return this._componentType;
18
+ }
19
+
20
+ setManyMeta(metaValues: Partial<IMetaDataTest>) {
21
+ this._meta = MeldrineMetaData;
22
+ }
23
+
24
+ getManyMeta(metaKeys: Array<keyof IMetaDataTest> = []): Partial<IMetaDataTest> {
25
+ return this._meta;
26
+ }
27
+ }
@@ -0,0 +1,181 @@
1
+ import { describe, test, beforeEach } from '@jest/globals';
2
+ import {
3
+ DTPlayerStub, IDTest as IDPlayerTest, toStringTest as toStringPlayerTest, KeyTest as KeyPlayerTest,
4
+ } from './DTPlayer.double';
5
+ import { DTElementMock, IDTest, KeyTest } from './DTElement.double';
6
+ import { MeldrineMetaData } from './DTComponentWithMeta.double';
7
+
8
+ describe('class DYOToolsElement', () => {
9
+ let elementMock: DTElementMock;
10
+
11
+ beforeEach(() => {
12
+ elementMock = new DTElementMock();
13
+ });
14
+
15
+ afterEach(() => {
16
+ jest.restoreAllMocks();
17
+ });
18
+
19
+ describe('_componentType', () => {
20
+ test('componentType must be "element"', () => {
21
+ expect(elementMock.getComponentType()).toBe('element');
22
+ });
23
+ });
24
+
25
+ describe('getOwner()', () => {
26
+ test('return empty owner by default', () => {
27
+ expect(elementMock.getOwner()).toBeUndefined();
28
+ });
29
+
30
+ test('return owner when set', () => {
31
+ const owner = new DTPlayerStub();
32
+ jest.spyOn(elementMock, 'setOwner').mockImplementation(function (owner) {
33
+ this._owner = owner;
34
+ });
35
+ elementMock.setOwner(owner);
36
+
37
+ expect(elementMock.getOwner().getId()).toBe(IDPlayerTest);
38
+ });
39
+ });
40
+
41
+ describe('setOwner()', () => {
42
+ test('add a new owner', () => {
43
+ const owner = new DTPlayerStub();
44
+ jest.spyOn(elementMock, 'getOwner').mockImplementation(function () {
45
+ return this._owner;
46
+ });
47
+ elementMock.setOwner(owner);
48
+
49
+ expect(elementMock.getOwner().getId()).toBe(IDPlayerTest);
50
+ });
51
+ });
52
+
53
+ describe('removeOwner()', () => {
54
+ test('remove the current Owner', () => {
55
+ const owner = new DTPlayerStub();
56
+ jest.spyOn(elementMock, 'setOwner').mockImplementation(function (owner) {
57
+ this._owner = owner;
58
+ });
59
+ jest.spyOn(elementMock, 'getOwner').mockImplementation(function () {
60
+ return this._owner;
61
+ });
62
+ elementMock.setOwner(owner);
63
+
64
+ elementMock.removeOwner();
65
+ expect(elementMock.getOwner()).toBeUndefined();
66
+ });
67
+ });
68
+
69
+ describe('copy()', () => {
70
+ test('copy an element - simple case with id and key', () => {
71
+ // This test doesn't mock the DOC (Depended-on Component) correctly
72
+ // Need to change implementation to implement correct testing
73
+ const elementMockCopy = elementMock.copy();
74
+ jest.spyOn(elementMock, 'getId').mockImplementation(function () {
75
+ return this._id;
76
+ });
77
+ jest.spyOn(elementMockCopy, 'getId').mockImplementation(function () {
78
+ return this._id;
79
+ });
80
+ jest.spyOn(elementMock, 'getKey').mockImplementation(function () {
81
+ return this._key;
82
+ });
83
+ jest.spyOn(elementMockCopy, 'getKey').mockImplementation(function () {
84
+ return this._key;
85
+ });
86
+
87
+ expect(elementMock.getId() === elementMockCopy.getId()).toBeFalsy();
88
+ expect(elementMock.getKey() === elementMockCopy.getKey()).toBeTruthy();
89
+ });
90
+
91
+ test('copy an element - not copy owner and context', () => {
92
+ // This test doesn't mock the DOC (Depended-on Component) correctly
93
+ // Need to change implementation to implement correct testing
94
+ jest.spyOn(elementMock, 'setContext').mockImplementation(function (context) {
95
+ this._context = context;
96
+ });
97
+ jest.spyOn(elementMock, 'setOwner').mockImplementation(function (owner) {
98
+ this._owner = owner;
99
+ });
100
+
101
+ elementMock.setContext(new DTElementMock());
102
+ elementMock.setOwner(new DTPlayerStub());
103
+
104
+ const elementMockCopy = elementMock.copy();
105
+ jest.spyOn(elementMockCopy, 'getContext').mockImplementation(function () {
106
+ return this._context;
107
+ });
108
+ jest.spyOn(elementMockCopy, 'getOwner').mockImplementation(function () {
109
+ return this._owner;
110
+ });
111
+
112
+ expect(elementMockCopy.getContext()).toBeUndefined();
113
+ expect(elementMockCopy.getOwner()).toBeUndefined();
114
+ });
115
+
116
+ test('copy an element - copy meta-data', () => {
117
+ // This test doesn't mock the DOC (Depended-on Component) correctly
118
+ // Need to change implementation to implement correct testing
119
+ elementMock.setManyMeta({});
120
+
121
+ const elementMockCopy = elementMock.copy();
122
+ jest.spyOn(elementMockCopy, 'getManyMeta').mockImplementation(function () {
123
+ return this._meta;
124
+ });
125
+
126
+ expect(elementMockCopy.getManyMeta()).toStrictEqual(MeldrineMetaData);
127
+ });
128
+ });
129
+
130
+ describe('toObject()', () => {
131
+ test('toObject output standard', () => {
132
+ const toObjectElement = elementMock.toObject();
133
+
134
+ expect(Object.keys(toObjectElement)).toStrictEqual(['id', 'key', 'type']);
135
+ expect(toObjectElement.id).toBe(IDTest);
136
+ expect(toObjectElement.key).toBe(KeyTest);
137
+ expect(toObjectElement.type).toBe('element');
138
+ });
139
+
140
+ test('toObject output standard with owner', () => {
141
+ jest.spyOn(elementMock, 'setOwner').mockImplementation(function (owner) {
142
+ this._owner = owner;
143
+ });
144
+ elementMock.setOwner(new DTPlayerStub());
145
+
146
+ const toObjectElement = elementMock.toObject();
147
+ expect(Object.keys(toObjectElement)).toStrictEqual(['id', 'key', 'type', 'owner']);
148
+ expect(toObjectElement.owner.toString()).toBe(toStringPlayerTest);
149
+ });
150
+
151
+ test('toObject output standard with owner and meta', () => {
152
+ jest.spyOn(elementMock, 'setOwner').mockImplementation(function (owner) {
153
+ this._owner = owner;
154
+ });
155
+ elementMock.setOwner(new DTPlayerStub());
156
+ elementMock.setManyMeta({});
157
+
158
+ const toObjectElement = elementMock.toObject();
159
+ expect(Object.keys(toObjectElement)).toStrictEqual(['id', 'key', 'type', 'owner', 'meta']);
160
+ expect(toObjectElement.meta).toStrictEqual(MeldrineMetaData);
161
+ });
162
+ });
163
+
164
+ describe('toString()', () => {
165
+ test('string output standard', () => {
166
+ const toStringElement = elementMock.toString();
167
+
168
+ expect(toStringElement).toBe(`Component ${KeyTest} - Type: Element`);
169
+ });
170
+
171
+ test('string output standard with owner', () => {
172
+ jest.spyOn(elementMock, 'setOwner').mockImplementation(function (owner) {
173
+ this._owner = owner;
174
+ });
175
+ elementMock.setOwner(new DTPlayerStub());
176
+
177
+ const toStringElement = elementMock.toString();
178
+ expect(toStringElement).toBe(`Component ${KeyTest} - Type: Element - Owner: ${KeyPlayerTest}`);
179
+ });
180
+ });
181
+ });
@@ -0,0 +1,43 @@
1
+ import { DTError } from '../../src';
2
+ import { DTComponentStub } from './DTComponent.double';
3
+
4
+ // Global Variables
5
+ export const CodeTest = 'DTError-code-123456';
6
+ export const MessageTest = 'DTError-message-test';
7
+ export const TimestampTest = 1500000000000;
8
+
9
+ // Stub for Error (used for other tests)
10
+ export class DTErrorStub extends DTError {
11
+ constructor() {
12
+ super('', '');
13
+ this.code = CodeTest;
14
+ this.message = MessageTest;
15
+ this.timestamp = new Date(Math.random() * 100000000);
16
+ }
17
+
18
+ getCode(): string {
19
+ return CodeTest;
20
+ }
21
+
22
+ getMessage(): string {
23
+ return MessageTest;
24
+ }
25
+
26
+ getTimestamp(): Date {
27
+ return this.timestamp;
28
+ }
29
+ }
30
+
31
+ // Mock Constructor for DTError
32
+ export class DTErrorMock extends DTError {
33
+ constructor(setComponents = false) {
34
+ super('', '');
35
+ this.code = CodeTest;
36
+ this.message = MessageTest;
37
+ this.timestamp = new Date(TimestampTest);
38
+ if (setComponents) {
39
+ this.initiator = new DTComponentStub('initiator');
40
+ this.convicted = new DTComponentStub('convicted');
41
+ }
42
+ }
43
+ }