zulip/frontend_tests/zjsunit/zjquery.js

233 lines
6.4 KiB
JavaScript
Raw Normal View History

var noop = function () {};
var exports = {};
2017-05-25 01:49:29 +02:00
exports.make_zjquery = function () {
var elems = {};
function new_elem(selector) {
var html = 'never-been-set';
var text = 'never-been-set';
var value;
var shown = false;
var focused = false;
var children = new Dict();
var my_parent;
2017-06-16 14:45:02 +02:00
var properties = new Dict();
2017-06-16 20:55:07 +02:00
var attrs = new Dict();
2017-05-25 01:49:29 +02:00
var classes = new Dict();
2017-06-14 20:28:55 +02:00
var on_functions = new Dict();
function generic_event(event_name, arg) {
if (typeof(arg) === 'function') {
on_functions.set(event_name, arg);
} else {
var handler = on_functions.get(event_name);
assert(handler);
handler(arg);
}
}
2017-05-25 01:49:29 +02:00
var self = {
val: function () {
if (arguments.length === 0) {
return value || '';
}
value = arguments[0];
},
css: noop,
data: noop,
empty: noop,
height: noop,
2017-06-16 20:55:07 +02:00
removeAttr: function (name) {
attrs.del(name);
},
2017-05-25 01:49:29 +02:00
removeData: noop,
2017-06-14 20:28:55 +02:00
trigger: function (ev) {
var funcs = on_functions.get(ev.name) || [];
_.each(funcs, function (f) {
f(ev.data);
});
},
2017-05-25 01:49:29 +02:00
blur: function () {
focused = false;
},
html: function (arg) {
if (arg !== undefined) {
html = arg;
} else {
return html;
}
},
text: function (arg) {
if (arg !== undefined) {
text = arg;
} else {
return text;
}
},
focus: function () {
focused = true;
},
show: function () {
shown = true;
},
hide: function () {
shown = false;
},
addClass: function (class_name) {
classes.set(class_name, true);
return self.wrapper;
2017-05-25 01:49:29 +02:00
},
removeClass: function (class_name) {
classes.del(class_name);
return self.wrapper;
2017-05-25 01:49:29 +02:00
},
hasClass: function (class_name) {
return classes.has(class_name);
},
debug: function () {
return {
value: value,
shown: shown,
selector: selector,
};
},
visible: function () {
return shown;
},
is_focused: function () {
// is_focused is not a jQuery thing; this is
// for our testing
return focused;
},
find: function (child_selector) {
var child = children.get(child_selector);
if (child) {
return child;
}
throw Error("Cannot find " + child_selector + " in " + selector);
2017-05-25 01:49:29 +02:00
},
add_child: function (child_selector, child_elem) {
child_elem.set_parent(self);
children.set(child_selector, child_elem);
},
remove_child: function (child_selector) {
children.del(child_selector);
},
set_parent: function (parent_elem) {
my_parent = parent_elem;
},
parent: function () {
return my_parent;
},
expectOne: function () {
// silently do nothing
return self.wrapper;
2017-05-25 01:49:29 +02:00
},
2017-06-14 20:28:55 +02:00
on: function (name, f) {
var funcs = on_functions.setdefault(name, []);
funcs.push(f);
return self.wrapper;
2017-05-25 01:49:29 +02:00
},
click: function (arg) {
generic_event('click', arg);
2017-06-20 00:31:00 +02:00
},
2017-05-25 01:49:29 +02:00
remove: function () {
if (my_parent) {
my_parent.remove_child(selector);
}
},
2017-06-14 21:08:32 +02:00
get: function (idx) {
// We have some legacy code that does $('foo').get(0).
assert.equal(idx, 0);
return selector;
},
2017-06-16 20:55:07 +02:00
attr: function (name, val) {
if (val === undefined) {
return attrs.get(name);
}
attrs.set(name, val);
},
2017-06-16 14:45:02 +02:00
prop: function (name, val) {
if (val === undefined) {
return properties.get(name);
}
properties.set(name, val);
},
2017-05-25 01:49:29 +02:00
};
2017-06-14 14:17:03 +02:00
if (selector[0] === '<') {
self.html(selector);
}
2017-05-25 01:49:29 +02:00
return self;
}
function jquery_array(elem) {
var result = [elem];
for (var attr in elem) {
if (Object.prototype.hasOwnProperty.call(elem, attr)) {
result[attr] = elem[attr];
2017-05-24 19:46:25 +02:00
}
2017-05-25 01:49:29 +02:00
}
elem.wrapper = result;
2017-05-24 19:46:25 +02:00
2017-05-25 01:49:29 +02:00
return result;
}
2017-05-25 01:49:29 +02:00
var zjquery = function (arg) {
if (typeof arg === "function") {
// If somebody is passing us a function, we emulate
// jQuery's behavior of running this function after
// page load time. But there are no pages to load,
// so we just call it right away.
arg();
return;
}
2017-05-25 01:49:29 +02:00
var selector = arg;
if (elems[selector] === undefined) {
var elem = new_elem(selector);
elems[selector] = jquery_array(elem);
}
2017-05-25 01:49:29 +02:00
return elems[selector];
};
2017-05-25 01:49:29 +02:00
zjquery.stub_selector = function (selector, stub) {
elems[selector] = stub;
};
2017-05-24 22:31:12 +02:00
2017-05-25 01:49:29 +02:00
zjquery.trim = function (s) { return s; };
2017-05-25 01:49:29 +02:00
zjquery.state = function () {
// useful for debugging
var res = _.map(elems, function (v) {
return v.debug();
});
2017-05-25 01:49:29 +02:00
res = _.map(res, function (v) {
return [v.selector, v.value, v.shown];
});
2017-05-25 01:49:29 +02:00
res.sort();
2017-05-25 01:49:29 +02:00
return res;
};
2017-06-14 20:28:55 +02:00
zjquery.Event = function (name, data) {
return {
name: name,
data: data,
};
};
2017-05-25 01:49:29 +02:00
return zjquery;
};
2017-05-25 01:49:29 +02:00
module.exports = exports;