tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser-loader-mocks.js (2376B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 // Map of mocked modules, keys are absolute URIs for devtools modules such as
      8 // "resource://devtools/path/to/mod.js, values are objects (anything passed to
      9 // setMockedModule technically).
     10 const _mocks = {};
     11 
     12 /**
     13 * Retrieve a mocked module matching the provided uri, eg "resource://path/to/file.js".
     14 */
     15 function getMockedModule(uri) {
     16  return _mocks[uri];
     17 }
     18 exports.getMockedModule = getMockedModule;
     19 
     20 /**
     21 * Module paths are transparently provided with or without ".js" when using the loader,
     22 * normalize the user-provided module paths to always have modules ending with ".js".
     23 */
     24 function _getUriForModulePath(modulePath) {
     25  // Assume js modules and add the .js extension if missing.
     26  if (!modulePath.endsWith(".js")) {
     27    modulePath = modulePath + ".js";
     28  }
     29 
     30  // Add resource:// scheme if no scheme is specified.
     31  if (!modulePath.includes("://")) {
     32    modulePath = "resource://" + modulePath;
     33  }
     34 
     35  return modulePath;
     36 }
     37 
     38 /**
     39 * Assign a mock object to the provided module path.
     40 *
     41 * @param mock
     42 *        Plain JavaScript object that will implement the expected API for the mocked
     43 *        module.
     44 * @param modulePath
     45 *        The module path should be the absolute module path, starting with `devtools`:
     46 *        "devtools/client/some-panel/some-module"
     47 */
     48 function setMockedModule(mock, modulePath) {
     49  const uri = _getUriForModulePath(modulePath);
     50  _mocks[uri] = new Proxy(mock, {
     51    get(target, key) {
     52      if (typeof target[key] === "function") {
     53        // Functions are wrapped to be able to update the methods during the test, even if
     54        // the methods were imported with destructuring. For instance:
     55        //   `const { someMethod } = require("devtools/client/shared/my-module");`
     56        return function () {
     57          return target[key].apply(target, arguments);
     58        };
     59      }
     60      return target[key];
     61    },
     62  });
     63 }
     64 exports.setMockedModule = setMockedModule;
     65 
     66 /**
     67 * Remove any mock object defined for the provided absolute module path.
     68 */
     69 function removeMockedModule(modulePath) {
     70  const uri = _getUriForModulePath(modulePath);
     71  delete _mocks[uri];
     72 }
     73 exports.removeMockedModule = removeMockedModule;