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
|
/* 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";
// 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 = {};
/**
* Retrieve a mocked module matching the provided uri, eg "resource://path/to/file.js".
*/
function getMockedModule(uri) {
return _mocks[uri];
}
exports.getMockedModule = getMockedModule;
/**
* Module paths are transparently provided with or without ".js" when using the loader,
* normalize the user-provided module paths to always have modules ending with ".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;
}
/**
* Assign a mock object to the provided module path.
* @param mock
* Plain JavaScript object that will implement the expected API for the mocked
* module.
* @param modulePath
* The module path should be the absolute module path, starting with `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");`
return function() {
return target[key].apply(target, arguments);
};
}
return target[key];
},
});
}
exports.setMockedModule = setMockedModule;
/**
* Remove any mock object defined for the provided absolute module path.
*/
function removeMockedModule(modulePath) {
const uri = _getUriForModulePath(modulePath);
delete _mocks[uri];
}
exports.removeMockedModule = removeMockedModule;
|