/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
// Map of mocked modules, keys are absolute URIs for devtools modules such as // "resource://devtools/path/to/mod.js, values are objects (anything passed to // setMockedModule technically). const _mocks = {};
/** *Modulepathsaretransparentlyprovidedwithorwithout".js"whenusingtheloader, *normalizetheuser-providedmodulepathstoalwayshavemodulesendingwith".js".
*/ function _getUriForModulePath(modulePath) { // Assume js modules and add the .js extension if missing. if (!modulePath.endsWith(".js")) {
modulePath = modulePath + ".js";
}
// Add resource:// scheme if no scheme is specified. if (!modulePath.includes("://")) {
modulePath = "resource://" + modulePath;
}
return modulePath;
}
/** *Assignamockobjecttotheprovidedmodulepath. * *@parammock *PlainJavaScriptobjectthatwillimplementtheexpectedAPIforthemocked *module. *@parammodulePath *Themodulepathshouldbetheabsolutemodulepath,startingwith`devtools`: *"devtools/client/some-panel/some-module"
*/ function setMockedModule(mock, modulePath) { const uri = _getUriForModulePath(modulePath);
_mocks[uri] = new Proxy(mock, {
get(target, key) { if (typeof target[key] === "function") { // Functions are wrapped to be able to update the methods during the test, even if // the methods were imported with destructuring. For instance: // `const { someMethod } = require("devtools/client/shared/my-module");` returnfunction () { return target[key].apply(target, arguments);
};
} return target[key];
},
});
}
exports.setMockedModule = setMockedModule;
/** *Removeanymockobjectdefinedfortheprovidedabsolutemodulepath.
*/ function removeMockedModule(modulePath) { const uri = _getUriForModulePath(modulePath); delete _mocks[uri];
}
exports.removeMockedModule = removeMockedModule;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.