2016-07-30 17:00:12 +02:00
|
|
|
var namespace = (function () {
|
|
|
|
|
2019-06-21 03:16:48 +02:00
|
|
|
var _ = require('underscore/underscore.js');
|
2016-07-30 17:00:12 +02:00
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var dependencies = [];
|
2016-07-30 19:34:51 +02:00
|
|
|
var requires = [];
|
2016-07-30 17:00:12 +02:00
|
|
|
var old_builtins = {};
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else if (/generated\/|js\/|third\//.test(fn)) {
|
|
|
|
// 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)];
|
|
|
|
var obj = require(fn);
|
|
|
|
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 = [];
|
|
|
|
_.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.
|
|
|
|
|
|
|
|
var clobber_callbacks = [];
|
|
|
|
|
|
|
|
var override = function (name, f) {
|
|
|
|
var parts = name.split('.');
|
|
|
|
var module = parts[0];
|
|
|
|
var 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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-30 17:00:12 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
module.exports = namespace;
|