zjquery: Support $.fn.foo mechanism.

We can now extend zjquery using the $.fn mechanism.  This isn't
necessarily recommended for test code (since you can just stub
individual objects directly), but some of our real code does this.
This commit is contained in:
Steve Howell 2018-04-12 12:45:06 +00:00 committed by Tim Abbott
parent f5e0794d5c
commit f505f3de04
2 changed files with 36 additions and 0 deletions

View File

@ -174,3 +174,26 @@ set_global('$', global.make_zjquery());
obj2.addClass('.striped');
assert(obj2.hasClass('.striped'));
}());
(function test_extensions() {
// You can extend $.fn so that all subsequent objects
// we create get a new function.
$.fn.area = function () {
return this.width() * this.height();
};
// Before we use area, though, let's illustrate that
// the predominant Zulip testing style is to stub objects
// using direct syntax:
var rect = $.create('rectangle');
rect.width = () => { return 5; };
rect.height = () => { return 7; };
assert.equal(rect.width(), 5);
assert.equal(rect.height(), 7);
// But we also have area available from general extension.
assert.equal(rect.area(), 35);
}());

View File

@ -6,6 +6,15 @@ exports.make_zjquery = function () {
var elems = {};
// Our fn structure helps us simulate extending jQuery.
var fn = {};
function add_extensions(obj) {
_.each(fn, (v, k) => {
obj[k] = v;
});
}
function new_elem(selector) {
var html = 'never-been-set';
var text = 'never-been-set';
@ -281,6 +290,8 @@ exports.make_zjquery = function () {
self[0] = 'you-must-set-the-child-yourself';
add_extensions(self);
return self;
}
@ -374,6 +385,8 @@ exports.make_zjquery = function () {
return _.extend(content, container);
};
zjquery.fn = fn;
return zjquery;
};