@tolgee/core 4.7.0 → 4.7.2

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 (97) hide show
  1. package/dist/tolgee.cjs.js +2 -4
  2. package/dist/tolgee.cjs.js.map +1 -1
  3. package/dist/tolgee.cjs.min.js +1 -1
  4. package/dist/tolgee.cjs.min.js.map +1 -1
  5. package/dist/{tolgee.esm.js → tolgee.esm.min.mjs} +2 -2
  6. package/dist/tolgee.esm.min.mjs.map +1 -0
  7. package/dist/tolgee.esm.mjs +5690 -0
  8. package/dist/tolgee.esm.mjs.map +1 -0
  9. package/dist/tolgee.umd.js +2 -4
  10. package/dist/tolgee.umd.js.map +1 -1
  11. package/dist/tolgee.umd.min.js +1 -1
  12. package/dist/tolgee.umd.min.js.map +1 -1
  13. package/package.json +10 -9
  14. package/src/Constants/Global.ts +9 -0
  15. package/src/Constants/ModifierKey.ts +6 -0
  16. package/src/Errors/ApiHttpError.ts +8 -0
  17. package/src/Observer.test.ts +119 -0
  18. package/src/Observer.ts +68 -0
  19. package/src/Properties.test.ts +150 -0
  20. package/src/Properties.ts +112 -0
  21. package/src/Tolgee.test.ts +473 -0
  22. package/src/Tolgee.ts +335 -0
  23. package/src/TolgeeConfig.test.ts +21 -0
  24. package/src/TolgeeConfig.ts +134 -0
  25. package/src/__integration/FormatterIcu.test.ts +80 -0
  26. package/src/__integration/FormatterMissing.ts +54 -0
  27. package/src/__integration/Tolgee.test.ts +90 -0
  28. package/src/__integration/TolgeeInvisible.test.ts +145 -0
  29. package/src/__integration/mockTranslations.ts +6 -0
  30. package/src/__integration/testConfig.ts +16 -0
  31. package/src/__testFixtures/classMock.ts +11 -0
  32. package/src/__testFixtures/createElement.ts +43 -0
  33. package/src/__testFixtures/createTestDom.ts +25 -0
  34. package/src/__testFixtures/mocked.ts +25 -0
  35. package/src/__testFixtures/setupAfterEnv.ts +34 -0
  36. package/src/helpers/NodeHelper.ts +90 -0
  37. package/src/helpers/TextHelper.test.ts +62 -0
  38. package/src/helpers/TextHelper.ts +58 -0
  39. package/src/helpers/commonTypes.ts +8 -0
  40. package/src/helpers/encoderPolyfill.ts +96 -0
  41. package/src/helpers/secret.test.ts +61 -0
  42. package/src/helpers/secret.ts +68 -0
  43. package/src/helpers/sleep.ts +2 -0
  44. package/src/highlighter/HighlightFunctionsInitializer.test.ts +40 -0
  45. package/src/highlighter/HighlightFunctionsInitializer.ts +61 -0
  46. package/src/highlighter/MouseEventHandler.test.ts +151 -0
  47. package/src/highlighter/MouseEventHandler.ts +191 -0
  48. package/src/highlighter/TranslationHighlighter.test.ts +177 -0
  49. package/src/highlighter/TranslationHighlighter.ts +113 -0
  50. package/src/index.ts +10 -0
  51. package/src/internal.ts +2 -0
  52. package/src/modules/IcuFormatter.ts +17 -0
  53. package/src/modules/index.ts +1 -0
  54. package/src/services/ApiHttpService.ts +85 -0
  55. package/src/services/CoreService.test.ts +142 -0
  56. package/src/services/CoreService.ts +76 -0
  57. package/src/services/DependencyService.test.ts +51 -0
  58. package/src/services/DependencyService.ts +116 -0
  59. package/src/services/ElementRegistrar.test.ts +131 -0
  60. package/src/services/ElementRegistrar.ts +108 -0
  61. package/src/services/EventEmitter.ts +52 -0
  62. package/src/services/EventService.ts +14 -0
  63. package/src/services/ModuleService.ts +14 -0
  64. package/src/services/ScreenshotService.ts +31 -0
  65. package/src/services/Subscription.ts +7 -0
  66. package/src/services/TextService.test.ts +88 -0
  67. package/src/services/TextService.ts +82 -0
  68. package/src/services/TranslationService.test.ts +358 -0
  69. package/src/services/TranslationService.ts +417 -0
  70. package/src/services/__mocks__/CoreService.ts +17 -0
  71. package/src/toolsManager/Messages.test.ts +79 -0
  72. package/src/toolsManager/Messages.ts +60 -0
  73. package/src/toolsManager/PluginManager.test.ts +108 -0
  74. package/src/toolsManager/PluginManager.ts +129 -0
  75. package/src/types/DTOs.ts +25 -0
  76. package/src/types/apiSchema.generated.ts +6208 -0
  77. package/src/types.ts +146 -0
  78. package/src/wrappers/AbstractWrapper.ts +14 -0
  79. package/src/wrappers/NodeHandler.ts +143 -0
  80. package/src/wrappers/WrappedHandler.ts +28 -0
  81. package/src/wrappers/invisible/AttributeHandler.ts +23 -0
  82. package/src/wrappers/invisible/Coder.ts +65 -0
  83. package/src/wrappers/invisible/ContentHandler.ts +15 -0
  84. package/src/wrappers/invisible/CoreHandler.ts +17 -0
  85. package/src/wrappers/invisible/InvisibleWrapper.ts +59 -0
  86. package/src/wrappers/invisible/ValueMemory.test.ts +25 -0
  87. package/src/wrappers/invisible/ValueMemory.ts +16 -0
  88. package/src/wrappers/text/AttributeHandler.test.ts +117 -0
  89. package/src/wrappers/text/AttributeHandler.ts +25 -0
  90. package/src/wrappers/text/Coder.test.ts +298 -0
  91. package/src/wrappers/text/Coder.ts +202 -0
  92. package/src/wrappers/text/ContentHandler.test.ts +185 -0
  93. package/src/wrappers/text/ContentHandler.ts +21 -0
  94. package/src/wrappers/text/CoreHandler.test.ts +106 -0
  95. package/src/wrappers/text/CoreHandler.ts +45 -0
  96. package/src/wrappers/text/TextWrapper.ts +69 -0
  97. package/dist/tolgee.esm.js.map +0 -1
