Bug 1811262 - [devtools] Add jest fixture for devtools/shared/indexed-db.js. r=jdescottes.

Differential Revision: https://phabricator.services.mozilla.com/D167350
This commit is contained in:
Nicolas Chevobbe 2023-01-25 08:11:26 +00:00
Родитель 0b3da2309e
Коммит 2495b179a0
4 изменённых файлов: 50 добавлений и 52 удалений

Просмотреть файл

@ -0,0 +1,15 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
"use strict";
const store = {};
module.exports = {
open: () => ({}),
getItem: async key => store[key],
setItem: async (key, value) => {
store[key] = value;
},
};

Просмотреть файл

@ -33,6 +33,7 @@ module.exports = {
// Sometimes returning an empty object is enough // Sometimes returning an empty object is enough
"^resource://devtools/client/shared/link": `${fixturesDir}/empty-module`, "^resource://devtools/client/shared/link": `${fixturesDir}/empty-module`,
"^devtools/shared/flags": `${fixturesDir}/empty-module`, "^devtools/shared/flags": `${fixturesDir}/empty-module`,
"^resource://devtools/shared/indexed-db.js": `${fixturesDir}/indexed-db`,
"^devtools/shared/layout/utils": `${fixturesDir}/empty-module`, "^devtools/shared/layout/utils": `${fixturesDir}/empty-module`,
"^devtools/client/shared/components/tree/TreeView": `${fixturesDir}/empty-module`, "^devtools/client/shared/components/tree/TreeView": `${fixturesDir}/empty-module`,
// Map all require("devtools/...") to the real devtools root. // Map all require("devtools/...") to the real devtools root.

Просмотреть файл

@ -46,10 +46,6 @@ function setMocksInGlobal() {
global.loader = { global.loader = {
lazyGetter: (context, name, fn) => { lazyGetter: (context, name, fn) => {
if (global.hasOwnProperty(name)) {
return;
}
Object.defineProperty(global, name, { Object.defineProperty(global, name, {
get() { get() {
delete global[name]; delete global[name];
@ -119,15 +115,6 @@ function setMocksInGlobal() {
}; };
} }
const store = {};
global.indexedDB = {
open: () => ({}),
getItem: async key => store[key],
setItem: async (key, value) => {
store[key] = value;
},
};
if (typeof global.TextEncoder === "undefined") { if (typeof global.TextEncoder === "undefined") {
const { TextEncoder } = require("util"); const { TextEncoder } = require("util");
global.TextEncoder = TextEncoder; global.TextEncoder = TextEncoder;

Просмотреть файл

@ -9,46 +9,41 @@
* a principal dedicated to DevTools. * a principal dedicated to DevTools.
*/ */
// When running in jest, we can't use Cu.Sandbox and only expose the native indexedDB object const PSEUDOURI = "indexeddb://fx-devtools";
if (globalThis.indexedDB) { const principaluri = Services.io.newURI(PSEUDOURI);
module.exports = globalThis.indexedDB; const principal = Services.scriptSecurityManager.createContentPrincipal(
} else { principaluri,
const PSEUDOURI = "indexeddb://fx-devtools"; {}
const principaluri = Services.io.newURI(PSEUDOURI); );
const principal = Services.scriptSecurityManager.createContentPrincipal(
principaluri,
{}
);
// indexedDB is only exposed to document globals. // indexedDB is only exposed to document globals.
// We are retrieving an instance from a Sandbox, which has to be loaded // We are retrieving an instance from a Sandbox, which has to be loaded
// from the system principal in order to avoid having wrappers around // from the system principal in order to avoid having wrappers around
// all indexed DB objects. // all indexed DB objects.
const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
const sandbox = Cu.Sandbox(systemPrincipal, { const sandbox = Cu.Sandbox(systemPrincipal, {
wantGlobalProperties: ["indexedDB"], wantGlobalProperties: ["indexedDB"],
}); });
const { indexedDB } = sandbox; const { indexedDB } = sandbox;
module.exports = Object.freeze({ module.exports = Object.freeze({
/** /**
* Only the standard version of indexedDB.open is supported. * Only the standard version of indexedDB.open is supported.
*/ */
open(name, version) { open(name, version) {
const options = {}; const options = {};
if (typeof version === "number") { if (typeof version === "number") {
options.version = version; options.version = version;
} }
return indexedDB.openForPrincipal(principal, name, options); return indexedDB.openForPrincipal(principal, name, options);
}, },
/** /**
* Only the standard version of indexedDB.deleteDatabase is supported. * Only the standard version of indexedDB.deleteDatabase is supported.
*/ */
deleteDatabase(name) { deleteDatabase(name) {
return indexedDB.deleteForPrincipal(principal, name); return indexedDB.deleteForPrincipal(principal, name);
}, },
cmp: indexedDB.cmp.bind(indexedDB), cmp: indexedDB.cmp.bind(indexedDB),
}); });
}