forked from rugk/offline-qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconHandler.test.js
108 lines (87 loc) · 3.39 KB
/
iconHandler.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import "https://unpkg.com/[email protected]/mocha.js"; /* globals mocha */
import "https://unpkg.com/[email protected]/pkg/sinon.js"; /* globals sinon */
import * as AddonSettingsStub from "/common/modules/AddonSettings/tests/helper/AddonSettingsStub.js";
import * as IconHandler from "/common/modules/IconHandler.js";
describe("common module: IconHandler", function () {
before(function () {
// Mocked function needs to be accessed at least once to get initiated and not be just a getter/property.
// Otherwise "TypeError: Attempted to wrap undefined property getUILanguage as functioncheckWrappedMethod" is shown.
// See https://discourse.mozilla.org/t/webextension-apis-made-as-getter-setter-lazily-evaluating-to-functions-beware-of-mocking-for-unit-tests/30849
/* eslint-disable no-unused-expressions */
browser.browserAction.setIcon;
/* eslint-enable no-unused-expressions */
});
let mockBrowserAction;
beforeEach(function() {
mockBrowserAction = sinon.mock(browser.browserAction);
});
afterEach(function() {
sinon.restore();
});
/**
* Verifies the colored icon has been set.
*
* @function
* @param {Object} testFunction the function to call for testing, must
return a Promise
* @returns {Promise}
*/
async function testIconIsColored(testFunction) {
/* eslint-disable indent */
mockBrowserAction.expects("setIcon")
.once().withArgs({path: "/icons/icon-small-colored.svg"})
.resolves();
await testFunction();
// verify results
mockBrowserAction.verify();
}
/**
* Verifies the colored icon has been unset/removed.
*
* @function
* @param {Object} testFunction the function to call for testing, must
return a Promise
* @returns {Promise}
*/
async function testIconIsNotColored(testFunction) {
/* eslint-disable indent */
mockBrowserAction.expects("setIcon")
.once().withArgs({path: null})
.resolves();
await testFunction();
// verify results
mockBrowserAction.verify();
}
describe("changeIconIfColored()", function () {
it("changes icon to colored one", function () {
return testIconIsColored(IconHandler.changeIconIfColored.bind(null, true));
});
it("resets icon to not colored one", function () {
return testIconIsNotColored(IconHandler.changeIconIfColored.bind(null, false));
});
});
describe("init()", function () {
before(function () {
AddonSettingsStub.before();
});
beforeEach(function() {
AddonSettingsStub.stubAllStorageApis();
});
afterEach(function() {
sinon.restore();
AddonSettingsStub.afterTest();
});
it("loads settings and sets colored icon", function () {
AddonSettingsStub.stubSettings({
"popupIconColored": true
});
return testIconIsColored(IconHandler.init);
});
it("loads settings and sets not colored icon", function () {
AddonSettingsStub.stubSettings({
"popupIconColored": false
});
return testIconIsNotColored(IconHandler.init);
});
});
});