Skip to content

Commit

Permalink
Extract tests to their own test() + assert message
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Mar 13, 2021
1 parent 97acf68 commit f6aece9
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions test/fixtures/detect-existing-browser-api-object/content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global originalAPIObjects */

test("browser api object in content script", async (t) => {
test("browser api object in content script", (t) => {
t.ok(browser && browser.runtime, "a global browser API object should be defined");
t.ok(chrome && chrome.runtime, "a global chrome API object should be defined");

Expand All @@ -10,27 +10,12 @@ test("browser api object in content script", async (t) => {
// On Firefox, window is not the global object for content scripts, and so we expect window.browser to not
// be defined.
t.equal(window.browser, undefined, "window.browser is expected to be undefined on Firefox");

try {
await browser.storage.sync.set({a: 'a'.repeat(10000000)});
t.fail('It should throw when attempting to set an object over quota');
} catch (error) {
t.ok(error instanceof Error);
}
} else {
// Check that the polyfill has created a wrapped API namespace as expected.
t.notEqual(browser.runtime, chrome.runtime, "browser.runtime and chrome.runtime should not be equal");
// On chrome, window is the global object and so the polyfilled browser API should
// be also equal to window.browser.
t.equal(browser, window.browser, "browser and window.browser should be the same object");

chrome.storage.local.set({a: 'a'.repeat(10000000)}, () => {
t.ok(chrome.runtime.lastError, 'It should throw when attempting to set an object over quota');
t.equal(chrome.runtime.lastError.message, 'QUOTA_BYTES quota exceeded');
t.notOk(chrome.runtime.lastError instanceof Error);
});

t.end();
}
});

Expand All @@ -48,3 +33,23 @@ test("browser api object in background page", async (t) => {
t.ok(!reply.windowBrowserIsUnchanged, "window.browser API object should have been defined by the polyfill");
}
});

test("error types", async (t) => {
t.plan(3);

if (navigator.userAgent.includes("Firefox/")) {
try {
await browser.storage.sync.set({a: 'a'.repeat(10000000)});
t.fail('It should throw when attempting to set an object over quota');
} catch (error) {
t.equal(error.message, 'QUOTA_BYTES quota exceeded');
t.ok(error instanceof Error);
}
} else {
chrome.storage.local.set({a: 'a'.repeat(10000000)}, () => {
t.ok(chrome.runtime.lastError, 'It should throw when attempting to set an object over quota');
t.equal(chrome.runtime.lastError.message, 'QUOTA_BYTES quota exceeded');
t.notOk(chrome.runtime.lastError instanceof Error);
});
}
});

0 comments on commit f6aece9

Please sign in to comment.