2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const noop = function () {};
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-07-21 05:46:22 +02:00
|
|
|
class Event {
|
|
|
|
constructor(type, props) {
|
2020-12-11 04:17:28 +01:00
|
|
|
if (!(this instanceof Event)) {
|
|
|
|
return new Event(type, props);
|
|
|
|
}
|
2020-07-21 05:46:22 +02:00
|
|
|
this.type = type;
|
|
|
|
Object.assign(this, props);
|
|
|
|
}
|
|
|
|
preventDefault() {}
|
|
|
|
stopPropagation() {}
|
|
|
|
}
|
|
|
|
|
2021-02-22 14:34:22 +01:00
|
|
|
function verify_selector_for_zulip(selector) {
|
|
|
|
const is_valid =
|
|
|
|
"<#.".includes(selector[0]) ||
|
|
|
|
selector === "window-stub" ||
|
|
|
|
selector === "document-stub" ||
|
|
|
|
selector === "body" ||
|
|
|
|
selector === "html" ||
|
|
|
|
selector.location ||
|
|
|
|
selector.includes("#") ||
|
|
|
|
selector.includes(".") ||
|
|
|
|
(selector.includes("[") && selector.indexOf("]") >= selector.indexOf("["));
|
|
|
|
|
|
|
|
if (!is_valid) {
|
2021-02-22 14:37:49 +01:00
|
|
|
// Check if selector has only english alphabets and space.
|
|
|
|
// Then, the user is probably trying to use a tag as a selector
|
|
|
|
// like $('div a').
|
|
|
|
if (/^[ A-Za-z]+$/.test(selector)) {
|
|
|
|
throw new Error("Selector too broad! Use id, class or attributes of target instead.");
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid selector: " + selector + " Use $.create() maybe?");
|
|
|
|
}
|
2021-02-22 14:34:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 15:38:51 +01:00
|
|
|
function make_event_store(selector) {
|
2018-04-16 21:45:29 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
This function returns an event_store object that
|
|
|
|
simulates the behavior of .on and .off from jQuery.
|
|
|
|
|
|
|
|
It also has methods to retrieve handlers that have
|
|
|
|
been set via .on (or similar methods), which can
|
|
|
|
be useful for tests that want to test the actual
|
|
|
|
handlers.
|
|
|
|
|
|
|
|
*/
|
2019-12-29 13:23:46 +01:00
|
|
|
const on_functions = new Map();
|
|
|
|
const child_on_functions = new Map();
|
2020-07-21 06:16:05 +02:00
|
|
|
let focused = false;
|
2017-05-25 01:49:29 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {
|
2020-07-20 22:18:43 +02:00
|
|
|
get_on_handler(name, child_selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let handler;
|
2018-04-16 21:45:29 +02:00
|
|
|
|
|
|
|
if (child_selector === undefined) {
|
2019-04-18 19:28:25 +02:00
|
|
|
handler = on_functions.get(name);
|
|
|
|
if (!handler) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("no " + name + " handler for " + selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
return handler;
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const child_on = child_on_functions.get(child_selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
if (child_on) {
|
|
|
|
handler = child_on.get(name);
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 19:28:25 +02:00
|
|
|
if (!handler) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("no " + name + " handler for " + selector + " " + child_selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
off(event_name, ...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 0) {
|
2019-12-29 13:23:46 +01:00
|
|
|
on_functions.delete(event_name);
|
2019-04-18 19:28:25 +02:00
|
|
|
return;
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
2019-04-18 19:28:25 +02:00
|
|
|
|
|
|
|
// In the Zulip codebase we never use this form of
|
|
|
|
// .off in code that we test: $(...).off('click', child_sel);
|
|
|
|
//
|
|
|
|
// So we don't support this for now.
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("zjquery does not support this call sequence");
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
on(event_name, ...args) {
|
2018-04-16 21:45:29 +02:00
|
|
|
// parameters will either be
|
|
|
|
// (event_name, handler) or
|
|
|
|
// (event_name, sel, handler)
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 1) {
|
|
|
|
const [handler] = args;
|
2019-04-18 19:28:25 +02:00
|
|
|
if (on_functions.has(event_name)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
console.info("\nEither the app or the test can be at fault here..");
|
|
|
|
console.info("(sometimes you just want to call $.clear_all_elements();)\n");
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("dup " + event_name + " handler for " + selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on_functions.set(event_name, handler);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length !== 2) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("wrong number of arguments passed in");
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
const [sel, handler] = args;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof sel, "string", "String selectors expected here.");
|
|
|
|
assert.equal(typeof handler, "function", "An handler function expected here.");
|
2019-12-29 13:23:46 +01:00
|
|
|
|
|
|
|
if (!child_on_functions.has(sel)) {
|
|
|
|
child_on_functions.set(sel, new Map());
|
|
|
|
}
|
|
|
|
|
|
|
|
const child_on = child_on_functions.get(sel);
|
2019-04-18 19:28:25 +02:00
|
|
|
|
|
|
|
if (child_on.has(event_name)) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("dup " + event_name + " handler for " + selector + " " + sel);
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
2019-04-18 19:28:25 +02:00
|
|
|
|
|
|
|
child_on.set(event_name, handler);
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
one(event_name, handler) {
|
|
|
|
self.on(event_name, function (ev) {
|
|
|
|
self.off(event_name);
|
|
|
|
return handler.call(this, ev);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
trigger($element, ev, data) {
|
2020-07-21 05:46:22 +02:00
|
|
|
if (typeof ev === "string") {
|
2020-12-11 04:07:01 +01:00
|
|
|
ev = new Event(ev);
|
|
|
|
}
|
|
|
|
if (!ev.target) {
|
|
|
|
ev.target = $element;
|
2020-07-21 05:46:22 +02:00
|
|
|
}
|
|
|
|
const func = on_functions.get(ev.type);
|
2019-04-18 19:28:25 +02:00
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
if (func) {
|
2019-04-18 19:28:25 +02:00
|
|
|
// It's possible that test code will trigger events
|
|
|
|
// that haven't been set up yet, but we are trying to
|
|
|
|
// eventually deprecate trigger in our codebase, so for
|
|
|
|
// now we just let calls to trigger silently do nothing.
|
|
|
|
// (And I think actual jQuery would do the same thing.)
|
2020-07-21 06:16:05 +02:00
|
|
|
func.call($element, ev, data);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
if (ev.type === "focus" || ev.type === "focusin") {
|
|
|
|
focused = true;
|
|
|
|
} else if (ev.type === "blur" || ev.type === "focusout") {
|
|
|
|
focused = false;
|
|
|
|
}
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
2020-07-21 08:14:03 +02:00
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
is_focused() {
|
|
|
|
return focused;
|
|
|
|
},
|
2018-04-16 21:45:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
2021-02-21 15:38:51 +01:00
|
|
|
}
|
2018-04-16 21:45:29 +02:00
|
|
|
|
2021-02-21 15:38:51 +01:00
|
|
|
function make_new_elem(selector, opts) {
|
2020-07-15 01:29:15 +02:00
|
|
|
let html = "never-been-set";
|
|
|
|
let text = "never-been-set";
|
2019-11-02 00:06:25 +01:00
|
|
|
let value;
|
|
|
|
let shown = false;
|
2021-02-08 16:27:29 +01:00
|
|
|
let height;
|
|
|
|
|
2019-12-29 13:23:46 +01:00
|
|
|
const find_results = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
let my_parent;
|
2019-12-29 13:23:46 +01:00
|
|
|
const parents_result = new Map();
|
|
|
|
const properties = new Map();
|
|
|
|
const attrs = new Map();
|
|
|
|
const classes = new Map();
|
2021-02-21 15:38:51 +01:00
|
|
|
const event_store = make_event_store(selector);
|
2018-04-16 21:45:29 +02:00
|
|
|
|
2019-10-26 00:26:37 +02:00
|
|
|
const self = {
|
2020-07-20 22:18:43 +02:00
|
|
|
addClass(class_name) {
|
2018-04-16 20:43:55 +02:00
|
|
|
classes.set(class_name, true);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
append(arg) {
|
2020-04-04 22:14:34 +02:00
|
|
|
html = html + arg;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
attr(name, val) {
|
2018-04-16 20:43:55 +02:00
|
|
|
if (val === undefined) {
|
|
|
|
return attrs.get(name);
|
|
|
|
}
|
|
|
|
attrs.set(name, val);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
data(name, val) {
|
2020-01-03 17:10:59 +01:00
|
|
|
if (val === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return attrs.get("data-" + name);
|
2020-01-03 17:10:59 +01:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
attrs.set("data-" + name, val);
|
2020-01-03 17:10:59 +01:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
delay() {
|
2018-08-22 00:31:48 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
debug() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
value,
|
|
|
|
shown,
|
|
|
|
selector,
|
2018-04-16 20:43:55 +02:00
|
|
|
};
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
empty(arg) {
|
2018-07-03 11:03:24 +02:00
|
|
|
if (arg === undefined) {
|
|
|
|
find_results.clear();
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
eq() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
expectOne() {
|
2018-04-16 20:43:55 +02:00
|
|
|
// silently do nothing
|
|
|
|
return self;
|
|
|
|
},
|
|
|
|
fadeTo: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
find(child_selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const child = find_results.get(child_selector);
|
2018-04-16 20:43:55 +02:00
|
|
|
if (child) {
|
|
|
|
return child;
|
|
|
|
}
|
2020-05-21 05:19:57 +02:00
|
|
|
if (child === false) {
|
|
|
|
// This is deliberately set to simulate missing find results.
|
|
|
|
// Return an empty array, the most common check is
|
|
|
|
// if ($.find().length) { //success }
|
|
|
|
return [];
|
|
|
|
}
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Cannot find " + child_selector + " in " + selector);
|
2018-04-16 20:43:55 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
get_on_handler(name, child_selector) {
|
2018-04-16 21:45:29 +02:00
|
|
|
return event_store.get_on_handler(name, child_selector);
|
2018-04-16 20:43:55 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
hasClass(class_name) {
|
2018-04-16 20:43:55 +02:00
|
|
|
return classes.has(class_name);
|
|
|
|
},
|
2021-02-08 16:27:29 +01:00
|
|
|
height() {
|
|
|
|
if (height === undefined) {
|
|
|
|
throw new Error(`Please call $("${selector}").set_height`);
|
|
|
|
}
|
|
|
|
return height;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
hide() {
|
2018-04-16 20:43:55 +02:00
|
|
|
shown = false;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
html(arg) {
|
2018-04-16 20:43:55 +02:00
|
|
|
if (arg !== undefined) {
|
|
|
|
html = arg;
|
2017-07-08 17:41:53 +02:00
|
|
|
return self;
|
2018-04-16 20:43:55 +02:00
|
|
|
}
|
|
|
|
return html;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
is(arg) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (arg === ":visible") {
|
2018-04-16 20:43:55 +02:00
|
|
|
return shown;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (arg === ":focus") {
|
2020-07-21 06:16:05 +02:00
|
|
|
return self.is_focused();
|
2018-04-21 14:59:03 +02:00
|
|
|
}
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
is_focused() {
|
2018-04-16 20:43:55 +02:00
|
|
|
// is_focused is not a jQuery thing; this is
|
|
|
|
// for our testing
|
2020-07-21 06:16:05 +02:00
|
|
|
return event_store.is_focused();
|
2018-04-16 20:43:55 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
off(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
event_store.off(...args);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2021-02-09 13:08:21 +01:00
|
|
|
offset() {
|
|
|
|
return {
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
};
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
event_store.on(...args);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
one(...args) {
|
2020-07-21 06:16:05 +02:00
|
|
|
event_store.one(...args);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
parent() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return my_parent;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
parents(parents_selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = parents_result.get(parents_selector);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2020-07-15 00:34:28 +02:00
|
|
|
result,
|
|
|
|
"You need to call set_parents_result for " + parents_selector + " in " + selector,
|
|
|
|
);
|
2018-04-16 20:43:55 +02:00
|
|
|
return result;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
prepend(arg) {
|
2020-04-04 22:14:34 +02:00
|
|
|
html = arg + html;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
prop(name, val) {
|
2018-04-16 20:43:55 +02:00
|
|
|
if (val === undefined) {
|
|
|
|
return properties.get(name);
|
|
|
|
}
|
|
|
|
properties.set(name, val);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
removeAttr(name) {
|
2019-12-29 13:23:46 +01:00
|
|
|
attrs.delete(name);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
removeClass(class_names) {
|
2020-07-15 01:29:15 +02:00
|
|
|
class_names = class_names.split(" ");
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const class_name of class_names) {
|
2019-12-29 13:23:46 +01:00
|
|
|
classes.delete(class_name);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
remove() {
|
2021-02-25 16:07:04 +01:00
|
|
|
throw new Error(`
|
|
|
|
We don't support remove in zjuery.
|
|
|
|
|
|
|
|
You can do $(...).remove = ... if necessary.
|
|
|
|
|
|
|
|
But you are probably writing too deep a test
|
|
|
|
for node testing.
|
|
|
|
`);
|
2018-04-16 20:43:55 +02:00
|
|
|
},
|
|
|
|
removeData: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
replaceWith() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
scrollTop() {
|
2019-02-27 12:52:05 +01:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
serializeArray() {
|
2019-02-27 12:52:05 +01:00
|
|
|
return self;
|
|
|
|
},
|
2021-02-09 13:08:21 +01:00
|
|
|
set_find_results(find_selector, jquery_object) {
|
|
|
|
find_results.set(find_selector, jquery_object);
|
|
|
|
},
|
2021-02-08 16:27:29 +01:00
|
|
|
set_height(fake_height) {
|
|
|
|
height = fake_height;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
set_parent(parent_elem) {
|
2018-04-16 20:43:55 +02:00
|
|
|
my_parent = parent_elem;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
set_parents_result(selector, result) {
|
2018-04-16 20:43:55 +02:00
|
|
|
parents_result.set(selector, result);
|
|
|
|
},
|
2021-02-09 13:08:21 +01:00
|
|
|
show() {
|
|
|
|
shown = true;
|
|
|
|
return self;
|
|
|
|
},
|
|
|
|
slice() {
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
stop() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
text(...args) {
|
2020-02-12 11:27:02 +01:00
|
|
|
if (args.length !== 0) {
|
|
|
|
if (args[0] !== undefined) {
|
|
|
|
text = args[0].toString();
|
|
|
|
}
|
2018-03-25 15:28:49 +02:00
|
|
|
return self;
|
2018-04-16 20:43:55 +02:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
},
|
2021-02-09 13:21:48 +01:00
|
|
|
toggle(show) {
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok([true, false].includes(show));
|
2021-02-09 13:21:48 +01:00
|
|
|
shown = show;
|
|
|
|
return self;
|
|
|
|
},
|
2020-12-01 23:21:38 +01:00
|
|
|
tooltip() {
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
trigger(ev) {
|
2020-07-21 07:32:54 +02:00
|
|
|
event_store.trigger(self, ev);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
val(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return value || "";
|
2018-04-16 20:43:55 +02:00
|
|
|
}
|
2020-02-12 01:35:16 +01:00
|
|
|
[value] = args;
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
visible() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return shown;
|
|
|
|
},
|
|
|
|
};
|
2017-07-06 00:18:15 +02:00
|
|
|
|
2021-02-08 15:48:50 +01:00
|
|
|
if (opts.children) {
|
|
|
|
self.map = (f) => opts.children.map((i, elem) => f(elem, i));
|
|
|
|
self.each = (f) => {
|
|
|
|
for (const child of opts.children) {
|
|
|
|
f.call(child);
|
|
|
|
}
|
|
|
|
};
|
2021-02-22 17:01:29 +01:00
|
|
|
self[Symbol.iterator] = function* () {
|
|
|
|
for (const child of opts.children) {
|
|
|
|
yield child;
|
|
|
|
}
|
|
|
|
};
|
2021-05-18 21:06:03 +02:00
|
|
|
|
|
|
|
for (const [i, child] of opts.children.entries()) {
|
|
|
|
self[i] = child;
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
self.length = opts.children.length;
|
2021-02-08 15:48:50 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (selector[0] === "<") {
|
2018-04-16 20:43:55 +02:00
|
|
|
self.html(selector);
|
|
|
|
}
|
2017-07-06 15:47:25 +02:00
|
|
|
|
2018-04-16 20:43:55 +02:00
|
|
|
self.selector = selector;
|
|
|
|
|
2021-05-26 13:55:36 +02:00
|
|
|
self.__zjquery = true;
|
|
|
|
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
2021-02-21 15:38:51 +01:00
|
|
|
}
|
2018-04-16 20:43:55 +02:00
|
|
|
|
2021-02-21 15:38:51 +01:00
|
|
|
function make_zjquery() {
|
2020-02-12 08:35:40 +01:00
|
|
|
const elems = new Map();
|
2018-04-16 20:43:55 +02:00
|
|
|
|
|
|
|
// Our fn structure helps us simulate extending jQuery.
|
2021-02-22 13:55:34 +01:00
|
|
|
// Use this with extreme caution.
|
2019-11-02 00:06:25 +01:00
|
|
|
const fn = {};
|
2017-05-25 01:49:29 +02:00
|
|
|
|
2021-02-08 15:48:50 +01:00
|
|
|
function new_elem(selector, create_opts) {
|
2021-02-21 15:38:51 +01:00
|
|
|
const elem = make_new_elem(selector, {...create_opts});
|
2020-02-06 03:52:12 +01:00
|
|
|
Object.assign(elem, fn);
|
2019-04-18 21:11:30 +02:00
|
|
|
|
|
|
|
// Create a proxy handler to detect missing stubs.
|
|
|
|
//
|
|
|
|
// For context, zjquery doesn't implement every method/attribute
|
|
|
|
// that you'd find on a "real" jQuery object. Sometimes we
|
|
|
|
// expects devs to create their own stubs.
|
2019-11-02 00:06:25 +01:00
|
|
|
const handler = {
|
2019-04-18 21:11:30 +02:00
|
|
|
get: (target, key) => {
|
|
|
|
// Handle the special case of equality checks, which
|
|
|
|
// we can infer by assert.equal trying to access the
|
|
|
|
// "stack" key.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (key === "stack") {
|
2020-07-15 00:34:28 +02:00
|
|
|
const error =
|
|
|
|
"\nInstead of doing equality checks on a full object, " +
|
2019-04-18 21:11:30 +02:00
|
|
|
'do `assert_equal(foo.selector, ".some_class")\n';
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(error);
|
2019-04-18 21:11:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const val = target[key];
|
|
|
|
|
2020-09-24 07:50:36 +02:00
|
|
|
if (val === undefined && typeof key !== "symbol" && key !== "inspect") {
|
2019-04-18 21:11:30 +02:00
|
|
|
// For undefined values, we'll throw errors to devs saying
|
|
|
|
// they need to create stubs. We ignore certain keys that
|
|
|
|
// are used for simply printing out the object.
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error('You must create a stub for $("' + selector + '").' + key);
|
2019-04-18 21:11:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const proxy = new Proxy(elem, handler);
|
2019-04-18 21:11:30 +02:00
|
|
|
|
|
|
|
return proxy;
|
2017-05-25 01:49:29 +02:00
|
|
|
}
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2021-03-13 15:49:01 +01:00
|
|
|
let initialize_function;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const zjquery = function (arg, arg2) {
|
2017-05-25 01:49:29 +02:00
|
|
|
if (typeof arg === "function") {
|
2021-03-13 15:49:01 +01:00
|
|
|
if (initialize_function) {
|
|
|
|
throw new Error(`
|
|
|
|
We are trying to avoid the $(...) mechanism
|
|
|
|
for initializing modules in our codebase,
|
|
|
|
and the code that you are compiling/running
|
|
|
|
has tried to do this twice. Please either
|
|
|
|
clean up the real code or reduce the scope
|
|
|
|
of what you are testing in this test module.
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
initialize_function = arg;
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-04-12 18:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If somebody is passing us an element, we return
|
|
|
|
// the element itself if it's been created with
|
|
|
|
// zjquery.
|
|
|
|
// This may happen in cases like $(this).
|
2020-02-12 08:35:40 +01:00
|
|
|
if (arg.selector && elems.has(arg.selector)) {
|
|
|
|
return arg;
|
2017-05-25 01:49:29 +02:00
|
|
|
}
|
2017-05-24 03:34:18 +02:00
|
|
|
|
2018-04-12 18:06:39 +02:00
|
|
|
// We occasionally create stub objects that know
|
|
|
|
// they want to be wrapped by jQuery (so they can
|
|
|
|
// in turn return stubs). The convention is that
|
|
|
|
// they provide a to_$ attribute.
|
|
|
|
if (arg.to_$) {
|
2021-06-08 05:08:12 +02:00
|
|
|
assert.equal(typeof arg.to_$, "function");
|
2018-04-12 18:06:39 +02:00
|
|
|
return arg.to_$();
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:46:45 +02:00
|
|
|
if (arg2 !== undefined) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("We only use one-argument variations of $(...) in Zulip code.");
|
2018-04-05 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const selector = arg;
|
2017-07-08 15:16:19 +02:00
|
|
|
|
2018-04-12 18:06:39 +02:00
|
|
|
if (typeof selector !== "string") {
|
|
|
|
console.info(arg);
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("zjquery does not know how to wrap this object yet");
|
2018-04-12 18:06:39 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:34:22 +01:00
|
|
|
verify_selector_for_zulip(selector);
|
2017-07-08 15:16:19 +02:00
|
|
|
|
2020-02-12 08:35:40 +01:00
|
|
|
if (!elems.has(selector)) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = new_elem(selector);
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.set(selector, elem);
|
2017-05-24 03:34:18 +02:00
|
|
|
}
|
2020-02-12 08:35:40 +01:00
|
|
|
return elems.get(selector);
|
2017-05-25 01:49:29 +02:00
|
|
|
};
|
2017-05-24 03:34:18 +02:00
|
|
|
|
2021-03-13 15:49:01 +01:00
|
|
|
zjquery.get_initialize_function = function () {
|
|
|
|
return initialize_function;
|
|
|
|
};
|
|
|
|
|
|
|
|
zjquery.clear_initialize_function = function () {
|
|
|
|
initialize_function = undefined;
|
|
|
|
};
|
|
|
|
|
2021-02-08 15:48:50 +01:00
|
|
|
zjquery.create = function (name, opts) {
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!elems.has(name), "You already created an object with this name!!");
|
2021-02-08 15:48:50 +01:00
|
|
|
const elem = new_elem(name, opts);
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.set(name, elem);
|
2021-02-08 15:48:50 +01:00
|
|
|
|
2020-02-12 08:35:40 +01:00
|
|
|
return elem;
|
2017-07-08 15:16:19 +02:00
|
|
|
};
|
2017-05-24 03:05:29 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
zjquery.trim = function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
zjquery.state = function () {
|
|
|
|
// useful for debugging
|
2020-07-02 01:39:34 +02:00
|
|
|
let res = Array.from(elems.values(), (v) => v.debug());
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
res = res.map((v) => [v.selector, v.value, v.shown]);
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
res.sort();
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
return res;
|
|
|
|
};
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-12-11 04:17:28 +01:00
|
|
|
zjquery.Event = Event;
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2021-02-22 13:55:34 +01:00
|
|
|
fn.popover = () => {
|
|
|
|
throw new Error(`
|
|
|
|
Do not try to test $.fn.popover code unless
|
|
|
|
you really know what you are doing.
|
|
|
|
`);
|
|
|
|
};
|
|
|
|
|
|
|
|
zjquery.fn = new Proxy(fn, {
|
|
|
|
set(obj, prop, value) {
|
|
|
|
if (prop === "popover") {
|
|
|
|
// We allow our popovers test to modify
|
|
|
|
// $.fn so we can bypass a gruesome hack
|
|
|
|
// in our popovers.js module.
|
|
|
|
obj[prop] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`
|
|
|
|
Please don't use node tests to test code
|
|
|
|
that extends $.fn unless you really know
|
|
|
|
what you are doing.
|
|
|
|
|
|
|
|
It's likely that you are better off testing
|
|
|
|
end-to-end behavior with puppeteer tests.
|
|
|
|
|
|
|
|
If you are trying to get coverage on a module
|
|
|
|
that extends $.fn, and you just want to skip
|
|
|
|
over that aspect of the module for the purpose
|
|
|
|
of testing, see if you can wrap the code
|
|
|
|
that extends $.fn and use override() to
|
|
|
|
replace the wrapper with () => {}.
|
|
|
|
`);
|
|
|
|
},
|
|
|
|
});
|
2018-04-12 14:45:06 +02:00
|
|
|
|
2018-07-05 14:58:14 +02:00
|
|
|
zjquery.clear_all_elements = function () {
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.clear();
|
2018-07-05 14:58:14 +02:00
|
|
|
};
|
|
|
|
|
2021-02-22 13:10:25 +01:00
|
|
|
zjquery.validator = {
|
|
|
|
addMethod() {
|
|
|
|
throw new Error("You must create your own $.validator.addMethod stub.");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
return zjquery;
|
2021-02-21 15:38:51 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 12:18:06 +01:00
|
|
|
const $ = new Proxy(make_zjquery(), {
|
|
|
|
set(obj, prop, value) {
|
2021-03-11 18:01:52 +01:00
|
|
|
if (obj[prop] && obj[prop]._patched_with_override) {
|
|
|
|
obj[prop] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-22 12:18:06 +01:00
|
|
|
if (value._patched_with_override) {
|
|
|
|
obj[prop] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`
|
|
|
|
Please don't modify $.${prop} if you are using zjquery.
|
|
|
|
|
|
|
|
You can do this instead:
|
|
|
|
|
|
|
|
override($, "${prop}", () => {...});
|
|
|
|
|
|
|
|
Or you can do this if you don't actually
|
|
|
|
need zjquery and just want to simulate one function.
|
|
|
|
|
2021-04-23 20:16:42 +02:00
|
|
|
mock_cjs("jquery", {
|
2021-02-22 12:18:06 +01:00
|
|
|
${prop}(...) {...},
|
|
|
|
});
|
|
|
|
|
|
|
|
It's also possible that you are testing code with
|
|
|
|
node tests when it would be a better strategy to
|
|
|
|
use puppeteer tests.
|
|
|
|
`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = $;
|