2019-11-02 00:06:25 +01:00
|
|
|
const _ = require('underscore/underscore.js');
|
2016-07-30 17:00:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let dependencies = [];
|
|
|
|
const requires = [];
|
|
|
|
let old_builtins = {};
|
2016-07-30 17:00:12 +02:00
|
|
|
|
|
|
|
exports.set_global = function (name, val) {
|
|
|
|
global[name] = val;
|
|
|
|
dependencies.push(name);
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.patch_builtin = function (name, val) {
|
|
|
|
old_builtins[name] = global[name];
|
|
|
|
global[name] = val;
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
2017-08-09 18:26:03 +02:00
|
|
|
exports.zrequire = function (name, fn) {
|
|
|
|
if (fn === undefined) {
|
2019-06-21 01:59:21 +02:00
|
|
|
fn = '../../static/js/' + name;
|
2019-10-04 23:08:11 +02:00
|
|
|
} else if (/^generated\/|^js\/|^shared\/|^third\//.test(fn)) {
|
2019-06-21 01:59:21 +02:00
|
|
|
// FIXME: Stealing part of the NPM namespace is confusing.
|
|
|
|
fn = '../../static/' + fn;
|
2017-08-09 18:26:03 +02:00
|
|
|
}
|
2019-03-24 08:37:53 +01:00
|
|
|
delete require.cache[require.resolve(fn)];
|
2019-11-02 00:06:25 +01:00
|
|
|
const obj = require(fn);
|
2019-03-24 08:37:53 +01:00
|
|
|
requires.push(fn);
|
2017-08-09 18:26:03 +02:00
|
|
|
set_global(name, obj);
|
2017-11-08 17:42:34 +01:00
|
|
|
return obj;
|
2017-08-09 18:26:03 +02:00
|
|
|
};
|
|
|
|
|
2016-07-30 17:00:12 +02:00
|
|
|
exports.restore = function () {
|
|
|
|
dependencies.forEach(function (name) {
|
|
|
|
delete global[name];
|
|
|
|
});
|
2016-07-30 19:34:51 +02:00
|
|
|
requires.forEach(function (fn) {
|
|
|
|
delete require.cache[require.resolve(fn)];
|
|
|
|
});
|
2016-07-30 17:00:12 +02:00
|
|
|
dependencies = [];
|
2019-07-16 02:52:55 +02:00
|
|
|
delete global.window.electron_bridge;
|
2016-07-30 17:00:12 +02:00
|
|
|
_.extend(global, old_builtins);
|
|
|
|
old_builtins = {};
|
|
|
|
};
|
|
|
|
|
2016-07-30 20:01:15 +02:00
|
|
|
exports.stub_out_jquery = function () {
|
|
|
|
set_global('$', function () {
|
|
|
|
return {
|
|
|
|
on: function () {},
|
|
|
|
trigger: function () {},
|
2016-12-03 23:18:44 +01:00
|
|
|
hide: function () {},
|
2018-03-14 00:56:31 +01:00
|
|
|
removeClass: function () {},
|
2016-07-30 20:01:15 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
$.fn = {};
|
|
|
|
$.now = function () {};
|
|
|
|
};
|
|
|
|
|
2017-03-11 21:07:24 +01:00
|
|
|
exports.with_overrides = function (test_function) {
|
|
|
|
// This function calls test_function() and passes in
|
|
|
|
// a way to override the namespace temporarily.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const clobber_callbacks = [];
|
2017-03-11 21:07:24 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const override = function (name, f) {
|
|
|
|
const parts = name.split('.');
|
|
|
|
const module = parts[0];
|
|
|
|
const func_name = parts[1];
|
2017-03-12 14:32:12 +01:00
|
|
|
|
|
|
|
if (!_.has(global, module)) {
|
|
|
|
set_global(module, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
global[module][func_name] = f;
|
2017-03-11 21:07:24 +01:00
|
|
|
|
|
|
|
clobber_callbacks.push(function () {
|
|
|
|
// If you get a failure from this, you probably just
|
|
|
|
// need to have your test do its own overrides and
|
|
|
|
// not cherry-pick off of the prior test's setup.
|
2017-03-12 14:32:12 +01:00
|
|
|
global[module][func_name] =
|
|
|
|
'ATTEMPTED TO REUSE OVERRIDDEN VALUE FROM PRIOR TEST';
|
2017-03-11 21:07:24 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
test_function(override);
|
|
|
|
|
|
|
|
_.each(clobber_callbacks, function (f) {
|
|
|
|
f();
|
|
|
|
});
|
|
|
|
};
|