Skip to content

Commit 6ebd83b

Browse files
Improve test coverage for background.js && languagebox.js (#4629)
* Enhance browser action tests for Firefox and Chrome environments * Improve test coverage for languagebox.js Signed-off-by: asim <aktech701@gmail.com> --------- Signed-off-by: asim <aktech701@gmail.com>
1 parent a6e1e38 commit 6ebd83b

File tree

2 files changed

+299
-19
lines changed

2 files changed

+299
-19
lines changed

js/__tests__/background.test.js

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,161 @@
2020
describe("Browser Action Behavior", () => {
2121
let mockBrowser;
2222
let mockChrome;
23+
let originalWindowOpen;
24+
let windowOpenMock;
2325

2426
beforeEach(() => {
25-
// Mock objects
27+
// Save the original window.open method
28+
originalWindowOpen = window.open;
29+
// Create a mock for window.open
30+
windowOpenMock = jest.fn();
31+
window.open = windowOpenMock;
32+
33+
// Mock objects with proper callback capture
2634
mockBrowser = {
2735
browserAction: {
28-
onClicked: { addListener: jest.fn() },
36+
onClicked: {
37+
addListener: jest.fn((callback) => {
38+
mockBrowser.browserAction.onClicked.callback = callback;
39+
})
40+
}
2941
},
3042
tabs: { create: jest.fn() },
3143
runtime: {
32-
onInstalled: { addListener: jest.fn() },
33-
},
44+
onInstalled: {
45+
addListener: jest.fn((callback) => {
46+
mockBrowser.runtime.onInstalled.callback = callback;
47+
})
48+
}
49+
}
3450
};
3551

3652
mockChrome = {
3753
browserAction: {
38-
onClicked: { addListener: jest.fn() },
54+
onClicked: {
55+
addListener: jest.fn((callback) => {
56+
mockChrome.browserAction.onClicked.callback = callback;
57+
})
58+
}
3959
},
4060
runtime: {
41-
onInstalled: { addListener: jest.fn() },
42-
getURL: jest.fn((path) => `chrome-extension://fake-id/${path}`),
43-
},
44-
tabs: { create: jest.fn() },
61+
onInstalled: {
62+
addListener: jest.fn((callback) => {
63+
mockChrome.runtime.onInstalled.callback = callback;
64+
})
65+
},
66+
getURL: jest.fn((path) => `chrome-extension://fake-id/${path}`)
67+
}
4568
};
4669

4770
global.browser = mockBrowser;
4871
global.chrome = mockChrome;
4972

5073
Object.defineProperty(global.navigator, "userAgent", {
5174
writable: true,
52-
value: "",
75+
value: ""
5376
});
5477
});
5578

5679
afterEach(() => {
5780
jest.clearAllMocks();
5881
delete global.browser;
5982
delete global.chrome;
83+
// Restore original window.open
84+
window.open = originalWindowOpen;
6085
});
6186

6287
it("should set up Firefox-specific listeners when user agent is Firefox", () => {
63-
navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0";
88+
navigator.userAgent =
89+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0";
6490

6591
jest.resetModules(); // Clear the module cache
6692
const { isFirefox, browserAction } = require("../background.js");
6793

6894
expect(isFirefox).toBe(true);
69-
expect(browserAction.onClicked.addListener).toHaveBeenCalledTimes(1);
95+
expect(mockBrowser.browserAction.onClicked.addListener).toHaveBeenCalledTimes(1);
7096
expect(mockBrowser.runtime.onInstalled.addListener).toHaveBeenCalledTimes(1);
7197
});
7298

7399
it("should set up Chrome-specific listeners when user agent is not Firefox", () => {
74-
navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
100+
navigator.userAgent =
101+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
75102

76103
jest.resetModules(); // Clear the module cache
77104
const { isFirefox, browserAction } = require("../background.js");
78105

79106
expect(isFirefox).toBe(false);
80-
expect(browserAction.onClicked.addListener).toHaveBeenCalledTimes(1);
107+
expect(mockChrome.browserAction.onClicked.addListener).toHaveBeenCalledTimes(1);
81108
expect(mockChrome.runtime.onInstalled.addListener).toHaveBeenCalledTimes(1);
82109
});
110+
111+
it("should create a new tab with index.html when browserAction is clicked in Firefox", () => {
112+
navigator.userAgent =
113+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0";
114+
115+
jest.resetModules(); // Clear the module cache
116+
require("../background.js");
117+
// Simulate clicking the browser action button
118+
mockBrowser.browserAction.onClicked.callback({ id: "tab-123" });
119+
120+
expect(mockBrowser.tabs.create).toHaveBeenCalledWith({ url: "index.html" });
121+
});
122+
123+
it("should create a new tab with index.html when extension is installed in Firefox", () => {
124+
navigator.userAgent =
125+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0";
126+
127+
jest.resetModules();
128+
require("../background.js");
129+
130+
// Simulate extension installation event
131+
mockBrowser.runtime.onInstalled.callback({ reason: "install" });
132+
133+
expect(mockBrowser.tabs.create).toHaveBeenCalledWith({ url: "index.html" });
134+
});
135+
136+
it("should open index.html in a new window when browserAction is clicked in Chrome", () => {
137+
navigator.userAgent =
138+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
139+
140+
jest.resetModules();
141+
require("../background.js");
142+
143+
mockChrome.browserAction.onClicked.callback({ id: "tab-123" });
144+
145+
expect(window.open).toHaveBeenCalledWith("chrome-extension://fake-id/index.html");
146+
expect(mockChrome.runtime.getURL).toHaveBeenCalledWith("index.html");
147+
});
148+
149+
it("should open index.html in a new window when extension is installed in Chrome", () => {
150+
navigator.userAgent =
151+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
152+
153+
jest.resetModules();
154+
require("../background.js");
155+
156+
mockChrome.runtime.onInstalled.callback({ reason: "install" });
157+
158+
expect(window.open).toHaveBeenCalledWith("chrome-extension://fake-id/index.html");
159+
expect(mockChrome.runtime.getURL).toHaveBeenCalledWith("index.html");
160+
});
161+
162+
it("should properly export module values for Node.js environment", () => {
163+
// Test Firefox environment exports
164+
navigator.userAgent =
165+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0";
166+
jest.resetModules();
167+
const firefoxExports = require("../background.js");
168+
169+
expect(firefoxExports.isFirefox).toBe(true);
170+
expect(firefoxExports.browserAction).toBe(mockBrowser.browserAction);
171+
172+
// Test Chrome environment exports
173+
navigator.userAgent = "Chrome";
174+
jest.resetModules();
175+
const chromeExports = require("../background.js");
176+
177+
expect(chromeExports.isFirefox).toBe(false);
178+
expect(chromeExports.browserAction).toBe(mockChrome.browserAction);
179+
});
83180
});

