Skip to content

Commit bd488e1

Browse files
authored
Release/0.1.0 (#5)
* add test case for batch translation
1 parent 5ce938b commit bd488e1

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- '*'
7+
workflow_dispatch:
78

89

910
jobs:

src/services/__tests__/translation.test.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe("TranslationService", () => {
163163
});
164164

165165
await translationService.init();
166-
const result = translationService.translate(testText, "button");
166+
const result = translationService.translate(testText, true);
167167

168168
expect(result).toBe(mockTranslation);
169169
});
@@ -199,11 +199,11 @@ describe("TranslationService", () => {
199199
// Clear the cache to ensure we get the original text initially
200200
translationService["cache"][mockConfig.targetLocale] = {};
201201

202-
const result = translationService.translate(testText, "button");
202+
const result = translationService.translate(testText, true);
203203
expect(result).toBe(testText); // Initially returns original text
204204

205205
// Manually add the text to pending translations as the service would
206-
translationService["pendingTranslations"].set(testText, "button");
206+
translationService["pendingTranslations"].set(testText, true);
207207

208208
// Trigger the batch translation
209209
jest.runAllTimers();
@@ -230,4 +230,62 @@ describe("TranslationService", () => {
230230
expect(updatedResult).toBe("Hola");
231231
});
232232
});
233+
234+
describe("translateBatch", () => {
235+
it("should batch translate multiple texts", async () => {
236+
await translationService.init();
237+
238+
const texts = [
239+
{ text: "Hello", persist: true },
240+
{ text: "World", persist: true },
241+
].map((item) => ({
242+
...item,
243+
hashkey: translationService["generateHash"](item.text),
244+
}));
245+
246+
// Mock the batch translation API call
247+
(global.fetch as jest.Mock).mockImplementationOnce(() =>
248+
Promise.resolve({
249+
ok: true,
250+
statusText: "OK",
251+
json: () =>
252+
Promise.resolve({
253+
[texts[0].hashkey]: "Hola",
254+
[texts[1].hashkey]: "Mundo",
255+
}),
256+
})
257+
);
258+
259+
const result = await translationService.translateBatch(texts);
260+
261+
// Verify the API was called with correct parameters
262+
expect(global.fetch).toHaveBeenCalledWith(
263+
`${mockBaseUrl}/v1/translate`,
264+
expect.objectContaining({
265+
method: "POST",
266+
headers: {
267+
"Content-Type": "application/json",
268+
},
269+
body: JSON.stringify({
270+
texts,
271+
sourceLocale: mockConfig.sourceLocale,
272+
targetLocale: mockConfig.targetLocale,
273+
apiKey: mockConfig.apiKey,
274+
}),
275+
})
276+
);
277+
278+
// Verify the translations were returned correctly
279+
expect(result).toEqual({
280+
Hello: "Hola",
281+
World: "Mundo",
282+
});
283+
284+
// Verify the cache was updated with the hashkey-based translations
285+
expect(translationService["cache"][mockConfig.targetLocale]).toEqual({
286+
[texts[0].hashkey]: "Hola",
287+
[texts[1].hashkey]: "Mundo",
288+
});
289+
});
290+
});
233291
});

0 commit comments

Comments
 (0)