mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 20:44:04 +00:00
node tests: Add with_stub() helper.
This commit is contained in:
@@ -19,6 +19,10 @@ global.patch_builtin = namespace.patch_builtin;
|
||||
global.add_dependencies = namespace.add_dependencies;
|
||||
global.stub_out_jquery = namespace.stub_out_jquery;
|
||||
|
||||
// Set up stub helpers.
|
||||
var stub = require('./stub.js');
|
||||
global.with_stub = stub.with_stub;
|
||||
|
||||
// Set up helpers to render templates.
|
||||
var render = require('./render.js');
|
||||
global.make_sure_all_templates_have_been_compiled =
|
||||
|
||||
52
frontend_tests/zjsunit/stub.js
Normal file
52
frontend_tests/zjsunit/stub.js
Normal file
@@ -0,0 +1,52 @@
|
||||
var stub = (function () {
|
||||
|
||||
var _ = require('node_modules/underscore/underscore.js');
|
||||
var exports = {};
|
||||
|
||||
// Stubs don't do any magical modifications to your namespace. They
|
||||
// just provide you a function that records what arguments get passed
|
||||
// to it. To use stubs as something more like "spies," use something
|
||||
// like set_global() to override your namespace.
|
||||
|
||||
exports.make_stub = function () {
|
||||
var self = {};
|
||||
self.num_calls = 0;
|
||||
|
||||
self.f = function () {
|
||||
self.last_call_args = _.clone(arguments);
|
||||
self.num_calls += 1;
|
||||
return true;
|
||||
};
|
||||
|
||||
self.get_args = function () {
|
||||
var param_names = arguments;
|
||||
var result = {};
|
||||
|
||||
_.each(param_names, function (name, i) {
|
||||
result[name] = self.last_call_args[i];
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
return self;
|
||||
};
|
||||
|
||||
exports.with_stub = function (f) {
|
||||
var stub = exports.make_stub();
|
||||
f(stub);
|
||||
assert.equal(stub.num_calls, 1);
|
||||
};
|
||||
|
||||
(function test_ourselves() {
|
||||
exports.with_stub(function (stub) {
|
||||
stub.f('blue', 42);
|
||||
var args = stub.get_args('color', 'n');
|
||||
assert.equal(args.color, 'blue');
|
||||
assert.equal(args.n, 42);
|
||||
});
|
||||
}());
|
||||
|
||||
return exports;
|
||||
}());
|
||||
module.exports = stub;
|
||||
Reference in New Issue
Block a user