js/__tests__/languagebox.test.js

Lines changed: 188 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ const LanguageBox = require("../languagebox");
2222
const mockActivity = {
2323
storage: {
2424
languagePreference: "enUS",
25-
kanaPreference: null,
25+
kanaPreference: null
2626
},
27-
textMsg: jest.fn(),
27+
textMsg: jest.fn()
2828
};
2929

3030
Object.defineProperty(global, "localStorage", {
3131
value: {
3232
getItem: jest.fn(),
33-
setItem: jest.fn(),
33+
setItem: jest.fn()
3434
},
35-
writable: true,
35+
writable: true
3636
});
3737

3838
delete global.window.location;
3939
global.window.location = {
40-
reload: jest.fn(),
40+
reload: jest.fn()
4141
};
4242

4343
document.querySelectorAll = jest.fn(() => []);
@@ -103,4 +103,187 @@ describe("LanguageBox Class", () => {
103103
expect(link.addEventListener).toHaveBeenCalledWith("click", expect.any(Function));
104104
});
105105
});
106+
107+
// Test each language selection method
108+
describe("Language selection methods", () => {
109+
it("should set language to enUS and call hide when enUS_onclick is called", () => {
110+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
111+
112+
languageBox.enUS_onclick();
113+
114+
expect(languageBox._language).toBe("enUS");
115+
expect(hideSpy).toHaveBeenCalled();
116+
});
117+
118+
it("should set language to enUK and call hide when enUK_onclick is called", () => {
119+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
120+
121+
languageBox.enUK_onclick();
122+
123+
expect(languageBox._language).toBe("enUK");
124+
expect(hideSpy).toHaveBeenCalled();
125+
});
126+
127+
it("should set language to ko and call hide when ko_onclick is called", () => {
128+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
129+
130+
languageBox.ko_onclick();
131+
132+
expect(languageBox._language).toBe("ko");
133+
expect(hideSpy).toHaveBeenCalled();
134+
});
135+
136+
it("should set language to ja, set kanji preference, and call hide when ja_onclick is called", () => {
137+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
138+
139+
languageBox.ja_onclick();
140+
141+
expect(languageBox._language).toBe("ja");
142+
expect(mockActivity.storage.kanaPreference).toBe("kanji");
143+
expect(hideSpy).toHaveBeenCalled();
144+
});
145+
146+
it("should set language to ja, set kana preference, and call hide when kana_onclick is called", () => {
147+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
148+
149+
languageBox.kana_onclick();
150+
151+
expect(languageBox._language).toBe("ja");
152+
expect(mockActivity.storage.kanaPreference).toBe("kana");
153+
expect(hideSpy).toHaveBeenCalled();
154+
});
155+
156+
it("should set language to es and call hide when es_onclick is called", () => {
157+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
158+
159+
languageBox.es_onclick();
160+
161+
expect(languageBox._language).toBe("es");
162+
expect(hideSpy).toHaveBeenCalled();
163+
});
164+
165+
it("should set language to pt and call hide when pt_onclick is called", () => {
166+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
167+
168+
languageBox.pt_onclick();
169+
170+
expect(languageBox._language).toBe("pt");
171+
expect(hideSpy).toHaveBeenCalled();
172+
});
173+
174+
it("should set language to zhCN and call hide when zhCN_onclick is called", () => {
175+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
176+
177+
languageBox.zhCN_onclick();
178+
179+
expect(languageBox._language).toBe("zhCN");
180+
expect(hideSpy).toHaveBeenCalled();
181+
});
182+
183+
it("should set language to th and call hide when th_onclick is called", () => {
184+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
185+
186+
languageBox.th_onclick();
187+
188+
expect(languageBox._language).toBe("th");
189+
expect(hideSpy).toHaveBeenCalled();
190+
});
191+
192+
it("should set language to hi and call hide when hi_onclick is called", () => {
193+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
194+
195+
languageBox.hi_onclick();
196+
197+
expect(languageBox._language).toBe("hi");
198+
expect(hideSpy).toHaveBeenCalled();
199+
});
200+
201+
it("should set language to ibo and call hide when ibo_onclick is called", () => {
202+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
203+
204+
languageBox.ibo_onclick();
205+
206+
expect(languageBox._language).toBe("ibo");
207+
expect(hideSpy).toHaveBeenCalled();
208+
});
209+
210+
it("should set language to ar and call hide when ar_onclick is called", () => {
211+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
212+
213+
languageBox.ar_onclick();
214+
215+
expect(languageBox._language).toBe("ar");
216+
expect(hideSpy).toHaveBeenCalled();
217+
});
218+
219+
it("should set language to he and call hide when he_onclick is called", () => {
220+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
221+
222+
languageBox.he_onclick();
223+
224+
expect(languageBox._language).toBe("he");
225+
expect(hideSpy).toHaveBeenCalled();
226+
});
227+
228+
it("should set language to te and call hide when te_onclick is called", () => {
229+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
230+
231+
languageBox.te_onclick();
232+
233+
expect(languageBox._language).toBe("te");
234+
expect(hideSpy).toHaveBeenCalled();
235+
});
236+
237+
it("should set language to ayc and call hide when ayc_onclick is called", () => {
238+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
239+
240+
languageBox.ayc_onclick();
241+
242+
expect(languageBox._language).toBe("ayc");
243+
expect(hideSpy).toHaveBeenCalled();
244+
});
245+
246+
it("should set language to quz and call hide when quz_onclick is called", () => {
247+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
248+
249+
languageBox.quz_onclick();
250+
251+
expect(languageBox._language).toBe("quz");
252+
expect(hideSpy).toHaveBeenCalled();
253+
});
254+
255+
it("should set language to gug and call hide when gug_onclick is called", () => {
256+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
257+
258+
languageBox.gug_onclick();
259+
260+
expect(languageBox._language).toBe("gug");
261+
expect(hideSpy).toHaveBeenCalled();
262+
});
263+
264+
it("should set language to ur and call hide when ur_onclick is called", () => {
265+
const hideSpy = jest.spyOn(languageBox, "hide").mockImplementation();
266+
267+
languageBox.ur_onclick();
268+
269+
expect(languageBox._language).toBe("ur");
270+
expect(hideSpy).toHaveBeenCalled();
271+
});
272+
});
273+
274+
// Test Japanese kana preference
275+
describe("Japanese kana handling", () => {
276+
it("should display kana message when Japanese language is selected with kana preference", () => {
277+
localStorage.getItem.mockReturnValue("enUS");
278+
mockActivity.storage.kanaPreference = "kana";
279+
mockActivity.textMsg.mockImplementation();
280+
281+
languageBox._language = "ja";
282+
languageBox.hide();
283+
284+
expect(mockActivity.textMsg).toHaveBeenCalledWith(
285+
expect.stringContaining("げんごを かえるには、ブラウザを こうしんしてください。")
286+
);
287+
});
288+
});
106289
});

0 commit comments

Comments
 (0)