@@ -0,0 +1,358 @@
1
+ jest.dontMock('./TranslationService');
2
+ jest.dontMock('../helpers/TextHelper');
3
+ jest.dontMock('../types/DTOs');
4
+ jest.dontMock('../Errors/ApiHttpError');
5
+ jest.dontMock('./DependencyService');
6
+
7
+ import { TranslationService } from './TranslationService';
8
+ import { getMockedInstance } from '@testFixtures/mocked';
9
+ import { ApiHttpService } from './ApiHttpService';
10
+ import { Properties } from '../Properties';
11
+ import { CoreService } from './CoreService';
12
+ import { TranslationData } from '../types/DTOs';
13
+ import { ApiHttpError } from '../Errors/ApiHttpError';
14
+ import { EventService } from './EventService';
15
+ import { DependencyService } from './DependencyService';
16
+
17
+ const mockedTranslations = {
18
+ en: {
19
+ key: 'translated',
20
+ 'translation.with.dots': 'Translation with dots',
21
+ structured: {
22
+ subtree: {
23
+ translation: 'Structured subtree translation',
24
+ },
25
+ },
26
+ 'key with: \\\\': {
27
+ t: 'Key with strange escapes',
28
+ },
29
+ just_en: 'Just en.',
30
+ },
31
+ de: {
32
+ key: 'übersetzen',
33
+ just_en: '',
34
+ },
35
+ };
36
+
37
+ global.fetch = jest.fn(async (url: string) => {
38
+ const isEn = url.indexOf('en.json') > -1;
39
+ const isDe = url.indexOf('de.json') > -1;
40
+
41
+ return {
42
+ json: jest.fn(async () =>
43
+ isEn ? mockedTranslations.en : isDe ? mockedTranslations.de : {}
44
+ ),
45
+ };
46
+ }) as any;
47
+
48
+ describe('TranslationService', () => {
49
+ let translationService: TranslationService;
50
+ const languageLoadedEmitMock = jest.fn();
51
+ const translationChangedEmitMock = jest.fn();
52
+
53
+ beforeEach(async () => {
54
+ const dependencyService = new DependencyService();
55
+ dependencyService.init({});
56
+ translationService = dependencyService.translationService;
57
+ (getMockedInstance(EventService) as any).LANGUAGE_LOADED = {
58
+ emit: languageLoadedEmitMock,
59
+ };
60
+ (getMockedInstance(EventService) as any).TRANSLATION_CHANGED = {
61
+ emit: translationChangedEmitMock,
62
+ };
63
+ getMockedInstance(ApiHttpService).fetchJson = jest.fn(
64
+ async () => mockedTranslations
65
+ );
66
+ getMockedInstance(Properties).currentLanguage = 'en';
67
+ });
68
+
69
+ afterEach(async () => {
70
+ jest.clearAllMocks();
71
+ });
72
+
73
+ describe('(translation loading and retrieval)', () => {
74
+ test('will load translations in production mode', async () => {
75
+ await translationService.loadTranslations('en');
76
+ expect(translationService.getFromCacheOrFallback('key', 'en')).toEqual(
77
+ 'translated'
78
+ );
79
+ });
80
+
81
+ test('will load translations in development mode', async () => {
82
+ getMockedInstance(Properties).mode = 'development';
83
+ await translationService.loadTranslations('en');
84
+ expect(translationService.getFromCacheOrFallback('key', 'en')).toEqual(
85
+ 'translated'
86
+ );
87
+ });
88
+
89
+ test('will try to load the languages only single time', async () => {
90
+ await translationService.loadTranslations('en');
91
+ await translationService.loadTranslations('en');
92
+ expect(fetch).toBeCalledTimes(1);
93
+ });
94
+
95
+ test('will get proper translation', async () => {
96
+ expect(await translationService.getTranslation('key', 'en')).toEqual(
97
+ 'translated'
98
+ );
99
+ });
100
+
101
+ test('will get proper translation containing .', async () => {
102
+ expect(
103
+ await translationService.getTranslation('translation.with.dots')
104
+ ).toEqual('Translation with dots');
105
+ });
106
+
107
+ test('will get proper structured subtree translation', async () => {
108
+ expect(
109
+ await translationService.getTranslation(
110
+ 'structured.subtree.translation'
111
+ )
112
+ ).toEqual('Structured subtree translation');
113
+ });
114
+
115
+ test('will get proper translation on strange key', async () => {
116
+ expect(
117
+ await translationService.getTranslation('key with: \\\\.t')
118
+ ).toEqual('Key with strange escapes');
119
+ });
120
+
121
+ test('will return fallback lang translation', async () => {
122
+ expect(await translationService.getTranslation('key', 'cz')).toEqual(
123
+ 'translated'
124
+ );
125
+ });
126
+
127
+ test('will get translations of key', async () => {
128
+ getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => ({
129
+ _embedded: {
130
+ keys: [
131
+ {
132
+ translations: {
133
+ en: { text: 'translated' },
134
+ de: { text: 'übersetzen' },
135
+ },
136
+ },
137
+ ],
138
+ },
139
+ }));
140
+ const [translationData] = await translationService.getTranslationsOfKey(
141
+ 'key',
142
+ new Set<string>(['en', 'de'])
143
+ );
144
+ expect(translationData).toEqual({
145
+ translations: {
146
+ en: { text: 'translated' },
147
+ de: { text: 'übersetzen' },
148
+ },
149
+ });
150
+ });
151
+
152
+ test('will reset preferred languages when language is not found', async () => {
153
+ global['apierror'] = ApiHttpError;
154
+ getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => {
155
+ throw new ApiHttpError(
156
+ { status: 404 } as Response,
157
+ 'language_not_found'
158
+ );
159
+ });
160
+
161
+ delete global.location;
162
+ global.location = { reload: jest.fn() } as any;
163
+ global.console.error = jest.fn();
164
+ getMockedInstance(CoreService).getLanguages = jest.fn(
165
+ async () => new Set(['dummyLang'])
166
+ );
167
+ await translationService.getTranslationsOfKey('aaa', new Set<string>());
168
+ expect(getMockedInstance(Properties).preferredLanguages).toEqual(
169
+ new Set(['dummyLang'])
170
+ );
171
+ // eslint-disable-next-line no-console
172
+ expect(console.error).toBeCalledTimes(1);
173
+ expect(location.reload).toBeCalledTimes(1);
174
+ });
175
+
176
+ test('will emit on translations load', async () => {
177
+ await translationService.loadTranslations('en');
178
+ await translationService.loadTranslations('de');
179
+ await translationService.loadTranslations('en');
180
+
181
+ expect(languageLoadedEmitMock).toBeCalledTimes(2);
182
+ expect(languageLoadedEmitMock).toHaveBeenNthCalledWith(1, 'en');
183
+ expect(languageLoadedEmitMock).toHaveBeenNthCalledWith(2, 'de');
184
+ });
185
+
186
+ test("won't throw exception when there is null in translation data", async () => {
187
+ getMockedInstance(Properties).mode = 'development';
188
+ getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => ({
189
+ en: {
190
+ hello: null,
191
+ yep: 'Load was successfull.',
192
+ },
193
+ }));
194
+ window.console.error = jest.fn();
195
+ await translationService.loadTranslations('en');
196
+ expect(window.console.error).toHaveBeenCalledTimes(0);
197
+ expect(translationService.getFromCacheOrFallback('yep')).toEqual(
198
+ 'Load was successfull.'
199
+ );
200
+ });
201
+
202
+ describe('set translation', () => {
203
+ const dummyTranslationData = {
204
+ key: 'test key',
205
+ translations: { en: 'translation' },
206
+ } as TranslationData;
207
+
208
+ beforeEach(async () => {
209
+ getMockedInstance(ApiHttpService).postJson = jest.fn(async () => {
210
+ return {
211
+ keyId: undefined,
212
+ keyName: 'test key',
213
+ translations: {
214
+ en: {
215
+ text: 'translation',
216
+ },
217
+ },
218
+ };
219
+ });
220
+ await translationService.setTranslations(dummyTranslationData);
221
+ });
222
+
223
+ test('will call the api http service', async () => {
224
+ expect(getMockedInstance(ApiHttpService).postJson).toBeCalledTimes(1);
225
+ expect(getMockedInstance(ApiHttpService).postJson).toBeCalledWith(
226
+ 'v2/projects/translations',
227
+ dummyTranslationData
228
+ );
229
+ });
230
+
231
+ test('will check the scopes', async () => {
232
+ expect(getMockedInstance(CoreService).checkScope).toBeCalledTimes(1);
233
+ });
234
+
235
+ test('emits translation changed event', async () => {
236
+ expect(translationChangedEmitMock).toBeCalledTimes(1);
237
+ expect(translationChangedEmitMock).toBeCalledWith(dummyTranslationData);
238
+ });
239
+
240
+ test('will update the data', async () => {
241
+ await translationService.loadTranslations('en');
242
+ await translationService.setTranslations(dummyTranslationData);
243
+ expect(
244
+ await translationService.getTranslation(
245
+ dummyTranslationData.key,
246
+ 'en'
247
+ )
248
+ ).toEqual(dummyTranslationData.translations.en);
249
+ });
250
+ });
251
+
252
+ test('will update the data when the key contains .', async () => {
253
+ const dummyTranslationData = {
254
+ key: 'test.key',
255
+ translations: { en: 'translation' },
256
+ } as TranslationData;
257
+ getMockedInstance(ApiHttpService).postJson = jest.fn(async () => {
258
+ return {
259
+ keyId: undefined,
260
+ keyName: 'test.key',
261
+ translations: {
262
+ en: {
263
+ text: 'translation',
264
+ },
265
+ },
266
+ };
267
+ });
268
+ await translationService.loadTranslations('en');
269
+ await translationService.setTranslations(dummyTranslationData);
270
+ expect(
271
+ await translationService.getTranslation(dummyTranslationData.key, 'en')
272
+ ).toEqual(dummyTranslationData.translations.en);
273
+ });
274
+
275
+ test('will update the data when the key contains \\.', async () => {
276
+ const dummyTranslationData = {
277
+ key: 'test\\.key',
278
+ translations: { en: 'translation' },
279
+ } as TranslationData;
280
+ getMockedInstance(ApiHttpService).postJson = jest.fn(async () => {
281
+ return {
282
+ keyId: undefined,
283
+ keyName: 'test\\.key',
284
+ translations: {
285
+ en: {
286
+ text: 'translation',
287
+ },
288
+ },
289
+ };
290
+ });
291
+ await translationService.loadTranslations('en');
292
+ await translationService.setTranslations(dummyTranslationData);
293
+ expect(
294
+ await translationService.getTranslation(dummyTranslationData.key, 'en')
295
+ ).toEqual(dummyTranslationData.translations.en);
296
+ });
297
+
298
+ test('will call load of fallback language on missing translation', async () => {
299
+ translationService.loadTranslations = jest.fn();
300
+ getMockedInstance(Properties).config.fallbackLanguage = 'en';
301
+ expect(await translationService.getTranslation('aaa', 'cs'));
302
+ expect(translationService.loadTranslations).toBeCalledTimes(2);
303
+ expect(translationService.loadTranslations).toBeCalledWith('cs');
304
+ expect(translationService.loadTranslations).toBeCalledWith('en');
305
+ });
306
+
307
+ test('will use fallback language on missing translation', async () => {
308
+ getMockedInstance(Properties).config.fallbackLanguage = 'en';
309
+ expect(
310
+ await translationService.getTranslation('translation.with.dots', 'de')
311
+ ).toEqual('Translation with dots');
312
+ });
313
+
314
+ test('getTranslation will return fallback when message is empty string', async () => {
315
+ getMockedInstance(Properties).config.fallbackLanguage = 'en';
316
+ expect(await translationService.getTranslation('just_en', 'de')).toEqual(
317
+ 'Just en.'
318
+ );
319
+ });
320
+
321
+ test('getFromCacheOrCallback will return fallback when message is empty string', async () => {
322
+ getMockedInstance(Properties).config.fallbackLanguage = 'en';
323
+ (translationService as any).setLanguageData('en', mockedTranslations.en);
324
+ (translationService as any).setLanguageData('de', mockedTranslations.de);
325
+ expect(
326
+ await translationService.getFromCacheOrFallback('just_en', 'de')
327
+ ).toEqual('Just en.');
328
+ });
329
+
330
+ test('uses provided static data', async () => {
331
+ getMockedInstance(Properties).config.staticData = {
332
+ en: { test: 'Test test' },
333
+ };
334
+ translationService.initStatic();
335
+ expect(await translationService.getTranslation('test', 'en')).toEqual(
336
+ 'Test test'
337
+ );
338
+ });
339
+
340
+ test('uses provided promise data', async () => {
341
+ getMockedInstance(Properties).config.staticData = {
342
+ en: () => new Promise((resolve) => resolve({ test: 'Test test' })),
343
+ };
344
+ expect(await translationService.getTranslation('test', 'en')).toEqual(
345
+ 'Test test'
346
+ );
347
+ });
348
+
349
+ test('uses provided data without init static (when mode is changed dynamically)', async () => {
350
+ getMockedInstance(Properties).config.staticData = {
351
+ en: { test: 'Test test' },
352
+ };
353
+ expect(await translationService.getTranslation('test', 'en')).toEqual(
354
+ 'Test test'
355
+ );
356
+ });
357
+ });
358
+ });