-
Notifications
You must be signed in to change notification settings - Fork 214
/
test-browser-global.js
97 lines (83 loc) · 3.12 KB
/
test-browser-global.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
"use strict";
const {deepEqual, equal, notEqual, ok} = require("chai").assert;
const {setupTestDOMWindow} = require("./setup");
describe("browser-polyfill", () => {
it("wraps the global chrome namespace with a global browser namespace", () => {
const fakeChrome = {};
return setupTestDOMWindow(fakeChrome).then(window => {
equal(typeof window.browser, "object", "Got the window.browser object");
});
});
it("does not override the global browser namespace if it already exists and supports promises", () => {
const fakeChrome = {
runtime: {lastError: null},
};
const fakeBrowser = {
mycustomns: {mybrowserkey: true},
};
return setupTestDOMWindow(fakeChrome, fakeBrowser).then(window => {
deepEqual(window.browser, fakeBrowser,
"The existing browser has not been wrapped");
});
});
it("wraps the global browser namespace if it doesn’t support promises", () => {
const fakePlatformInfo = {
os: "test",
arch: "test",
};
const fakeBrowser = {
runtime: {
lastError: null,
getPlatformInfo: (cb) => {
if (typeof cb !== "function") {
throw new Error(cb + " is not a callback");
}
cb(fakePlatformInfo);
},
},
};
return setupTestDOMWindow(undefined, fakeBrowser).then(window => {
console.log(window.browser.runtime.getPlatformInfo());
notEqual(window.browser, fakeBrowser,
"The existing browser has been wrapped");
return Promise.all([
window,
window.browser.runtime.getPlatformInfo(),
]);
}).then(([window, platformInfo]) => {
deepEqual(platformInfo, fakePlatformInfo,
"The wrapped browser returns the expected value");
});
});
describe("browser wrapper", () => {
it("supports custom properties defined using Object.defineProperty", () => {
const fakeChrome = {};
return setupTestDOMWindow(fakeChrome).then(window => {
Object.defineProperty(window.browser, "myns", {
enumerable: true,
configurable: true,
value: {mykey: true},
});
ok("myns" in window.browser, "The custom property exists");
ok("mykey" in window.browser.myns,
"The content of the custom property exists");
deepEqual(window.browser.myns, {mykey: true},
"The custom property has the expected content");
delete window.browser.myns;
ok(!("myns" in window.browser),
"The deleted custom defined property has been removed");
});
});
it("returns undefined for property undefined in the target", () => {
const fakeChrome = {myns: {mychromekey: true}};
return setupTestDOMWindow(fakeChrome).then(window => {
equal(window.browser.myns.mychromekey, true,
"Got the expected result from a wrapped property");
equal(window.browser.myns.nonexistent, undefined,
"Got undefined for non existent property");
equal(window.browser.nonexistent, undefined,
"Got undefined for non existent namespaces");
});
});
});
});