Skip to content

Commit e908288

Browse files
authored
Tests for background.js and planetInterface.js (#4318)
* Create background.test.js * Exporting modules for test * Create planetInterface.test.js * Exporting modules for test * Update planetInterface.test.js
1 parent e3308f7 commit e908288

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

js/__tests__/background.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
describe("Browser Action Behavior", () => {
2+
let mockBrowser;
3+
let mockChrome;
4+
5+
beforeEach(() => {
6+
// Mock objects
7+
mockBrowser = {
8+
browserAction: {
9+
onClicked: { addListener: jest.fn() },
10+
},
11+
tabs: { create: jest.fn() },
12+
runtime: {
13+
onInstalled: { addListener: jest.fn() },
14+
},
15+
};
16+
17+
mockChrome = {
18+
browserAction: {
19+
onClicked: { addListener: jest.fn() },
20+
},
21+
runtime: {
22+
onInstalled: { addListener: jest.fn() },
23+
getURL: jest.fn((path) => `chrome-extension://fake-id/${path}`),
24+
},
25+
tabs: { create: jest.fn() },
26+
};
27+
28+
global.browser = mockBrowser;
29+
global.chrome = mockChrome;
30+
31+
Object.defineProperty(global.navigator, "userAgent", {
32+
writable: true,
33+
value: "",
34+
});
35+
});
36+
37+
afterEach(() => {
38+
jest.clearAllMocks();
39+
delete global.browser;
40+
delete global.chrome;
41+
});
42+
43+
it("should set up Firefox-specific listeners when user agent is Firefox", () => {
44+
navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0";
45+
46+
jest.resetModules(); // Clear the module cache
47+
const { isFirefox, browserAction } = require("../background.js");
48+
49+
expect(isFirefox).toBe(true);
50+
expect(browserAction.onClicked.addListener).toHaveBeenCalledTimes(1);
51+
expect(mockBrowser.runtime.onInstalled.addListener).toHaveBeenCalledTimes(1);
52+
});
53+
54+
it("should set up Chrome-specific listeners when user agent is not Firefox", () => {
55+
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";
56+
57+
jest.resetModules(); // Clear the module cache
58+
const { isFirefox, browserAction } = require("../background.js");
59+
60+
expect(isFirefox).toBe(false);
61+
expect(browserAction.onClicked.addListener).toHaveBeenCalledTimes(1);
62+
expect(mockChrome.runtime.onInstalled.addListener).toHaveBeenCalledTimes(1);
63+
});
64+
});
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const PlanetInterface = require('../planetInterface');
2+
global.platformColor = {
3+
header: '#8bc34a'
4+
};
5+
6+
const mockActivity = {
7+
hideSearchWidget: jest.fn(),
8+
prepSearchWidget: jest.fn(),
9+
sendAllToTrash: jest.fn(),
10+
refreshCanvas: jest.fn(),
11+
_loadStart: jest.fn(),
12+
doLoadAnimation: jest.fn(),
13+
textMsg: jest.fn(),
14+
stage: { enableDOMEvents: jest.fn() },
15+
blocks: { loadNewBlocks: jest.fn(), palettes: { _hideMenus: jest.fn() }, trashStacks: [] },
16+
logo: { doStopTurtles: jest.fn() },
17+
canvas: {},
18+
turtles: {},
19+
loading: false,
20+
prepareExport: jest.fn(),
21+
_allClear: jest.fn()
22+
};
23+
24+
document.body.innerHTML = `
25+
<div id="helpElem"></div>
26+
<div class="canvasHolder"></div>
27+
<div id="canvas"></div>
28+
<meta id="theme-color">
29+
<div id="toolbars"></div>
30+
<div id="palette"></div>
31+
<div id="buttoncontainerBOTTOM"></div>
32+
<div id="buttoncontainerTOP"></div>
33+
<canvas id="overlayCanvas"></canvas>
34+
<iframe id="planet-iframe"></iframe>
35+
<input id="myOpenFile" type="file">
36+
`;
37+
38+
const docById = jest.fn((id) => document.getElementById(id));
39+
global.docById = docById;
40+
41+
beforeAll(() => {
42+
mockCanvas = {
43+
click: jest.fn()
44+
};
45+
window.widgetWindows = {
46+
hideAllWindows: jest.fn(),
47+
showWindows: jest.fn(),
48+
};
49+
window.scroll = jest.fn();
50+
});
51+
52+
describe('PlanetInterface', () => {
53+
let planetInterface;
54+
55+
beforeEach(() => {
56+
planetInterface = new PlanetInterface(mockActivity);
57+
});
58+
59+
test('hideMusicBlocks hides relevant elements and disables DOM events', () => {
60+
planetInterface.hideMusicBlocks();
61+
62+
expect(mockActivity.hideSearchWidget).toHaveBeenCalled();
63+
expect(mockActivity.logo.doStopTurtles).toHaveBeenCalled();
64+
expect(docById('helpElem').style.visibility).toBe('hidden');
65+
expect(document.querySelector('.canvasHolder').classList.contains('hide')).toBe(true);
66+
expect(document.querySelector('#canvas').style.display).toBe('none');
67+
expect(document.querySelector('#theme-color').content).toBe('#8bc34a');
68+
});
69+
70+
test('showMusicBlocks shows relevant elements and enables DOM events', () => {
71+
mockActivity.planet = { getCurrentProjectName: jest.fn(() => 'Test Project') };
72+
73+
planetInterface.showMusicBlocks();
74+
75+
expect(document.title).toBe('Test Project');
76+
expect(docById('toolbars').style.display).toBe('block');
77+
expect(docById('palette').style.display).toBe('block');
78+
expect(mockActivity.prepSearchWidget).toHaveBeenCalled();
79+
expect(document.querySelector('.canvasHolder').classList.contains('hide')).toBe(false);
80+
expect(document.querySelector('#canvas').style.display).toBe('');
81+
});
82+
83+
test('hidePlanet hides the planet interface', () => {
84+
planetInterface.iframe = document.querySelector('#planet-iframe');
85+
planetInterface.hidePlanet();
86+
expect(planetInterface.iframe.style.display).toBe('none');
87+
});
88+
89+
test('openPlanet calls saveLocally, hideMusicBlocks, and showPlanet', () => {
90+
jest.spyOn(planetInterface, 'saveLocally').mockImplementation(() => {});
91+
jest.spyOn(planetInterface, 'hideMusicBlocks').mockImplementation(() => {});
92+
jest.spyOn(planetInterface, 'showPlanet').mockImplementation(() => {});
93+
planetInterface.openPlanet();
94+
expect(planetInterface.saveLocally).toHaveBeenCalled();
95+
expect(planetInterface.hideMusicBlocks).toHaveBeenCalled();
96+
expect(planetInterface.showPlanet).toHaveBeenCalled();
97+
});
98+
99+
test('closePlanet calls hidePlanet and showMusicBlocks', () => {
100+
jest.spyOn(planetInterface, 'hidePlanet').mockImplementation(() => {});
101+
jest.spyOn(planetInterface, 'showMusicBlocks').mockImplementation(() => {});
102+
planetInterface.closePlanet();
103+
expect(planetInterface.hidePlanet).toHaveBeenCalled();
104+
expect(planetInterface.showMusicBlocks).toHaveBeenCalled();
105+
});
106+
107+
test('newProject calls closePlanet, initialiseNewProject, _loadStart, and saveLocally', () => {
108+
jest.spyOn(planetInterface, 'closePlanet').mockImplementation(() => {});
109+
jest.spyOn(planetInterface, 'initialiseNewProject').mockImplementation(() => {});
110+
jest.spyOn(planetInterface, 'saveLocally').mockImplementation(() => {});
111+
planetInterface.newProject();
112+
expect(planetInterface.closePlanet).toHaveBeenCalled();
113+
expect(planetInterface.initialiseNewProject).toHaveBeenCalled();
114+
expect(mockActivity._loadStart).toHaveBeenCalled();
115+
expect(planetInterface.saveLocally).toHaveBeenCalled();
116+
});
117+
});

js/background.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ if (navigator.userAgent.search("Firefox") !== -1) {
3434
window.open(chrome.runtime.getURL("index.html"));
3535
});
3636
}
37+
if (typeof module !== 'undefined' && module.exports) {
38+
module.exports = {
39+
isFirefox: navigator.userAgent.search("Firefox") !== -1,
40+
browserAction: navigator.userAgent.search("Firefox") !== -1 ? browser.browserAction : chrome.browserAction,
41+
};
42+
}

js/planetInterface.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,6 @@ class PlanetInterface {
331331
};
332332
}
333333
}
334+
if (typeof module !== "undefined" && module.exports) {
335+
module.exports = PlanetInterface;
336+
}

0 commit comments

Comments
 (0)