fix: cleanup async function tests and other minor tweaks on tests.

This commit is contained in:
Luca Greco 2016-10-11 14:27:26 +02:00
parent b0a111cdc7
commit f28858961f
4 changed files with 34 additions and 12 deletions

View File

@ -10,21 +10,38 @@ describe("browser-polyfill", () => {
it("returns a promise which resolves to the callback parameters", () => { it("returns a promise which resolves to the callback parameters", () => {
const fakeChrome = { const fakeChrome = {
alarms: {clear: sinon.stub()}, alarms: {clear: sinon.stub()},
runtime: {lastError: null}, runtime: {
lastError: null,
requestUpdateCheck: sinon.stub(),
},
tabs: {
query: sinon.stub(),
},
}; };
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
// Test for single callback argument.
fakeChrome.alarms.clear fakeChrome.alarms.clear
.onFirstCall().callsArgWith(1, "res1") .onFirstCall().callsArgWith(1, "res1");
.onSecondCall().callsArgWith(1, "res1", "res2", "res3");
// 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");
return Promise.all([ return Promise.all([
window.browser.alarms.clear("test1"), window.browser.alarms.clear("test1"),
window.browser.alarms.clear("test2"), window.browser.tabs.query({active: true}),
window.browser.runtime.requestUpdateCheck(),
]); ]);
}).then(results => { }).then(results => {
assert.equal(results[0], "res1", "The first call resolved to a single value"); assert.equal(results[0], "res1", "Fake alarms.clear call resolved to a single value");
assert.deepEqual(results[1], ["res1", "res2", "res3"], assert.deepEqual(results[1], ["res1", "res2"],
"The second call resolved to an array of the expected values"); "Fake tabs.query resolved to an array of values");
assert.deepEqual(results[2], ["res1", "res2"],
"Fake runtime.requestUpdateCheck resolved to an array of values");
}); });
}); });
}); });

View File

@ -5,7 +5,7 @@ const {assert} = require("chai");
const {setupTestDOMWindow} = require("./setup"); const {setupTestDOMWindow} = require("./setup");
describe("browser-polyfill", () => { describe("browser-polyfill", () => {
it("automatically wrapps chrome into a browser object", () => { it("wraps the global chrome namespace with a global browser namespace", () => {
const fakeChrome = {}; const fakeChrome = {};
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
assert.equal(typeof window.browser, "object", "Got the window.browser object"); assert.equal(typeof window.browser, "object", "Got the window.browser object");

View File

@ -22,7 +22,7 @@ describe("browser-polyfill", () => {
}); });
}); });
it("delete proxy getter/setter that are not wrapped", () => { it("deletes proxy getter/setter that are not wrapped", () => {
const fakeChrome = {}; const fakeChrome = {};
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
window.browser.newns = {newkey: "test-value"}; window.browser.newns = {newkey: "test-value"};
@ -41,6 +41,8 @@ describe("browser-polyfill", () => {
delete window.browser.newns.newkey2; delete window.browser.newns.newkey2;
assert.equal(window.browser.newns.newkey2, undefined, assert.equal(window.browser.newns.newkey2, undefined,
"Got the expected result from setting a wrapped property name"); "Got the expected result from setting a wrapped property name");
assert.ok(!("newkey2" in window.browser.newns),
"The deleted property is not listed anymore");
}); });
}); });
}); });

View File

@ -7,7 +7,7 @@ const {setupTestDOMWindow} = require("./setup");
describe("browser-polyfill", () => { describe("browser-polyfill", () => {
describe("wrapped runtime.onMessage listener", () => { describe("wrapped runtime.onMessage listener", () => {
it("keep track of the listeners added", () => { it("keeps track of the listeners added", () => {
const messageListener = sinon.spy(); const messageListener = sinon.spy();
const fakeChrome = { const fakeChrome = {
@ -24,13 +24,14 @@ describe("browser-polyfill", () => {
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
fakeChrome.runtime.onMessage.hasListener fakeChrome.runtime.onMessage.hasListener
.onFirstCall().returns(false) .onFirstCall().returns(false)
.onSecondCall().returns(true); .onSecondCall().returns(true)
.onThirdCall().returns(false);
assert.equal(window.browser.runtime.onMessage.hasListener(messageListener), assert.equal(window.browser.runtime.onMessage.hasListener(messageListener),
false, "Got hasListener==false before the listener has been added"); false, "Got hasListener==false before the listener has been added");
window.browser.runtime.onMessage.addListener(messageListener); window.browser.runtime.onMessage.addListener(messageListener);
assert.equal(window.browser.runtime.onMessage.hasListener(messageListener), assert.equal(window.browser.runtime.onMessage.hasListener(messageListener),
true, "Got hasListener=true once the listener has been added"); true, "Got hasListener==true once the listener has been added");
window.browser.runtime.onMessage.addListener(messageListener); window.browser.runtime.onMessage.addListener(messageListener);
assert.ok(fakeChrome.runtime.onMessage.addListener.calledTwice, assert.ok(fakeChrome.runtime.onMessage.addListener.calledTwice,
@ -49,6 +50,8 @@ describe("browser-polyfill", () => {
assert.equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0], assert.equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
fakeChrome.runtime.onMessage.removeListener.firstCall.args[0], fakeChrome.runtime.onMessage.removeListener.firstCall.args[0],
"both the addListener and removeListenercalls received the same wrapped listener"); "both the addListener and removeListenercalls received the same wrapped listener");
assert.equal(fakeChrome.runtime.onMessage.hasListener(messageListener), false,
"Got hasListener==false once the listener has been removed");
}); });
}); });