Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mololab
GitHub Repository: mololab/json-translator
Path: blob/master/src/test/json-object.spec.ts
235 views
1
import 'openai/shims/node';
2
import * as translator from '../core/translator';
3
import { deepDiver, objectTranslator } from '../core/json_object';
4
import { GoogleTranslateLanguages } from '../modules/languages';
5
import { TranslationConfig, TranslationModules } from '../modules/modules';
6
import { default_concurrency_limit, default_fallback } from '../utils/micro';
7
8
declare global {
9
var totalTranslation: number;
10
var totalTranslated: number;
11
var skipInCache: number;
12
var proxyList: string[];
13
var proxyIndex: number;
14
}
15
16
describe(`JSON OBJECT`, () => {
17
test('sanity check for test environment', () => {
18
expect(true).toBeTruthy();
19
});
20
21
const test_object = {
22
a: 'a',
23
b: 'b',
24
c: {
25
d: 'd',
26
e: 'e',
27
},
28
f: [
29
{
30
g: 'g',
31
h: 'h',
32
},
33
{
34
i: 'i',
35
j: {
36
k: 'k',
37
l: 'l',
38
},
39
m: {
40
n: {
41
o: {
42
p: {
43
q: 'q {{name}}',
44
},
45
},
46
},
47
},
48
},
49
],
50
};
51
52
const from = GoogleTranslateLanguages.English;
53
54
const to = GoogleTranslateLanguages.Dutch;
55
56
afterEach(() => {
57
jest.clearAllMocks();
58
});
59
60
const to_multiple = [
61
GoogleTranslateLanguages.Bulgarian,
62
GoogleTranslateLanguages.Catalan,
63
GoogleTranslateLanguages.Turkish,
64
];
65
66
it('should dive every value in the object', async () => {
67
// arrange
68
jest.spyOn(translator, 'plaintranslate').mockResolvedValue('');
69
70
let config: TranslationConfig = {
71
moduleKey: 'google',
72
TranslationModule: TranslationModules['google'],
73
concurrencyLimit: default_concurrency_limit,
74
fallback: default_fallback,
75
cacheEnabled: false,
76
};
77
78
// act
79
await deepDiver(config, test_object, from, to, undefined);
80
81
// assert
82
expect(translator.plaintranslate).toBeCalledTimes(10);
83
});
84
85
it('should translate object into one language', async () => {
86
// arrange
87
jest.spyOn(translator, 'plaintranslate').mockResolvedValue('');
88
89
let config: TranslationConfig = {
90
moduleKey: 'google',
91
TranslationModule: TranslationModules['google'],
92
concurrencyLimit: default_concurrency_limit,
93
fallback: default_fallback,
94
cacheEnabled: false,
95
};
96
97
// act
98
const response = await objectTranslator(config, test_object, from, [to]);
99
100
// assert
101
expect(response).toMatchObject([test_object]);
102
});
103
104
it('should translate object into multiple languages', async () => {
105
// arrange
106
jest.spyOn(translator, 'plaintranslate').mockResolvedValue('');
107
108
let config: TranslationConfig = {
109
moduleKey: 'google',
110
TranslationModule: TranslationModules['google'],
111
concurrencyLimit: default_concurrency_limit,
112
fallback: default_fallback,
113
cacheEnabled: false,
114
};
115
116
// act
117
const response = await objectTranslator(
118
config,
119
test_object,
120
from,
121
to_multiple
122
);
123
124
// assert
125
expect(response.length).toEqual(to_multiple.length);
126
});
127
});
128
129