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
|
|
|
const requires = [];
|
2019-07-25 09:13:22 +02:00
|
|
|
const new_globals = new Set();
|
|
|
|
let old_globals = {};
|
2016-07-30 17:00:12 +02:00
|
|
|
|
|
|
|
exports.set_global = function (name, val) {
|
2019-07-25 09:13:22 +02:00
|
|
|
if (!(name in old_globals)) {
|
|
|
|
if (!(name in global)) {
|
|
|
|
new_globals.add(name);
|
|
|
|
}
|
|
|
|
old_globals[name] = global[name];
|
|
|
|
}
|
2016-07-30 17:00:12 +02:00
|
|
|
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)];
|
|
|
|
requires.push(fn);
|
2019-07-26 03:47:36 +02:00
|
|
|
return require(fn);
|
2017-08-09 18:26:03 +02:00
|
|
|
};
|
|
|
|
|
2016-07-30 17:00:12 +02:00
|
|
|
exports.restore = function () {
|
2016-07-30 19:34:51 +02:00
|
|
|
requires.forEach(function (fn) {
|
|
|
|
delete require.cache[require.resolve(fn)];
|
|
|
|
});
|
2019-07-25 09:13:22 +02:00
|
|
|
_.extend(global, old_globals);
|
|
|
|
old_globals = {};
|
|
|
|
for (const name of new_globals) {
|
|
|
|
delete global[name];
|
|
|
|
}
|
|
|
|
new_globals.clear();
|
2016-07-30 17:00:12 +02:00
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
};
|