2016-10-10 00:48:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
const {deepEqual, equal, fail, throws} = require("chai").assert;
|
2016-10-10 00:48:35 +00:00
|
|
|
const sinon = require("sinon");
|
|
|
|
|
|
|
|
const {setupTestDOMWindow} = require("./setup");
|
|
|
|
|
|
|
|
describe("browser-polyfill", () => {
|
|
|
|
describe("wrapped async functions", () => {
|
|
|
|
it("returns a promise which resolves to the callback parameters", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
alarms: {clear: sinon.stub()},
|
2016-10-11 12:27:26 +00:00
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
|
|
|
requestUpdateCheck: sinon.stub(),
|
|
|
|
},
|
|
|
|
tabs: {
|
|
|
|
query: sinon.stub(),
|
|
|
|
},
|
2016-10-10 00:48:35 +00:00
|
|
|
};
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-11 12:27:26 +00:00
|
|
|
// Test for single callback argument.
|
2016-10-10 00:48:35 +00:00
|
|
|
fakeChrome.alarms.clear
|
2016-10-11 12:27:26 +00:00
|
|
|
.onFirstCall().callsArgWith(1, "res1");
|
|
|
|
|
|
|
|
// Test for single array callback argument.
|
|
|
|
fakeChrome.tabs.query
|
|
|
|
.onFirstCall().callsArgWith(1, ["res1", "res2"]);
|
|
|
|
|
|
|
|
// Test for multiple callback arguments.
|
|
|
|
fakeChrome.runtime.requestUpdateCheck
|
|
|
|
.onFirstCall().callsArgWith(0, "res1", "res2");
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
window.browser.alarms.clear("test1"),
|
2016-10-11 12:27:26 +00:00
|
|
|
window.browser.tabs.query({active: true}),
|
|
|
|
window.browser.runtime.requestUpdateCheck(),
|
2016-10-10 00:48:35 +00:00
|
|
|
]);
|
|
|
|
}).then(results => {
|
2016-10-12 00:08:52 +00:00
|
|
|
equal(results[0], "res1", "Fake alarms.clear call resolved to a single value");
|
|
|
|
deepEqual(results[1], ["res1", "res2"],
|
2016-10-11 12:27:26 +00:00
|
|
|
"Fake tabs.query resolved to an array of values");
|
2016-10-12 00:08:52 +00:00
|
|
|
deepEqual(results[2], ["res1", "res2"],
|
2016-10-11 12:27:26 +00:00
|
|
|
"Fake runtime.requestUpdateCheck resolved to an array of values");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
2016-10-11 13:08:24 +00:00
|
|
|
|
|
|
|
it("rejects the returned promise if chrome.runtime.lastError is not null", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: new Error("fake lastError"),
|
|
|
|
},
|
|
|
|
tabs: {
|
|
|
|
query: sinon.stub(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
// Test for single array callback argument.
|
|
|
|
fakeChrome.tabs.query
|
|
|
|
.onFirstCall().callsArgWith(1, ["res1", "res2"]);
|
|
|
|
|
|
|
|
return window.browser.tabs.query({active: true}).then(
|
2016-10-12 00:08:52 +00:00
|
|
|
() => fail("Expected a rejected promise"),
|
|
|
|
(err) => equal(err, fakeChrome.runtime.lastError,
|
2016-10-11 13:08:24 +00:00
|
|
|
"Got the expected error in the rejected promise")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("throws if the number of arguments are not in the range defined in the metadata", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
|
|
|
lastError: null,
|
|
|
|
sendMessage: sinon.spy(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
2016-10-12 00:08:52 +00:00
|
|
|
throws(() => {
|
2016-10-11 13:08:24 +00:00
|
|
|
window.browser.runtime.sendMessage();
|
|
|
|
}, "Expected at least 1 arguments for sendMessage(), got 0");
|
|
|
|
|
2016-10-12 00:08:52 +00:00
|
|
|
throws(() => {
|
2016-10-11 13:08:24 +00:00
|
|
|
window.browser.runtime.sendMessage("0", "1", "2", "3");
|
|
|
|
}, "Expected at most 3 arguments for sendMessage(), got 4");
|
|
|
|
});
|
|
|
|
});
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|