Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mololab
GitHub Repository: mololab/json-translator
Path: blob/master/src/test/json-file.spec.ts
235 views
1
import 'openai/shims/node';
2
import { fileTranslator } from '../core/json_file';
3
import * as appConsole from '../utils/console';
4
import * as jsonObject from '../core/json_object';
5
import * as core from '../core/core';
6
import { GoogleTranslateLanguages } from '../modules/languages';
7
import { TranslationConfig, TranslationModules } from '../modules/modules';
8
import { default_concurrency_limit, default_fallback } from '../utils/micro';
9
import { translatedObject } from '..';
10
11
declare global {
12
var totalTranslation: number;
13
var totalTranslated: number;
14
var skipInCache: number;
15
var proxyList: string[];
16
var proxyIndex: number;
17
}
18
19
describe(`JSON FILE`, () => {
20
it(`test environment is ready`, () => {
21
expect(true).toBeTruthy();
22
});
23
24
it('should get `no file` error on undefined file path', async () => {
25
// arrange
26
const mock_objectPath = 'mock_objectPath';
27
const mock_from = GoogleTranslateLanguages.English;
28
const mock_to = GoogleTranslateLanguages.Dutch;
29
const mock_newFileName = '';
30
31
const mock_json_file = undefined;
32
33
jest.spyOn(core, 'getFile').mockResolvedValue(mock_json_file);
34
jest.spyOn(appConsole, 'error').mockReturnValue();
35
36
let config: TranslationConfig = {
37
moduleKey: 'google',
38
TranslationModule: TranslationModules['google'],
39
concurrencyLimit: default_concurrency_limit,
40
fallback: default_fallback,
41
cacheEnabled: false,
42
};
43
44
// act
45
await fileTranslator(
46
config,
47
mock_objectPath,
48
mock_from,
49
[mock_to],
50
mock_newFileName
51
);
52
53
// assert
54
expect(appConsole.error).toHaveBeenCalled();
55
expect(appConsole.error).toBeCalledWith(
56
appConsole.messages.file.no_file_in_path
57
);
58
});
59
60
it('should get file & translate it to one language & saves it', async () => {
61
// arrange
62
const mock_objectPath = 'mock_objectPath';
63
const mock_from = GoogleTranslateLanguages.English;
64
const mock_to = GoogleTranslateLanguages.German;
65
const mock_newFileName = '';
66
const mock_json_object = JSON.stringify({
67
login: {
68
title: 'Login',
69
email: 'Please, enter your email',
70
failure: 'Failed',
71
},
72
});
73
const mock_translated_json_object: translatedObject[] = [
74
{
75
data: {
76
login: {
77
title: 'Anmeldung',
78
email: 'Bitte geben Sie ihre E-Mail-Adresse ein',
79
failure: 'Fehlgeschlagen',
80
},
81
},
82
},
83
];
84
const mock_root_folder = 'mock_root_folder';
85
86
jest.spyOn(core, 'getFile').mockResolvedValue(mock_json_object);
87
jest
88
.spyOn(jsonObject, 'objectTranslator')
89
.mockResolvedValue(mock_translated_json_object);
90
jest.spyOn(core, 'getRootFolder').mockReturnValue(mock_root_folder);
91
jest.spyOn(core, 'saveFilePublic').mockResolvedValue();
92
jest.spyOn(appConsole, 'success').mockReturnValue();
93
94
let config: TranslationConfig = {
95
moduleKey: 'google',
96
TranslationModule: TranslationModules['google'],
97
concurrencyLimit: default_concurrency_limit,
98
fallback: default_fallback,
99
cacheEnabled: false,
100
};
101
102
// act
103
await fileTranslator(
104
config,
105
mock_objectPath,
106
mock_from,
107
[mock_to],
108
mock_newFileName
109
);
110
111
// assert
112
expect(core.saveFilePublic).toBeCalledTimes(1);
113
});
114
115
it('should get file & translate it to multiple languages & saves it', async () => {
116
// arrange
117
const mock_objectPath = 'mock_objectPath';
118
const mock_from = GoogleTranslateLanguages.English;
119
const mock_to = [
120
GoogleTranslateLanguages.German,
121
GoogleTranslateLanguages.French,
122
];
123
const mock_newFileName = '';
124
const mock_json_object = JSON.stringify({
125
login: {
126
title: 'Login',
127
email: 'Please, enter your email',
128
failure: 'Failed',
129
},
130
});
131
const mock_translated_json_object = [
132
{
133
data: {
134
login: {
135
title: 'Anmeldung',
136
email: 'Bitte geben Sie ihre E-Mail-Adresse ein',
137
failure: 'Fehlgeschlagen',
138
},
139
},
140
},
141
{
142
data: {
143
login: {
144
title: 'Connexion',
145
email: "S'il vous plaît, entrez votre e-mail",
146
failure: 'Manquée',
147
},
148
},
149
},
150
];
151
const mock_root_folder = 'mock_root_folder';
152
153
jest.spyOn(core, 'getFile').mockResolvedValue(mock_json_object);
154
jest
155
.spyOn(jsonObject, 'objectTranslator')
156
.mockResolvedValue(mock_translated_json_object);
157
jest.spyOn(core, 'getRootFolder').mockReturnValue(mock_root_folder);
158
jest.spyOn(core, 'saveFilePublic').mockResolvedValue();
159
jest.spyOn(appConsole, 'success').mockReturnValue();
160
161
let config: TranslationConfig = {
162
moduleKey: 'google',
163
TranslationModule: TranslationModules['google'],
164
concurrencyLimit: default_concurrency_limit,
165
fallback: default_fallback,
166
cacheEnabled: false,
167
};
168
169
// act
170
await fileTranslator(
171
config,
172
mock_objectPath,
173
mock_from,
174
mock_to,
175
mock_newFileName
176
);
177
178
// assert
179
expect(core.saveFilePublic).toBeCalled();
180
expect(appConsole.success).toBeCalled();
181
});
182
});
183
184