style: destructure the assert methods to globals

This commit is contained in:
Luca Greco 2016-10-12 02:08:52 +02:00
parent 2853e98546
commit 3842bd1693
4 changed files with 62 additions and 62 deletions

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const {assert} = require("chai"); const {deepEqual, equal, fail, throws} = require("chai").assert;
const sinon = require("sinon"); const sinon = require("sinon");
const {setupTestDOMWindow} = require("./setup"); const {setupTestDOMWindow} = require("./setup");
@ -37,10 +37,10 @@ describe("browser-polyfill", () => {
window.browser.runtime.requestUpdateCheck(), window.browser.runtime.requestUpdateCheck(),
]); ]);
}).then(results => { }).then(results => {
assert.equal(results[0], "res1", "Fake alarms.clear call resolved to a single value"); equal(results[0], "res1", "Fake alarms.clear call resolved to a single value");
assert.deepEqual(results[1], ["res1", "res2"], deepEqual(results[1], ["res1", "res2"],
"Fake tabs.query resolved to an array of values"); "Fake tabs.query resolved to an array of values");
assert.deepEqual(results[2], ["res1", "res2"], deepEqual(results[2], ["res1", "res2"],
"Fake runtime.requestUpdateCheck resolved to an array of values"); "Fake runtime.requestUpdateCheck resolved to an array of values");
}); });
}); });
@ -61,8 +61,8 @@ describe("browser-polyfill", () => {
.onFirstCall().callsArgWith(1, ["res1", "res2"]); .onFirstCall().callsArgWith(1, ["res1", "res2"]);
return window.browser.tabs.query({active: true}).then( return window.browser.tabs.query({active: true}).then(
() => assert.fail("Expected a rejected promise"), () => fail("Expected a rejected promise"),
(err) => assert.equal(err, fakeChrome.runtime.lastError, (err) => equal(err, fakeChrome.runtime.lastError,
"Got the expected error in the rejected promise") "Got the expected error in the rejected promise")
); );
}); });
@ -77,11 +77,11 @@ describe("browser-polyfill", () => {
}; };
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
assert.throws(() => { throws(() => {
window.browser.runtime.sendMessage(); window.browser.runtime.sendMessage();
}, "Expected at least 1 arguments for sendMessage(), got 0"); }, "Expected at least 1 arguments for sendMessage(), got 0");
assert.throws(() => { throws(() => {
window.browser.runtime.sendMessage("0", "1", "2", "3"); window.browser.runtime.sendMessage("0", "1", "2", "3");
}, "Expected at most 3 arguments for sendMessage(), got 4"); }, "Expected at most 3 arguments for sendMessage(), got 4");
}); });

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const {assert} = require("chai"); const {deepEqual, equal, ok} = require("chai").assert;
const {setupTestDOMWindow} = require("./setup"); const {setupTestDOMWindow} = require("./setup");
@ -8,7 +8,7 @@ describe("browser-polyfill", () => {
it("wraps the global chrome namespace with a global browser namespace", () => { 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"); equal(typeof window.browser, "object", "Got the window.browser object");
}); });
}); });
@ -21,7 +21,7 @@ describe("browser-polyfill", () => {
}; };
return setupTestDOMWindow(fakeChrome, fakeBrowser).then(window => { return setupTestDOMWindow(fakeChrome, fakeBrowser).then(window => {
assert.deepEqual(window.browser, fakeBrowser, deepEqual(window.browser, fakeBrowser,
"The existent browser has not been wrapped"); "The existent browser has not been wrapped");
}); });
}); });
@ -36,16 +36,16 @@ describe("browser-polyfill", () => {
value: {mykey: true}, value: {mykey: true},
}); });
assert.ok("myns" in window.browser, "The custom property exists"); ok("myns" in window.browser, "The custom property exists");
assert.ok("mykey" in window.browser.myns, ok("mykey" in window.browser.myns,
"The content of the custom property exists"); "The content of the custom property exists");
assert.deepEqual(window.browser.myns, {mykey: true}, deepEqual(window.browser.myns, {mykey: true},
"The custom property has the expected content"); "The custom property has the expected content");
delete window.browser.myns; delete window.browser.myns;
assert.ok(!("myns" in window.browser), ok(!("myns" in window.browser),
"The deleted custom defined property has been removed"); "The deleted custom defined property has been removed");
}); });
}); });
@ -53,11 +53,11 @@ describe("browser-polyfill", () => {
it("returns undefined for property undefined in the target", () => { it("returns undefined for property undefined in the target", () => {
const fakeChrome = {myns: {mykey: true}}; const fakeChrome = {myns: {mykey: true}};
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
assert.equal(window.browser.myns.mykey, true, equal(window.browser.myns.mykey, true,
"Got the expected result from a wrapped property"); "Got the expected result from a wrapped property");
assert.equal(window.browser.myns.nonexistent, undefined, equal(window.browser.myns.nonexistent, undefined,
"Got undefined for non existent property"); "Got undefined for non existent property");
assert.equal(window.browser.nonexistent, undefined, equal(window.browser.nonexistent, undefined,
"Got undefined for non existent namespaces"); "Got undefined for non existent namespaces");
}); });
}); });

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const {assert} = require("chai"); const {deepEqual, equal, ok} = require("chai").assert;
const sinon = require("sinon"); const sinon = require("sinon");
const {setupTestDOMWindow} = require("./setup"); const {setupTestDOMWindow} = require("./setup");
@ -14,14 +14,14 @@ describe("browser-polyfill", () => {
}, },
}; };
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
assert.ok(window.browser.runtime.nonwrappedmethod); ok(window.browser.runtime.nonwrappedmethod);
const fakeCallback = () => {}; const fakeCallback = () => {};
window.browser.runtime.nonwrappedmethod(fakeCallback); window.browser.runtime.nonwrappedmethod(fakeCallback);
const receivedCallback = fakeChrome.runtime.nonwrappedmethod.firstCall.args[0]; const receivedCallback = fakeChrome.runtime.nonwrappedmethod.firstCall.args[0];
assert.equal(fakeCallback, receivedCallback, equal(fakeCallback, receivedCallback,
"The callback has not been wrapped for the nonwrappedmethod"); "The callback has not been wrapped for the nonwrappedmethod");
}); });
}); });
@ -36,9 +36,9 @@ describe("browser-polyfill", () => {
}; };
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
// Check that the property values on the generated wrapper. // Check that the property values on the generated wrapper.
assert.equal(window.browser.runtime.myprop, "previous-value", equal(window.browser.runtime.myprop, "previous-value",
"Got the expected result from setting a wrapped property name"); "Got the expected result from setting a wrapped property name");
assert.equal(window.browser.nowrapns.nowrapkey, "previous-value", equal(window.browser.nowrapns.nowrapkey, "previous-value",
"Got the expected result from setting a wrapped property name"); "Got the expected result from setting a wrapped property name");
// Update the properties on the generated wrapper. // Update the properties on the generated wrapper.
@ -46,30 +46,30 @@ describe("browser-polyfill", () => {
const setResult2 = window.browser.nowrapns.nowrapkey = "new-value"; const setResult2 = window.browser.nowrapns.nowrapkey = "new-value";
// Check the results of setting the new value of the wrapped properties. // Check the results of setting the new value of the wrapped properties.
assert.equal(setResult, "new-value", equal(setResult, "new-value",
"Got the expected result from setting a wrapped property name"); "Got the expected result from setting a wrapped property name");
assert.equal(setResult2, "new-value", equal(setResult2, "new-value",
"Got the expected result from setting a wrapped property name"); "Got the expected result from setting a wrapped property name");
// Verify that the wrapped properties has been updated. // Verify that the wrapped properties has been updated.
assert.equal(window.browser.runtime.myprop, "new-value", equal(window.browser.runtime.myprop, "new-value",
"Got the expected updated value from the browser property"); "Got the expected updated value from the browser property");
assert.equal(window.browser.nowrapns.nowrapkey, "new-value", equal(window.browser.nowrapns.nowrapkey, "new-value",
"Got the expected updated value from the browser property"); "Got the expected updated value from the browser property");
// Verify that the target properties has been updated. // Verify that the target properties has been updated.
assert.equal(window.chrome.runtime.myprop, "new-value", equal(window.chrome.runtime.myprop, "new-value",
"Got the expected updated value on the related chrome property"); "Got the expected updated value on the related chrome property");
assert.equal(window.chrome.nowrapns.nowrapkey, "new-value", equal(window.chrome.nowrapns.nowrapkey, "new-value",
"Got the expected updated value on the related chrome property"); "Got the expected updated value on the related chrome property");
// Set a property multiple times before read. // Set a property multiple times before read.
window.browser.nowrapns.nowrapkey2 = "new-value2"; window.browser.nowrapns.nowrapkey2 = "new-value2";
window.browser.nowrapns.nowrapkey2 = "new-value3"; window.browser.nowrapns.nowrapkey2 = "new-value3";
assert.equal(window.chrome.nowrapns.nowrapkey2, "new-value3", equal(window.chrome.nowrapns.nowrapkey2, "new-value3",
"Got the expected updated value on the related chrome property"); "Got the expected updated value on the related chrome property");
assert.equal(window.browser.nowrapns.nowrapkey2, "new-value3", equal(window.browser.nowrapns.nowrapkey2, "new-value3",
"Got the expected updated value on the wrapped property"); "Got the expected updated value on the wrapped property");
}); });
}); });
@ -79,24 +79,24 @@ describe("browser-polyfill", () => {
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
window.browser.newns = {newkey: "test-value"}; window.browser.newns = {newkey: "test-value"};
assert.ok("newns" in window.browser, "The custom namespace is in the wrapper"); ok("newns" in window.browser, "The custom namespace is in the wrapper");
assert.ok("newns" in window.chrome, "The custom namespace is in the target"); ok("newns" in window.chrome, "The custom namespace is in the target");
assert.equal(window.browser.newns.newkey, "test-value", equal(window.browser.newns.newkey, "test-value",
"Got the expected result from setting a wrapped property name"); "Got the expected result from setting a wrapped property name");
const setRes = window.browser.newns = {newkey2: "new-value"}; const setRes = window.browser.newns = {newkey2: "new-value"};
assert.equal(window.browser.newns.newkey2, "new-value", equal(window.browser.newns.newkey2, "new-value",
"The new non-wrapped getter is cached"); "The new non-wrapped getter is cached");
assert.deepEqual(setRes, {newkey2: "new-value"}, deepEqual(setRes, {newkey2: "new-value"},
"Got the expected result from setting a new wrapped property name"); "Got the expected result from setting a new wrapped property name");
assert.deepEqual(window.browser.newns, window.chrome.newns, deepEqual(window.browser.newns, window.chrome.newns,
"chrome.newns and browser.newns are the same"); "chrome.newns and browser.newns are the same");
delete window.browser.newns.newkey2; delete window.browser.newns.newkey2;
assert.equal(window.browser.newns.newkey2, undefined, 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), ok(!("newkey2" in window.browser.newns),
"The deleted property is not listed anymore"); "The deleted property is not listed anymore");
}); });
}); });

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const {assert} = require("chai"); const {deepEqual, equal, ok} = require("chai").assert;
const sinon = require("sinon"); const sinon = require("sinon");
const {setupTestDOMWindow} = require("./setup"); const {setupTestDOMWindow} = require("./setup");
@ -24,7 +24,7 @@ describe("browser-polyfill", () => {
window.browser.runtime.onMessage.addListener(fakeNonFunctionListener); window.browser.runtime.onMessage.addListener(fakeNonFunctionListener);
assert.deepEqual(fakeChrome.runtime.onMessage.addListener.firstCall.args[0], deepEqual(fakeChrome.runtime.onMessage.addListener.firstCall.args[0],
fakeNonFunctionListener, fakeNonFunctionListener,
"The non-function listener has not been wrapped"); "The non-function listener has not been wrapped");
}); });
@ -53,37 +53,37 @@ describe("browser-polyfill", () => {
// Fake the hasListener result for "listener unregistered". // Fake the hasListener result for "listener unregistered".
.onThirdCall().returns(false); .onThirdCall().returns(false);
assert.equal(window.browser.runtime.onMessage.hasListener(messageListener), 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), 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");
// Add the same listener again to test that it will be called with the // Add the same listener again to test that it will be called with the
// same wrapped listener. // same wrapped listener.
window.browser.runtime.onMessage.addListener(messageListener); window.browser.runtime.onMessage.addListener(messageListener);
assert.ok(fakeChrome.runtime.onMessage.addListener.calledTwice, ok(fakeChrome.runtime.onMessage.addListener.calledTwice,
"addListener has been called twice"); "addListener has been called twice");
assert.equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0], equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
fakeChrome.runtime.onMessage.addListener.firstCall.args[0], fakeChrome.runtime.onMessage.addListener.firstCall.args[0],
"both the addListener calls received the same wrapped listener"); "both the addListener calls received the same wrapped listener");
// Retrieve the wrapped listener and execute it to fake a received message. // Retrieve the wrapped listener and execute it to fake a received message.
const wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0]; const wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
wrappedListener("msg", {name: "sender"}, function sendResponse() {}); wrappedListener("msg", {name: "sender"}, function sendResponse() {});
assert.ok(messageListener.calledOnce, "The listener has been called once"); ok(messageListener.calledOnce, "The listener has been called once");
// Remove the listener. // Remove the listener.
window.browser.runtime.onMessage.removeListener(messageListener); window.browser.runtime.onMessage.removeListener(messageListener);
assert.ok(fakeChrome.runtime.onMessage.removeListener.calledOnce, ok(fakeChrome.runtime.onMessage.removeListener.calledOnce,
"removeListener has been called once"); "removeListener has been called once");
assert.equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0], 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, equal(fakeChrome.runtime.onMessage.hasListener(messageListener), false,
"Got hasListener==false once the listener has been removed"); "Got hasListener==false once the listener has been removed");
}); });
}); });
@ -118,19 +118,19 @@ describe("browser-polyfill", () => {
return setupTestDOMWindow(fakeChrome).then(window => { return setupTestDOMWindow(fakeChrome).then(window => {
window.browser.runtime.onMessage.addListener(messageListener); window.browser.runtime.onMessage.addListener(messageListener);
assert.ok(fakeChrome.runtime.onMessage.addListener.calledOnce); ok(fakeChrome.runtime.onMessage.addListener.calledOnce);
wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0]; wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
wrappedListener("fake message", {name: "fake sender"}, sendResponseSpy); wrappedListener("fake message", {name: "fake sender"}, sendResponseSpy);
assert.ok(messageListener.calledOnce, "The unwrapped message listener has been called"); ok(messageListener.calledOnce, "The unwrapped message listener has been called");
assert.deepEqual(messageListener.firstCall.args, deepEqual(messageListener.firstCall.args,
["fake message", {name: "fake sender"}], ["fake message", {name: "fake sender"}],
"The unwrapped message listener has received the expected parameters"); "The unwrapped message listener has received the expected parameters");
assert.ok(sendResponseSpy.calledOnce, "The sendResponse function has been called"); ok(sendResponseSpy.calledOnce, "The sendResponse function has been called");
assert.equal(sendResponseSpy.firstCall.args[0], "fake reply", equal(sendResponseSpy.firstCall.args[0], "fake reply",
"sendResponse callback has been called with the expected parameters"); "sendResponse callback has been called with the expected parameters");
wrappedListener("fake message2", {name: "fake sender2"}, sendResponseSpy); wrappedListener("fake message2", {name: "fake sender2"}, sendResponseSpy);
@ -138,29 +138,29 @@ describe("browser-polyfill", () => {
// Wait the second response promise to be resolved. // Wait the second response promise to be resolved.
return secondResponse; return secondResponse;
}).then(() => { }).then(() => {
assert.ok(messageListener.calledTwice, ok(messageListener.calledTwice,
"The unwrapped message listener has been called"); "The unwrapped message listener has been called");
assert.deepEqual(messageListener.secondCall.args, deepEqual(messageListener.secondCall.args,
["fake message2", {name: "fake sender2"}], ["fake message2", {name: "fake sender2"}],
"The unwrapped listener has received the expected parameters"); "The unwrapped listener has received the expected parameters");
assert.ok(sendResponseSpy.calledTwice, "The sendResponse function has been called"); ok(sendResponseSpy.calledTwice, "The sendResponse function has been called");
assert.equal(sendResponseSpy.secondCall.args[0], "fake reply 2", equal(sendResponseSpy.secondCall.args[0], "fake reply 2",
"sendResponse callback has been called with the expected parameters"); "sendResponse callback has been called with the expected parameters");
}).then(() => { }).then(() => {
wrappedListener("fake message3", {name: "fake sender3"}, sendResponseSpy); wrappedListener("fake message3", {name: "fake sender3"}, sendResponseSpy);
// Wait the third response promise to be rejected. // Wait the third response promise to be rejected.
return thirdResponse.catch(err => { return thirdResponse.catch(err => {
assert.equal(messageListener.callCount, 3, equal(messageListener.callCount, 3,
"The unwrapped message listener has been called"); "The unwrapped message listener has been called");
assert.deepEqual(messageListener.thirdCall.args, deepEqual(messageListener.thirdCall.args,
["fake message3", {name: "fake sender3"}], ["fake message3", {name: "fake sender3"}],
"The unwrapped listener has received the expected parameters"); "The unwrapped listener has received the expected parameters");
assert.equal(sendResponseSpy.callCount, 3, equal(sendResponseSpy.callCount, 3,
"The sendResponse function has been called"); "The sendResponse function has been called");
assert.equal(sendResponseSpy.thirdCall.args[0], err, equal(sendResponseSpy.thirdCall.args[0], err,
"sendResponse callback has been called with the expected parameters"); "sendResponse callback has been called with the expected parameters");
}); });
}); });