mirror of https://github.com/zulip/zulip.git
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:
parent
f5e0794d5c
commit
f505f3de04
|
@ -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);
|
||||
}());
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue