@nest-yalc-2/utils 1.0.0 → 2.1.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 (133) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/object-mapper.helper.spec.ts +1 -1
  3. package/src/__tests__/object-mapper.interceptor.spec.ts +1 -1
  4. package/src/index.d.ts +19 -19
  5. package/src/index.js +22 -35
  6. package/src/index.js.map +1 -1
  7. package/src/index.ts +21 -21
  8. package/src/object-mapper.interceptor.d.ts +1 -1
  9. package/src/object-mapper.interceptor.js +5 -10
  10. package/src/object-mapper.interceptor.js.map +1 -1
  11. package/src/object-mapper.interceptor.ts +1 -1
  12. package/src/simple-mapper.interceptor.js +2 -7
  13. package/src/simple-mapper.interceptor.js.map +1 -1
  14. package/src/__mocks__/enum.mock.ts +0 -9
  15. package/src/__tests__/class.helper.spec.ts +0 -32
  16. package/src/__tests__/command.helper.spec.ts +0 -35
  17. package/src/__tests__/config-manager.helper.spec.ts +0 -93
  18. package/src/__tests__/custom-validator.spec.ts +0 -34
  19. package/src/__tests__/data-structure.helper.spec.ts +0 -30
  20. package/src/__tests__/date.helper.spec.ts +0 -78
  21. package/src/__tests__/encryption.helper.spec.ts +0 -45
  22. package/src/__tests__/enum.helper.spec.ts +0 -53
  23. package/src/__tests__/env.helper.spec.ts +0 -68
  24. package/src/__tests__/error.helper.spec.ts +0 -20
  25. package/src/__tests__/faker-helper.spec.ts +0 -187
  26. package/src/__tests__/files.helper.spec.ts +0 -22
  27. package/src/__tests__/http.helper.spec.ts +0 -15
  28. package/src/__tests__/interval.helper.spec.ts +0 -29
  29. package/src/__tests__/math.helper.spec.ts +0 -24
  30. package/src/__tests__/object.helper.spec.ts +0 -181
  31. package/src/__tests__/plugin.helper.spec.ts +0 -85
  32. package/src/__tests__/promise.helper.spec.ts +0 -76
  33. package/src/__tests__/returnValue.spec.ts +0 -49
  34. package/src/__tests__/rxjs.helper.spec.ts +0 -79
  35. package/src/__tests__/tsconfig.json +0 -3
  36. package/src/__tests__/validator-helper.spec.ts +0 -34
  37. package/src/__tests__/zlib.helper.spec.ts +0 -20
  38. package/src/class.helper.d.ts +0 -4
  39. package/src/class.helper.js +0 -19
  40. package/src/class.helper.js.map +0 -1
  41. package/src/class.helper.ts +0 -33
  42. package/src/command.helper.d.ts +0 -1
  43. package/src/command.helper.js +0 -16
  44. package/src/command.helper.js.map +0 -1
  45. package/src/command.helper.ts +0 -19
  46. package/src/config-manager.helper.d.ts +0 -12
  47. package/src/config-manager.helper.js +0 -60
  48. package/src/config-manager.helper.js.map +0 -1
  49. package/src/config-manager.helper.ts +0 -122
  50. package/src/custom-validator.d.ts +0 -3
  51. package/src/custom-validator.js +0 -13
  52. package/src/custom-validator.js.map +0 -1
  53. package/src/custom-validator.ts +0 -13
  54. package/src/data-structure.helper.d.ts +0 -1
  55. package/src/data-structure.helper.js +0 -13
  56. package/src/data-structure.helper.js.map +0 -1
  57. package/src/data-structure.helper.ts +0 -14
  58. package/src/date.helper.d.ts +0 -12
  59. package/src/date.helper.js +0 -47
  60. package/src/date.helper.js.map +0 -1
  61. package/src/date.helper.ts +0 -65
  62. package/src/encryption.helper.d.ts +0 -2
  63. package/src/encryption.helper.js +0 -58
  64. package/src/encryption.helper.js.map +0 -1
  65. package/src/encryption.helper.ts +0 -29
  66. package/src/enum.helper.d.ts +0 -8
  67. package/src/enum.helper.js +0 -21
  68. package/src/enum.helper.js.map +0 -1
  69. package/src/enum.helper.ts +0 -34
  70. package/src/env.helper.d.ts +0 -3
  71. package/src/env.helper.js +0 -23
  72. package/src/env.helper.js.map +0 -1
  73. package/src/env.helper.ts +0 -35
  74. package/src/error.helper.d.ts +0 -1
  75. package/src/error.helper.js +0 -12
  76. package/src/error.helper.js.map +0 -1
  77. package/src/error.helper.ts +0 -12
  78. package/src/faker-helper.d.ts +0 -16
  79. package/src/faker-helper.js +0 -73
  80. package/src/faker-helper.js.map +0 -1
  81. package/src/faker-helper.ts +0 -98
  82. package/src/files.helper.d.ts +0 -2
  83. package/src/files.helper.js +0 -13
  84. package/src/files.helper.js.map +0 -1
  85. package/src/files.helper.ts +0 -20
  86. package/src/http.helper.d.ts +0 -26
  87. package/src/http.helper.js +0 -84
  88. package/src/http.helper.js.map +0 -1
  89. package/src/http.helper.ts +0 -152
  90. package/src/interval.helper.d.ts +0 -5
  91. package/src/interval.helper.js +0 -16
  92. package/src/interval.helper.js.map +0 -1
  93. package/src/interval.helper.ts +0 -13
  94. package/src/math.helper.d.ts +0 -1
  95. package/src/math.helper.js +0 -8
  96. package/src/math.helper.js.map +0 -1
  97. package/src/math.helper.ts +0 -4
  98. package/src/nest.decorator.d.ts +0 -3
  99. package/src/nest.decorator.js +0 -10
  100. package/src/nest.decorator.js.map +0 -1
  101. package/src/nest.decorator.ts +0 -10
  102. package/src/object-mapper.helper.d.ts +0 -16
  103. package/src/object-mapper.helper.js +0 -47
  104. package/src/object-mapper.helper.js.map +0 -1
  105. package/src/object-mapper.helper.ts +0 -148
  106. package/src/object.helper.d.ts +0 -18
  107. package/src/object.helper.js +0 -91
  108. package/src/object.helper.js.map +0 -1
  109. package/src/object.helper.ts +0 -173
  110. package/src/plugin.helper.d.ts +0 -22
  111. package/src/plugin.helper.js +0 -26
  112. package/src/plugin.helper.js.map +0 -1
  113. package/src/plugin.helper.ts +0 -42
  114. package/src/promise.helper.d.ts +0 -13
  115. package/src/promise.helper.js +0 -71
  116. package/src/promise.helper.js.map +0 -1
  117. package/src/promise.helper.ts +0 -65
  118. package/src/returnValue.d.ts +0 -14
  119. package/src/returnValue.js +0 -17
  120. package/src/returnValue.js.map +0 -1
  121. package/src/returnValue.ts +0 -26
  122. package/src/rxjs.helper.d.ts +0 -7
  123. package/src/rxjs.helper.js +0 -33
  124. package/src/rxjs.helper.js.map +0 -1
  125. package/src/rxjs.helper.ts +0 -71
  126. package/src/validator-helper.d.ts +0 -2
  127. package/src/validator-helper.js +0 -22
  128. package/src/validator-helper.js.map +0 -1
  129. package/src/validator-helper.ts +0 -26
  130. package/src/zlib.helper.d.ts +0 -3
  131. package/src/zlib.helper.js +0 -25
  132. package/src/zlib.helper.js.map +0 -1
  133. package/src/zlib.helper.ts +0 -19
@@ -1,53 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
-
11
- import {
12
- belongsToEnum,
13
- mergeEnums,
14
- getEnumValueByEnumKey,
15
- } from '../enum.helper.js';
16
- import { TestEnum1, TestEnum2 } from '../__mocks__/enum.mock.js';
17
-
18
- test('belongsToEnum should check if a value belongs to a specific Enum', () => {
19
- enum TestEnum {
20
- TEST = 'test',
21
- }
22
-
23
- let result = belongsToEnum(TestEnum, 'test');
24
- expect(result).toBeTruthy();
25
-
26
- result = belongsToEnum(TestEnum, 'TEST');
27
- expect(result).toBeFalsy();
28
- });
29
-
30
- test('mergeEnums should merge 2 enums', () => {
31
- const result = mergeEnums(TestEnum1, TestEnum2);
32
- const compareWith = {
33
- ...TestEnum1,
34
- ...TestEnum2,
35
- };
36
-
37
- expect(result).toMatchObject(compareWith);
38
- });
39
-
40
- test('getEnumValueByEnumKey should return value of enum 1 by key of enum 2', () => {
41
- const result = getEnumValueByEnumKey(TestEnum1, Object.keys(TestEnum2)[0]);
42
- expect(result).toBe('test2');
43
- });
44
-
45
- test('getEnumValueByEnumKey should return value of enum 1 by key of enum 2', () => {
46
- const result = getEnumValueByEnumKey(TestEnum1, Object.keys(TestEnum2)[1]);
47
- expect(result).toBeUndefined();
48
- });
49
-
50
- test('getEnumValueByEnumKey should return null when no undefined key is provided ', () => {
51
- const result = getEnumValueByEnumKey(TestEnum1, undefined);
52
- expect(result).toBeUndefined();
53
- });
@@ -1,68 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
-
11
- import { envIsTrue, envToArray, isProduction } from '../env.helper.js';
12
- import { envTestHelper } from '@nest-yalc-2/jest';
13
-
14
- describe('environment helper test', () => {
15
- it('should convert a comma separated list to an array', () => {
16
- process.env.Test = 'apple,pear,pineapple,cherry';
17
- const res = envToArray('Test');
18
- expect(res).toEqual(['apple', 'pear', 'pineapple', 'cherry']);
19
- });
20
-
21
- it('should return an empty array when the var is an empty string', () => {
22
- process.env.Test = '';
23
- const res = envToArray('Test');
24
- expect(res).toEqual([]);
25
- });
26
-
27
- it('should return an empty array when the var is missing', () => {
28
- delete process.env.Test;
29
- const res = envToArray('Test');
30
- expect(res).toEqual([]);
31
- });
32
-
33
- it('should return false for "undefined"', () => {
34
- expect(envIsTrue(undefined)).toEqual(false);
35
- });
36
-
37
- it('should return true for "true"', () => {
38
- expect(envIsTrue('true')).toEqual(true);
39
- });
40
-
41
- it('should return true for "1"', () => {
42
- expect(envIsTrue('1')).toEqual(true);
43
- });
44
-
45
- it('should return true for "on"', () => {
46
- expect(envIsTrue('on')).toEqual(true);
47
- });
48
-
49
- it('should return true if is production"', () => {
50
- const env = envTestHelper(process.env);
51
- env.setEnv('NODE_ENV', 'production');
52
- expect(isProduction(false)).toBeTruthy();
53
-
54
- env.setEnv('NODE_ENV', 'somethingelse');
55
- expect(isProduction(true)).toBeTruthy();
56
- env.reset();
57
- });
58
-
59
- it('should return false if is not production"', () => {
60
- const env = envTestHelper(process.env);
61
- env.setEnv('NODE_ENV', 'pipeline');
62
- expect(isProduction()).toBeFalsy();
63
-
64
- env.setEnv('NODE_ENV', 'somethingelse');
65
- expect(isProduction(false)).toBeFalsy();
66
- env.reset();
67
- });
68
- });
@@ -1,20 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
- import { throwWrap } from '../error.helper.js';
11
-
12
- describe('Test errors helpers', () => {
13
- it('should throw an error without wraping it', () => {
14
- const fixedErrorMessage = `i'm an error`;
15
- const fixedError = new Error(fixedErrorMessage);
16
-
17
- expect(() => throwWrap(fixedErrorMessage)).toThrow(fixedErrorMessage);
18
- expect(() => throwWrap(fixedError)).toThrow(fixedErrorMessage);
19
- });
20
- });
@@ -1,187 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
-
11
- import {
12
- DEF_FAKER_MAX_TIME,
13
- FakerHelper,
14
- } from '../faker-helper.js';
15
- import { faker } from '@faker-js/faker';
16
-
17
- enum testEnum {
18
- KEY = 'value',
19
- VALUE_KEY = 'test',
20
- }
21
- describe('faker helper test', () => {
22
- // Don't do beforeEach, since we need a single instance
23
- const fakerHelper = new FakerHelper();
24
- it('should generate a person object, with first-, lastName, gender and email', () => {
25
- jest.spyOn(faker.person, 'firstName').mockReturnValueOnce('Elon');
26
- jest.spyOn(faker.person, 'lastName').mockReturnValueOnce('Musk');
27
- jest
28
- .spyOn(faker.internet, 'email')
29
- .mockReturnValueOnce('Elon_Musk@gmail.test');
30
- const person = fakerHelper.createPerson();
31
- expect(person).toBeDefined();
32
- expect(person.email).toBeDefined();
33
- expect(person.firstName).toBeDefined();
34
- expect(person.lastName).toBeDefined();
35
- expect(person.gender).toBeDefined();
36
- });
37
-
38
- it('should not reuse already generated names', () => {
39
- jest.spyOn(faker.person, 'firstName').mockReturnValueOnce('Elon');
40
- jest.spyOn(faker.person, 'lastName').mockReturnValueOnce('Musk');
41
- jest
42
- .spyOn(faker.internet, 'email')
43
- .mockReturnValueOnce('Elon_Musk@gmail.test');
44
- const person = fakerHelper.createPerson();
45
- expect(person).toBeDefined();
46
- expect(person.email).not.toEqual('ElonMusk@google.test');
47
- expect(person.firstName).toBeDefined();
48
- expect(person.lastName).toBeDefined();
49
- expect(person.gender).toBeDefined();
50
- });
51
-
52
- it('should be able to use first and lastname to generate email', () => {
53
- const firstName = 'Elon';
54
- const lastName = 'Musk';
55
-
56
- const email = fakerHelper.generateNewEmail(firstName, lastName);
57
- expect(email).toContain(firstName);
58
-
59
- // Not validating lastName as the Faker library may not use it
60
- // @url https://github.com/Marak/faker.js/issues/791
61
- });
62
-
63
- it('should be able to use first name, lastname and provider to generate email', () => {
64
- const firstName = 'Elon';
65
- const lastName = 'Musk';
66
- const provider = 'tesla.com';
67
-
68
- const email = fakerHelper.generateNewEmail(firstName, lastName, provider);
69
- expect(email).toContain(firstName);
70
- expect(email).toContain(provider);
71
- });
72
-
73
- it('should generate unique emails on multiple calls', () => {
74
- const firstName = 'Elon';
75
- const lastName = 'Musk';
76
- const provider = 'dogecoin.lol';
77
-
78
- const email1 = fakerHelper.generateNewEmail(firstName, lastName, provider);
79
- const email2 = fakerHelper.generateNewEmail(firstName, lastName, provider);
80
- const email3 = fakerHelper.generateNewEmail(firstName, lastName, provider);
81
-
82
- expect(email1).not.toEqual(email2);
83
- expect(email2).not.toEqual(email3);
84
- });
85
-
86
- it('should fall back when unique email generation exceeds the time limit', () => {
87
- const helper = new FakerHelper();
88
- jest
89
- .spyOn(faker.internet, 'email')
90
- .mockReturnValueOnce('same@gmail.test');
91
- helper.generateNewEmail('Elon', 'Musk', 'gmail.test');
92
-
93
- jest
94
- .spyOn(Date, 'now')
95
- .mockReturnValueOnce(0)
96
- .mockReturnValueOnce(DEF_FAKER_MAX_TIME + 1);
97
- jest
98
- .spyOn(faker.internet, 'email')
99
- .mockReturnValueOnce('same@gmail.test')
100
- .mockReturnValueOnce('fallback@gmail.test');
101
-
102
- expect(helper.generateNewEmail('Elon', 'Musk', 'gmail.test')).toBe(
103
- 'fallback@gmail.test',
104
- );
105
- expect(
106
- (
107
- helper as unknown as { generatedEmails: Set<string> }
108
- ).generatedEmails.has('fallback@gmail.test'),
109
- ).toBe(true);
110
- });
111
-
112
- it('should be able to generate a valid birthDate between age 18 and 100 (YYYY-MM-DD) (min age)', () => {
113
- const currentDate = new Date();
114
- // make a copy instead of reusing pointer
115
- const returnDate = new Date(currentDate.toUTCString());
116
- jest.spyOn(faker.date, 'past').mockReturnValue(returnDate);
117
-
118
- const currentYear = currentDate.getUTCFullYear();
119
- const currentMonth = currentDate.getUTCMonth() + 1;
120
- const currentDay = currentDate.getUTCDate();
121
-
122
- const birthDate = fakerHelper.randomBirthDate();
123
- const splitted = birthDate.split('-');
124
-
125
- expect(parseInt(splitted[0])).toEqual(currentYear - 18);
126
-
127
- // TODO: fix it for the edge cases (february, 00:00 etc.)
128
-
129
- // expect(parseInt(splitted[1])).toEqual(currentMonth);
130
- // expect(parseInt(splitted[2])).toEqual(currentDay);
131
- });
132
-
133
- it('should be able to generate a valid birthDate between age 18 and 100 (YYYY-MM-DD) (max age)', () => {
134
- const currentDate = new Date();
135
- // make a copy instead of reusing pointer
136
- const returnDate = new Date(currentDate.valueOf());
137
- returnDate.setUTCFullYear(returnDate.getUTCFullYear() - 82);
138
- jest.spyOn(faker.date, 'past').mockReturnValue(returnDate);
139
-
140
- const currentYear = currentDate.getUTCFullYear();
141
- const currentMonth = currentDate.getUTCMonth() + 1;
142
- // const currentDay = currentDate.getUTCDate();
143
-
144
- const birthDate = fakerHelper.randomBirthDate();
145
- const splitted = birthDate.split('-');
146
-
147
- expect(parseInt(splitted[0])).toEqual(currentYear - 100);
148
-
149
- // TODO: fix it for the edge cases (february, 00:00 etc.)
150
-
151
- // expect(parseInt(splitted[1])).toEqual(currentMonth);
152
- // expect(parseInt(splitted[2])).toEqual(currentDay);
153
- });
154
-
155
- it('should generate valid lock dates in the past', () => {
156
- jest.clearAllMocks();
157
- jest.spyOn(faker.number, 'int').mockReturnValue(0 as never);
158
- expect(fakerHelper.randomLockDate().valueOf()).toBeLessThan(Date.now());
159
- });
160
-
161
- it('should generate valid lock dates in the future', () => {
162
- jest.clearAllMocks();
163
- jest.spyOn(faker.number, 'int').mockReturnValueOnce(1 as never);
164
- jest
165
- .spyOn(faker.date, 'future')
166
- .mockReturnValueOnce(new Date(Date.now() + 1000));
167
- expect(fakerHelper.randomLockDate().valueOf()).toBeGreaterThan(Date.now());
168
- });
169
-
170
- it('should return undefined when no lock is present', () => {
171
- jest.clearAllMocks();
172
- jest.spyOn(faker.number, 'int').mockReturnValue(2 as never);
173
- expect(fakerHelper.randomLockDate()).toEqual(undefined);
174
- });
175
-
176
- it('should return the first element of the enum', () => {
177
- jest.clearAllMocks();
178
- jest.spyOn(faker.number, 'int').mockReturnValue(0 as never);
179
- expect(fakerHelper.randomFromEnum(testEnum)).toEqual(testEnum.KEY);
180
- });
181
-
182
- it('should return a decimal', () => {
183
- jest.clearAllMocks();
184
- jest.spyOn(faker.number, 'float').mockReturnValue(3.14 as never);
185
- expect(fakerHelper.randomDecimal(0, 42, 2)).toEqual('3.14');
186
- });
187
- });
@@ -1,22 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
-
11
- import { ___dirname, __filename } from '../files.helper.js';
12
-
13
- describe('Test files.helper.ts', () => {
14
- it('should run __filename', () => {
15
- expect(__filename('file:///C:/Users/nn/Desktop/code/nestjs-yalc/utils/src/__tests__/files.helper.spec.ts')).toBeDefined();
16
- });
17
-
18
- it('should run ___dirname', () => {
19
- const dir = ___dirname('file:///C:/Users/nn/Desktop/code/nestjs-yalc/utils/src/__tests__/files.helper.spec.ts');
20
- expect(dir).toContain('utils');
21
- });
22
- });
@@ -1,15 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
-
3
- import { getHttpStatusDescription } from '../http.helper.js';
4
-
5
- describe('Test http.helper.ts', () => {
6
- it('should run getHttpStatusDescription', () => {
7
- expect(getHttpStatusDescription(200)).toBe(
8
- '200: Request successful and response provided.',
9
- );
10
- });
11
-
12
- it('should work with the fallback', () => {
13
- expect(getHttpStatusDescription(999)).toBe('Unknown status code');
14
- });
15
- });
@@ -1,29 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
- import { IntervalHelper } from '../interval.helper.js';
11
-
12
- describe('interval helper test', () => {
13
- it('Generates interval of 1 day with miliseconds', () => {
14
- const interval = IntervalHelper.createOneDayInterval();
15
-
16
- expect(interval).toEqual(86400000);
17
- });
18
-
19
- it('Generates 1 second interval', () => {
20
- const interval = IntervalHelper.createOneSecondInterval();
21
-
22
- expect(interval).toEqual(1000);
23
- });
24
- it('Generates interval of 5 mins with miliseconds', () => {
25
- const interval = IntervalHelper.createFiveMinutesInterval();
26
-
27
- expect(interval).toEqual(300000);
28
- });
29
- });
@@ -1,24 +0,0 @@
1
- import {
2
- describe,
3
- expect,
4
- it,
5
- jest,
6
- beforeAll,
7
- beforeEach,
8
- test,
9
- } from '@jest/globals';
10
- import { isScientificNotation } from '../math.helper.js';
11
-
12
- describe('math helper test', () => {
13
- it('should return true for scientific notation', () => {
14
- const result = isScientificNotation(0.000000002);
15
-
16
- expect(result).toBeTruthy();
17
- });
18
-
19
- it('should return false for non scientific notation', () => {
20
- const result = isScientificNotation(0.002);
21
-
22
- expect(result).toBeFalsy();
23
- });
24
- });
@@ -1,181 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import {
3
- _deepMerge,
4
- deepMerge,
5
- deepMergeWithoutArrayConcat,
6
- getObjectId,
7
- isObject,
8
- isObjectStrict,
9
- objectsHaveSameKeys,
10
- objectSetProp,
11
- } from '../object.helper.js';
12
-
13
- describe('test object.helper.ts', () => {
14
- it('should set a property correctly', () => {
15
- const obj: any = {};
16
- objectSetProp(obj, 'test', 1);
17
- expect(obj.test).toBe(1);
18
- });
19
-
20
- it('should set a subproperty correctly', () => {
21
- const obj: any = {};
22
- const res = objectSetProp(obj, 'test.foo', 1);
23
- expect(obj.test.foo).toBe(1);
24
- expect(res.test.foo).toBe(1);
25
- });
26
-
27
- it('should set a subproperty on an existing parent object', () => {
28
- const obj: any = { test: {} };
29
- const res = objectSetProp(obj, 'test.foo', 1);
30
- expect(res.test.foo).toBe(1);
31
- });
32
-
33
- it('should merge 2 objects correctly', () => {
34
- const obj1 = { test: 1, sub: { test2: 2 }, array: [1, 2, 4] };
35
- const obj2 = {
36
- test: 2,
37
- sub: { test3: 3 },
38
- new: {
39
- test4: 4,
40
- },
41
- array: [3, 4, 5],
42
- };
43
-
44
- const res = deepMerge(obj1, obj2);
45
-
46
- expect(res).toStrictEqual({
47
- test: 2,
48
- sub: {
49
- test2: 2,
50
- test3: 3,
51
- },
52
- new: {
53
- test4: 4,
54
- },
55
- array: [1, 2, 4, 3, 5],
56
- });
57
- });
58
-
59
- it('should merge objects correctly (last undefined)', () => {
60
- const obj1 = { test: 1, sub: { test2: 2 } };
61
-
62
- const res = deepMerge(obj1);
63
-
64
- expect(res).toStrictEqual({
65
- test: 1,
66
- sub: {
67
- test2: 2,
68
- },
69
- });
70
- });
71
-
72
- it('should skip merge work for non-object targets and sources', () => {
73
- const inherited = Object.create({ inherited: true });
74
- expect(_deepMerge(true, null as any, { test: 1 })).toBeNull();
75
- expect(deepMerge({}, undefined as any)).toStrictEqual({});
76
- expect(deepMerge({}, inherited)).toStrictEqual({});
77
- });
78
-
79
- it('should merge objects correctly with array concat', () => {
80
- const obj1 = { test: 1, sub: { test2: ['1', '2'] } };
81
- const obj2 = { test: 2, sub: { test2: ['3','2'] } };
82
-
83
- const res = deepMerge(obj1,obj2);
84
-
85
- expect(res).toStrictEqual({
86
- test: 2,
87
- sub: {
88
- test2: ['1', '2', '3'],
89
- },
90
- });
91
- });
92
-
93
- it('should merge objects correctly without array concat', () => {
94
- const obj1 = { test: 1, sub: { test2: ['1', '2'] } };
95
- const obj2 = { test: 2, sub: { test2: ['3','2'] } };
96
-
97
- const res = deepMergeWithoutArrayConcat(obj1,obj2);
98
-
99
- expect(res).toStrictEqual({
100
- test: 2,
101
- sub: {
102
- test2: ['3', '2'],
103
- },
104
- });
105
- });
106
-
107
- it('should merge objects correctly with functions', () => {
108
- const obj1 = { test: 1};
109
- const obj2 = { test: 2, sub: { test2: () => {} } };
110
-
111
- const res = deepMerge(obj1,obj2);
112
-
113
- expect(res).toStrictEqual({
114
- test: 2,
115
- sub: {
116
- test2: expect.any(Function),
117
- },
118
- });
119
- });
120
-
121
- it('should isObjectStrict return true', () => {
122
- expect(isObjectStrict({})).toBe(true);
123
- expect(isObjectStrict({ test: 1 })).toBe(true);
124
- expect(isObjectStrict({ test: 1, sub: { test2: 2 } })).toBe(true);
125
- expect(isObjectStrict(new Object('something'))).toBe(true);
126
- });
127
-
128
- it('should isObjectStrict return false', () => {
129
- expect(isObjectStrict(null)).toBe(false);
130
- expect(isObjectStrict(undefined)).toBe(false);
131
- expect(isObjectStrict(1)).toBe(false);
132
- expect(isObjectStrict('string')).toBe(false);
133
- expect(isObjectStrict(true)).toBe(false);
134
- expect(isObjectStrict(false)).toBe(false);
135
- expect(isObjectStrict([1, 2, 3])).toBe(false);
136
- expect(isObjectStrict(new Date())).toBe(false);
137
- expect(isObjectStrict(() => {})).toBe(false);
138
- });
139
-
140
- it('should isObject return true', () => {
141
- expect(isObject({})).toBe(true);
142
- expect(isObject({ test: 1 })).toBe(true);
143
- expect(isObject({ test: 1, sub: { test2: 2 } })).toBe(true);
144
- expect(isObject(new Object('something'))).toBe(true);
145
- expect(isObject(null)).toBe(false);
146
- expect(isObject(() => {})).toBe(true);
147
- });
148
-
149
- it('should isObject return false', () => {
150
- expect(isObject(undefined)).toBe(false);
151
- expect(isObject(1)).toBe(false);
152
- expect(isObject('string')).toBe(false);
153
- expect(isObject(true)).toBe(false);
154
- expect(isObject(false)).toBe(false);
155
- expect(isObject([1, 2, 3])).toBe(false);
156
- });
157
-
158
-
159
- it('should getObjectId return the correct id', () => {
160
- const obj = { id: 1 };
161
- let res = getObjectId(obj);
162
- expect(res).toBeGreaterThan(0);
163
- // it should give the same result
164
- expect(getObjectId(obj)).toBe(res);
165
- // different objects instead should give different results
166
- expect(getObjectId({ id: 1 })).not.toBe(1);
167
- });
168
-
169
-
170
- it('should objectsHaveSameKeys return true', () => {
171
- const obj1 = { test: 1, sub: { test2: 2 } };
172
- const obj2 = { test: 1, sub: { test2: 2 } };
173
- expect(objectsHaveSameKeys(obj1, obj2)).toBe(true);
174
- });
175
-
176
- it('should objectsHaveSameKeys return false', () => {
177
- const obj1 = { test: 1, sub: { test2: 2 } };
178
- const obj2 = { test: 1, sub2: { test2: 2 } };
179
- expect(objectsHaveSameKeys(obj1, obj2)).toBe(false);
180
- });
181
- });
@@ -1,85 +0,0 @@
1
- import { WithPluginSystem, Plugin, PluginMethods } from '../plugin.helper.js';
2
- import { describe, it, expect, beforeEach, jest } from '@jest/globals';
3
-
4
- describe('WithPluginSystem', () => {
5
- class TestMethods implements PluginMethods {
6
- [key: string]: { (...args: any[]): void } | undefined;
7
- testMethod1?: (...args: any[]) => void;
8
- testMethod2?: (...args: any[]) => void;
9
- }
10
-
11
- class TestBase {}
12
-
13
- it('should create a PluginSystemWrapper with default base class', () => {
14
- const PluginSystemWrapper = WithPluginSystem<TestMethods>();
15
- expect(new PluginSystemWrapper()).toBeInstanceOf(PluginSystemWrapper);
16
- });
17
-
18
- it('should create a PluginSystemWrapper with a custom base class', () => {
19
- const PluginSystemWrapper = WithPluginSystem<TestMethods>(TestBase);
20
- expect(new PluginSystemWrapper()).toBeInstanceOf(TestBase);
21
- });
22
-
23
- describe('PluginSystemWrapper', () => {
24
- let PluginSystemWrapper: any;
25
- let pluginSystem: any;
26
-
27
- beforeEach(() => {
28
- PluginSystemWrapper = WithPluginSystem<TestMethods>();
29
- pluginSystem = new PluginSystemWrapper();
30
- });
31
-
32
- describe('registerPlugin', () => {
33
- it('should register a plugin', () => {
34
- const plugin: Plugin<TestMethods> = { pluginMethods: {} };
35
- pluginSystem.registerPlugin(plugin);
36
- expect(pluginSystem.plugins).toContain(plugin);
37
- });
38
- });
39
-
40
- describe('unregisterPlugin', () => {
41
- it('should unregister a plugin', () => {
42
- const plugin: Plugin<TestMethods> = { pluginMethods: {} };
43
- pluginSystem.registerPlugin(plugin);
44
- pluginSystem.unregisterPlugin(plugin);
45
- expect(pluginSystem.plugins).not.toContain(plugin);
46
- });
47
- });
48
-
49
- describe('invokePlugins', () => {
50
- it('should invoke the correct method on all plugins', () => {
51
- const plugin1: Plugin<TestMethods> = {
52
- pluginMethods: {
53
- testMethod1: jest.fn(),
54
- },
55
- };
56
- const plugin2: Plugin<TestMethods> = {
57
- pluginMethods: {
58
- testMethod1: jest.fn(),
59
- },
60
- };
61
- pluginSystem.registerPlugin(plugin1);
62
- pluginSystem.registerPlugin(plugin2);
63
- pluginSystem.invokePlugins('testMethod1', 'arg1', 'arg2');
64
-
65
- expect(plugin1.pluginMethods.testMethod1).toHaveBeenCalledWith(
66
- 'arg1',
67
- 'arg2',
68
- );
69
- expect(plugin2.pluginMethods.testMethod1).toHaveBeenCalledWith(
70
- 'arg1',
71
- 'arg2',
72
- );
73
- });
74
-
75
- it('should not throw if a method is undefined', () => {
76
- const plugin: Plugin<TestMethods> = { pluginMethods: {} };
77
- pluginSystem.registerPlugin(plugin);
78
-
79
- expect(() =>
80
- pluginSystem.invokePlugins('testMethod1', 'arg1'),
81
- ).not.toThrow();
82
- });
83
- });
84
- });
85
- });