-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate-json.test.js
84 lines (68 loc) · 3.15 KB
/
translate-json.test.js
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
const translateJson = require("./translate-json");
const { Translate } = require("@google-cloud/translate").v2;
const fs = require("fs");
jest.mock("@google-cloud/translate");
jest.mock("fs");
describe("translateJson", () => {
beforeEach(() => {
jest.clearAllMocks();
});
test("translates JSON file correctly", async () => {
const mockTranslate = {
translate: jest.fn().mockResolvedValue([["Bonjour", "Le monde"]]),
};
Translate.mockImplementation(() => mockTranslate);
fs.readFileSync.mockReturnValue(JSON.stringify({ hello: "Hello", world: "World" }));
await translateJson("en", "fr", "fake-api-key");
expect(mockTranslate.translate).toHaveBeenCalledWith(["Hello", "World"], expect.any(Object));
expect(fs.writeFileSync).toHaveBeenCalledWith("fr.json", expect.any(String));
});
test("handles nested JSON structures", async () => {
const mockTranslate = {
translate: jest.fn().mockResolvedValue([["Bonjour", "Le monde", "Imbriqué"]]),
};
Translate.mockImplementation(() => mockTranslate);
fs.readFileSync.mockReturnValue(
JSON.stringify({
greeting: "Hello",
messages: { welcome: "World", nested: "Nested" },
})
);
await translateJson("en", "fr", "fake-api-key");
expect(mockTranslate.translate).toHaveBeenCalledWith(["Hello", "World", "Nested"], expect.any(Object));
expect(fs.writeFileSync).toHaveBeenCalledWith("fr.json", expect.any(String));
});
test("translates to multiple languages", async () => {
const mockTranslate = {
translate: jest
.fn()
.mockResolvedValueOnce([["Hola", "Mundo"]])
.mockResolvedValueOnce([["Bonjour", "Le monde"]]),
};
Translate.mockImplementation(() => mockTranslate);
fs.readFileSync.mockReturnValue(JSON.stringify({ hello: "Hello", world: "World" }));
await translateJson("en", "es,fr", "fake-api-key");
expect(mockTranslate.translate).toHaveBeenCalledTimes(2);
expect(fs.writeFileSync).toHaveBeenCalledWith("es.json", expect.any(String));
expect(fs.writeFileSync).toHaveBeenCalledWith("fr.json", expect.any(String));
});
test("removes diacritics for specific languages", async () => {
const mockTranslate = {
translate: jest.fn().mockResolvedValue([["مَرحَبًا", "العَالَم"]]),
};
Translate.mockImplementation(() => mockTranslate);
fs.readFileSync.mockReturnValue(JSON.stringify({ hello: "Hello", world: "World" }));
await translateJson("en", "ar", "fake-api-key");
expect(fs.writeFileSync).toHaveBeenCalledWith("ar.json", expect.stringContaining("مرحبا"));
expect(fs.writeFileSync).toHaveBeenCalledWith("ar.json", expect.stringContaining("العالم"));
});
test("handles translation errors", async () => {
const mockTranslate = {
translate: jest.fn().mockRejectedValue(new Error("Translation API error")),
};
Translate.mockImplementation(() => mockTranslate);
fs.readFileSync.mockReturnValue(JSON.stringify({ hello: "Hello", world: "World" }));
await expect(translateJson("en", "fr", "fake-api-key")).rejects.toThrow("Translation API error");
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
});