2021-06-12 17:50:07 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
|
|
const FakeEvent = require("./zjquery_event");
|
|
|
|
|
|
|
|
const noop = function () {};
|
|
|
|
|
|
|
|
// TODO: convert this to a true class
|
|
|
|
function FakeElement(selector, opts) {
|
|
|
|
let html = "never-been-set";
|
|
|
|
let text = "never-been-set";
|
|
|
|
let value;
|
|
|
|
let shown = false;
|
|
|
|
let height;
|
|
|
|
|
|
|
|
const find_results = new Map();
|
2022-01-25 11:36:19 +01:00
|
|
|
let $my_parent;
|
2021-06-12 17:50:07 +02:00
|
|
|
const parents_result = new Map();
|
|
|
|
const properties = new Map();
|
|
|
|
const attrs = new Map();
|
|
|
|
const classes = new Map();
|
|
|
|
const event_store = make_event_store(selector);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $self = {
|
2021-06-12 17:50:07 +02:00
|
|
|
addClass(class_name) {
|
|
|
|
classes.set(class_name, true);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
append(arg) {
|
|
|
|
html = html + arg;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
attr(name, val) {
|
|
|
|
if (val === undefined) {
|
|
|
|
return attrs.get(name);
|
|
|
|
}
|
|
|
|
attrs.set(name, val);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
data(name, val) {
|
|
|
|
if (val === undefined) {
|
|
|
|
return attrs.get("data-" + name);
|
|
|
|
}
|
|
|
|
attrs.set("data-" + name, val);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
delay() {
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-06-12 17:50:07 +02:00
|
|
|
debug() {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
shown,
|
|
|
|
selector,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
empty(arg) {
|
|
|
|
if (arg === undefined) {
|
|
|
|
find_results.clear();
|
2022-08-18 00:30:47 +02:00
|
|
|
html = "";
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
expectOne() {
|
|
|
|
// silently do nothing
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
fadeTo: noop,
|
|
|
|
find(child_selector) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $child = find_results.get(child_selector);
|
|
|
|
if ($child) {
|
|
|
|
return $child;
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($child === false) {
|
2021-06-12 17:50:07 +02:00
|
|
|
// This is deliberately set to simulate missing find results.
|
|
|
|
// Return an empty array, the most common check is
|
|
|
|
// if ($.find().length) { //success }
|
|
|
|
return [];
|
|
|
|
}
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-07-05 14:09:03 +02:00
|
|
|
throw new Error(`
|
|
|
|
We need you to simulate the results of $(...).find(...)
|
|
|
|
by using set_find_results. You want something like this:
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = ...;
|
|
|
|
const $child = ...;
|
|
|
|
$container.set_find_results("${child_selector}", $child);
|
2021-07-05 14:09:03 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
Then calling $container.find("${child_selector}") will return
|
|
|
|
the "$child" zjquery element.
|
2021-07-05 14:09:03 +02:00
|
|
|
|
|
|
|
`);
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
get_on_handler(name, child_selector) {
|
|
|
|
return event_store.get_on_handler(name, child_selector);
|
|
|
|
},
|
|
|
|
hasClass(class_name) {
|
|
|
|
return classes.has(class_name);
|
|
|
|
},
|
|
|
|
height() {
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.notEqual(height, undefined, `Please call $("${selector}").set_height`);
|
2021-06-12 17:50:07 +02:00
|
|
|
return height;
|
|
|
|
},
|
|
|
|
hide() {
|
|
|
|
shown = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
html(arg) {
|
|
|
|
if (arg !== undefined) {
|
|
|
|
html = arg;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
return html;
|
|
|
|
},
|
|
|
|
is(arg) {
|
2022-04-09 23:44:38 +02:00
|
|
|
switch (arg) {
|
|
|
|
case ":visible":
|
|
|
|
return shown;
|
|
|
|
case ":focus":
|
|
|
|
return $self.is_focused();
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error("zjquery does not support this is() call");
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
is_focused() {
|
|
|
|
// is_focused is not a jQuery thing; this is
|
|
|
|
// for our testing
|
|
|
|
return event_store.is_focused();
|
|
|
|
},
|
|
|
|
off(...args) {
|
|
|
|
event_store.off(...args);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
offset() {
|
|
|
|
return {
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
on(...args) {
|
|
|
|
event_store.on(...args);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-06-12 17:50:07 +02:00
|
|
|
one(...args) {
|
|
|
|
event_store.one(...args);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
parent() {
|
2022-01-25 11:36:19 +01:00
|
|
|
return $my_parent;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
parents(parents_selector) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $result = parents_result.get(parents_selector);
|
2021-06-12 17:50:07 +02:00
|
|
|
assert.ok(
|
2022-01-25 11:36:19 +01:00
|
|
|
$result,
|
2021-06-12 17:50:07 +02:00
|
|
|
"You need to call set_parents_result for " + parents_selector + " in " + selector,
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $result;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
prepend(arg) {
|
|
|
|
html = arg + html;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
prop(name, val) {
|
|
|
|
if (val === undefined) {
|
|
|
|
return properties.get(name);
|
|
|
|
}
|
|
|
|
properties.set(name, val);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
removeAttr(name) {
|
|
|
|
attrs.delete(name);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
removeClass(class_names) {
|
|
|
|
class_names = class_names.split(" ");
|
|
|
|
for (const class_name of class_names) {
|
|
|
|
classes.delete(class_name);
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-06-12 17:50:07 +02:00
|
|
|
remove() {
|
|
|
|
throw new Error(`
|
2021-11-02 08:55:14 +01:00
|
|
|
We don't support remove in zjquery.
|
2021-06-12 17:50:07 +02:00
|
|
|
|
|
|
|
You can do $(...).remove = ... if necessary.
|
|
|
|
|
|
|
|
But you are probably writing too deep a test
|
|
|
|
for node testing.
|
|
|
|
`);
|
|
|
|
},
|
|
|
|
removeData: noop,
|
2022-01-25 11:36:19 +01:00
|
|
|
set_find_results(find_selector, $jquery_object) {
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.notEqual(
|
|
|
|
$jquery_object,
|
|
|
|
undefined,
|
|
|
|
"Please make the 'find result' be something like $.create('unused')",
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
find_results.set(find_selector, $jquery_object);
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
set_height(fake_height) {
|
|
|
|
height = fake_height;
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
set_parent($parent_elem) {
|
|
|
|
$my_parent = $parent_elem;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
set_parents_result(selector, $result) {
|
|
|
|
parents_result.set(selector, $result);
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
show() {
|
|
|
|
shown = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
text(...args) {
|
|
|
|
if (args.length !== 0) {
|
|
|
|
if (args[0] !== undefined) {
|
|
|
|
text = args[0].toString();
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
},
|
|
|
|
toggle(show) {
|
|
|
|
assert.ok([true, false].includes(show));
|
|
|
|
shown = show;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
2023-01-09 07:43:39 +01:00
|
|
|
toggleClass(class_name, add) {
|
|
|
|
if (add) {
|
|
|
|
classes.set(class_name, true);
|
|
|
|
} else {
|
|
|
|
classes.delete(class_name);
|
|
|
|
}
|
|
|
|
return $self;
|
|
|
|
},
|
2021-06-12 17:50:07 +02:00
|
|
|
trigger(ev) {
|
2022-01-25 11:36:19 +01:00
|
|
|
event_store.trigger($self, ev);
|
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
val(...args) {
|
|
|
|
if (args.length === 0) {
|
|
|
|
return value || "";
|
|
|
|
}
|
|
|
|
[value] = args;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
},
|
|
|
|
visible() {
|
|
|
|
return shown;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (opts.children) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.map = (f) => opts.children.map((i, elem) => f(elem, i));
|
|
|
|
$self.each = (f) => {
|
2021-06-12 17:50:07 +02:00
|
|
|
for (const child of opts.children) {
|
|
|
|
f.call(child);
|
|
|
|
}
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
$self[Symbol.iterator] = function* () {
|
2021-06-12 17:50:07 +02:00
|
|
|
for (const child of opts.children) {
|
|
|
|
yield child;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const [i, child] of opts.children.entries()) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$self[i] = child;
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.length = opts.children.length;
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selector[0] === "<") {
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.html(selector);
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.selector = selector;
|
2021-06-12 17:50:07 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.__zjquery = true;
|
2021-06-12 17:50:07 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function make_event_store(selector) {
|
|
|
|
/*
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
const on_functions = new Map();
|
|
|
|
const child_on_functions = new Map();
|
|
|
|
let focused = false;
|
|
|
|
|
|
|
|
const self = {
|
|
|
|
get_on_handler(name, child_selector) {
|
|
|
|
let handler;
|
|
|
|
|
|
|
|
if (child_selector === undefined) {
|
|
|
|
handler = on_functions.get(name);
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.ok(handler, `no ${name} handler for ${selector}`);
|
2021-06-12 17:50:07 +02:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
const child_on = child_on_functions.get(child_selector);
|
|
|
|
if (child_on) {
|
|
|
|
handler = child_on.get(name);
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.ok(handler, `no ${name} handler for ${selector} ${child_selector}`);
|
2021-06-12 17:50:07 +02:00
|
|
|
|
|
|
|
return handler;
|
|
|
|
},
|
|
|
|
|
|
|
|
off(event_name, ...args) {
|
|
|
|
if (args.length === 0) {
|
|
|
|
on_functions.delete(event_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-06-12 17:50:07 +02:00
|
|
|
throw new Error("zjquery does not support this call sequence");
|
|
|
|
},
|
|
|
|
|
|
|
|
on(event_name, ...args) {
|
|
|
|
// parameters will either be
|
|
|
|
// (event_name, handler) or
|
|
|
|
// (event_name, sel, handler)
|
|
|
|
if (args.length === 1) {
|
|
|
|
const [handler] = args;
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore if */
|
2021-06-12 17:50:07 +02:00
|
|
|
if (on_functions.has(event_name)) {
|
|
|
|
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");
|
|
|
|
throw new Error("dup " + event_name + " handler for " + selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
on_functions.set(event_name, handler);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.equal(args.length, 2, "wrong number of arguments passed in");
|
2021-06-12 17:50:07 +02:00
|
|
|
|
|
|
|
const [sel, handler] = args;
|
|
|
|
assert.equal(typeof sel, "string", "String selectors expected here.");
|
|
|
|
assert.equal(typeof handler, "function", "An handler function expected here.");
|
|
|
|
|
|
|
|
if (!child_on_functions.has(sel)) {
|
|
|
|
child_on_functions.set(sel, new Map());
|
|
|
|
}
|
|
|
|
|
|
|
|
const child_on = child_on_functions.get(sel);
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.ok(
|
|
|
|
!child_on.has(event_name),
|
|
|
|
`dup ${event_name} handler for ${selector} ${sel}`,
|
|
|
|
);
|
2021-06-12 17:50:07 +02:00
|
|
|
|
|
|
|
child_on.set(event_name, handler);
|
|
|
|
},
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-06-12 17:50:07 +02:00
|
|
|
one(event_name, handler) {
|
|
|
|
self.on(event_name, function (ev) {
|
|
|
|
self.off(event_name);
|
|
|
|
return handler.call(this, ev);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
trigger($element, ev, data) {
|
|
|
|
if (typeof ev === "string") {
|
|
|
|
ev = new FakeEvent(ev);
|
|
|
|
}
|
|
|
|
if (!ev.target) {
|
2022-01-25 11:36:19 +01:00
|
|
|
// FIXME: event.target should not be a jQuery object
|
|
|
|
ev.target = $element; // eslint-disable-line no-jquery/variable-pattern
|
2021-06-12 17:50:07 +02:00
|
|
|
}
|
|
|
|
const func = on_functions.get(ev.type);
|
|
|
|
|
|
|
|
if (func) {
|
|
|
|
// 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.)
|
|
|
|
func.call($element, ev, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.type === "focus" || ev.type === "focusin") {
|
|
|
|
focused = true;
|
|
|
|
} else if (ev.type === "blur" || ev.type === "focusout") {
|
|
|
|
focused = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
is_focused() {
|
|
|
|
return focused;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FakeElement;
|