-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
156 lines (129 loc) · 4.71 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import http from 'node:http';
import createTacoTranslateClient, {
type ClientGetLocalizationsParameters,
type ClientGetTranslationsParameters,
type CreateTacoTranslateClientParameters,
type Translations,
createEntry,
getEntryFromTranslations,
localeCodes,
patchDefaultString,
template,
translateEntries,
} from '.';
const translations: Translations = {
// eslint-disable-next-line @typescript-eslint/naming-convention
'some_text_id': 'Hello, there!',
'Hello, world!': 'Hallo, verden!',
'Hello, {{name}}!': 'Hallo, {{name}}!',
};
async function getTranslations(): Promise<Translations> {
return translations;
}
async function getLocalizations() {
return {};
}
const createClient = ({
projectLocale,
isEnabled = true,
}: Pick<
CreateTacoTranslateClientParameters,
'projectLocale' | 'isEnabled'
>) => ({
getTranslations: async ({locale}: ClientGetTranslationsParameters) =>
isEnabled && locale !== projectLocale ? getTranslations() : {},
getLocalizations: async ({locale}: ClientGetLocalizationsParameters) =>
isEnabled && locale !== projectLocale ? getLocalizations() : {},
getLocales: async () => localeCodes,
});
test('template strings should be replaced', () => {
expect(template('Hello, {{name}}!', {name: 'Pedro'})).toBe('Hello, Pedro!');
});
test('default strings should be patched', () => {
expect(patchDefaultString('Hello, [[[Pedro]]]!')).toBe('Hello, Pedro!');
});
test('get entry from translation by string', () => {
const translations = {'Hello, [[[Pablo]]]!': 'Hallo, Pablo!'};
const entry = createEntry({string: 'Hello, [[[Pablo]]]!'});
expect(getEntryFromTranslations(entry, translations)).toBe('Hallo, Pablo!');
});
test('get entry from missing translation', () => {
const translations = {};
const entry = createEntry({string: 'Hello, [[[Pablo]]]!'});
expect(getEntryFromTranslations(entry, translations)).toBe('Hello, Pablo!');
});
test('get translations from translateEntries', async () => {
const translationKey = 'Hello, world!';
const client = createClient({projectLocale: 'en'});
const text = createEntry({string: translations[translationKey]});
const t = await translateEntries(client, {origin: '*', locale: 'es'}, [text]);
expect(t(text)).toEqual(translations[translationKey]);
});
test('get missing translation from translateEntries', async () => {
const client = createClient({projectLocale: 'en'});
const text = createEntry({string: 'missing'});
const t = await translateEntries(client, {origin: '*', locale: 'es'}, [text]);
expect(t(text)).toEqual('missing');
});
test('get translations from translateEntries with variables', async () => {
const translationKey = 'Hello, {{name}}!';
const client = createClient({projectLocale: 'en'});
const text = createEntry({string: translations[translationKey]});
const t = await translateEntries(client, {origin: '*', locale: 'es'}, [text]);
expect(t(text, {name: 'Pablo'})).toEqual('Hallo, Pablo!');
});
test('get missing translation from translateEntries with variables', async () => {
const client = createClient({projectLocale: 'en'});
const text = createEntry({string: 'missing {{name}}'});
const t = await translateEntries(client, {origin: '*', locale: 'es'}, [text]);
expect(t(text, {name: 'Pablo'})).toEqual('missing Pablo');
});
test('equal requests should be batched', async () => {
const requests: Array<string | undefined> = [];
const instance = http.createServer((request, response) => {
requests.push(request.url);
response.writeHead(200, {'content-type': 'application/json'});
response.end(JSON.stringify({success: true, translations}));
});
const port: number = await new Promise((resolve, reject) => {
const server = instance.listen(0, () => {
const address = server.address();
if (address && typeof address === 'object') {
resolve(address.port);
}
reject(new Error('Missing port'));
});
});
const client = createTacoTranslateClient({
apiUrl: `http://localhost:${port}`,
apiKey: 'test',
});
const locale = 'en';
const origin = 'test';
const promises: Array<Promise<Translations>> = [
client.getTranslations({locale: 'no', origin, entries: [{s: 'Hello!'}]}),
client.getTranslations({locale, origin: 'foo', entries: [{s: 'Hello!'}]}),
];
for (let iteration = 0; iteration < 25; iteration += 1) {
promises.push(
client.getTranslations({locale, origin, entries: [{s: 'Hello!'}]})
);
}
await Promise.all(promises);
expect(requests.length).toBe(3);
expect(
requests.includes(
'/api/v1/t?a=test&l=no&o=test&s=%5B%7B%22s%22%3A%22Hello!%22%7D%5D'
)
).toBe(true);
expect(
requests.includes(
'/api/v1/t?a=test&l=en&o=foo&s=%5B%7B%22s%22%3A%22Hello!%22%7D%5D'
)
).toBe(true);
expect(
requests.includes(
'/api/v1/t?a=test&l=en&o=test&s=%5B%7B%22s%22%3A%22Hello!%22%7D%5D'
)
).toBe(true);
});