Skip to content

Commit f5595e8

Browse files
[test] Refactor translation loading test to use async/await and mock fetch
1 parent caf275e commit f5595e8

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

tests/unit/classes/translator_spec.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,32 @@ describe("Translator", () => {
198198
});
199199
});
200200

201-
it("should not load translations, if module fallback exists", () => {
202-
return new Promise((done) => {
203-
const dom = new JSDOM(`<script>var Log = {log: () => {}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
204-
dom.window.onload = async () => {
205-
const { Translator, XMLHttpRequest } = dom.window;
206-
const file = "translation_test.json";
201+
it("should not load translations, if module fallback exists", async () => {
202+
const dom = new JSDOM(`<script>var Log = {log: () => {}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
203+
await new Promise((resolve) => dom.window.onload = resolve);
207204

208-
XMLHttpRequest.prototype.send = () => {
209-
throw new Error("Shouldn't load files");
210-
};
205+
const { Translator } = dom.window;
206+
const file = "translation_test.json";
211207

212-
Translator.translationsFallback[mmm.name] = {
213-
Hello: "Hallo"
214-
};
208+
// Mock fetch to ensure no actual requests are made
209+
const originalFetch = dom.window.fetch;
210+
dom.window.fetch = jest.fn(() => {
211+
throw new Error("Shouldn't load files");
212+
});
215213

216-
await Translator.load(mmm, file, false);
217-
expect(Translator.translations[mmm.name]).toBeUndefined();
218-
expect(Translator.translationsFallback[mmm.name]).toEqual({
219-
Hello: "Hallo"
220-
});
221-
done();
222-
};
214+
// Restore the original fetch after the test
215+
afterAll(() => {
216+
dom.window.fetch = originalFetch;
217+
});
218+
219+
Translator.translationsFallback[mmm.name] = {
220+
Hello: "Hallo"
221+
};
222+
223+
await Translator.load(mmm, file, false);
224+
expect(Translator.translations[mmm.name]).toBeUndefined();
225+
expect(Translator.translationsFallback[mmm.name]).toEqual({
226+
Hello: "Hallo"
223227
});
224228
});
225229
});

0 commit comments

Comments
 (0)