node_tests: Move set_global mock in components before zrequire call.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-03-10 20:43:42 -08:00
parent 92a46888f0
commit ac36d59728
1 changed files with 153 additions and 153 deletions

View File

@ -7,6 +7,159 @@ const _ = require("lodash");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
let keydown_f;
let click_f;
const tabs = [];
let focused_tab;
function make_tab(i) {
const self = {};
assert.equal(tabs.length, i);
self.stub = true;
self.class = [];
self.addClass = (c) => {
self.class += " " + c;
const tokens = self.class.trim().split(/ +/);
self.class = _.uniq(tokens).join(" ");
};
self.removeClass = (c) => {
const tokens = self.class.trim().split(/ +/);
self.class = _.without(tokens, c).join(" ");
};
self.hasClass = (c) => {
const tokens = self.class.trim().split(/ +/);
return tokens.includes(c);
};
self.data = (name) => {
assert.equal(name, "tab-id");
return i;
};
self.text = (text) => {
assert.equal(
text,
[
"translated: Keyboard shortcuts",
"translated: Message formatting",
"translated: Search operators",
][i],
);
};
self.trigger = (type) => {
if (type === "focus") {
focused_tab = i;
}
};
tabs.push(self);
return self;
}
const ind_tab = (function () {
const self = {};
self.stub = true;
self.on = (name, f) => {
if (name === "click") {
click_f = f;
} else if (name === "keydown") {
keydown_f = f;
}
};
self.removeClass = (c) => {
for (const tab of tabs) {
tab.removeClass(c);
}
};
self.eq = (idx) => tabs[idx];
return self;
})();
const switcher = (function () {
const self = {};
self.stub = true;
self.children = [];
self.classList = new Set();
self.append = (child) => {
self.children.push(child);
};
self.addClass = (c) => {
self.classList.add(c);
self.addedClass = c;
};
self.find = (sel) => {
switch (sel) {
case ".ind-tab":
return ind_tab;
default:
throw new Error("unknown selector: " + sel);
}
};
return self;
})();
set_global("$", (sel, attributes) => {
if (sel.stub) {
// The component often redundantly re-wraps objects.
return sel;
}
switch (sel) {
case "<div class='tab-switcher'></div>":
return switcher;
case "<div class='tab-switcher stream_sorter_toggle'></div>":
return switcher;
case "<div>": {
const tab_id = attributes["data-tab-id"];
assert.deepEqual(
attributes,
[
{
class: "ind-tab",
"data-tab-key": "keyboard-shortcuts",
"data-tab-id": 0,
tabindex: 0,
},
{
class: "ind-tab",
"data-tab-key": "message-formatting",
"data-tab-id": 1,
tabindex: 0,
},
{
class: "ind-tab",
"data-tab-key": "search-operators",
"data-tab-id": 2,
tabindex: 0,
},
][tab_id],
);
return make_tab(tab_id);
}
default:
throw new Error("unknown selector: " + sel);
}
});
const components = zrequire("components");
const noop = () => {};
@ -15,160 +168,7 @@ const LEFT_KEY = {which: 37, preventDefault: noop, stopPropagation: noop};
const RIGHT_KEY = {which: 39, preventDefault: noop, stopPropagation: noop};
run_test("basics", () => {
let keydown_f;
let click_f;
const tabs = [];
let focused_tab;
let callback_args;
function make_tab(i) {
const self = {};
assert.equal(tabs.length, i);
self.stub = true;
self.class = [];
self.addClass = (c) => {
self.class += " " + c;
const tokens = self.class.trim().split(/ +/);
self.class = _.uniq(tokens).join(" ");
};
self.removeClass = (c) => {
const tokens = self.class.trim().split(/ +/);
self.class = _.without(tokens, c).join(" ");
};
self.hasClass = (c) => {
const tokens = self.class.trim().split(/ +/);
return tokens.includes(c);
};
self.data = (name) => {
assert.equal(name, "tab-id");
return i;
};
self.text = (text) => {
assert.equal(
text,
[
"translated: Keyboard shortcuts",
"translated: Message formatting",
"translated: Search operators",
][i],
);
};
self.trigger = (type) => {
if (type === "focus") {
focused_tab = i;
}
};
tabs.push(self);
return self;
}
const ind_tab = (function () {
const self = {};
self.stub = true;
self.on = (name, f) => {
if (name === "click") {
click_f = f;
} else if (name === "keydown") {
keydown_f = f;
}
};
self.removeClass = (c) => {
for (const tab of tabs) {
tab.removeClass(c);
}
};
self.eq = (idx) => tabs[idx];
return self;
})();
const switcher = (function () {
const self = {};
self.stub = true;
self.children = [];
self.classList = new Set();
self.append = (child) => {
self.children.push(child);
};
self.addClass = (c) => {
self.classList.add(c);
self.addedClass = c;
};
self.find = (sel) => {
switch (sel) {
case ".ind-tab":
return ind_tab;
default:
throw new Error("unknown selector: " + sel);
}
};
return self;
})();
set_global("$", (sel, attributes) => {
if (sel.stub) {
// The component often redundantly re-wraps objects.
return sel;
}
switch (sel) {
case "<div class='tab-switcher'></div>":
return switcher;
case "<div class='tab-switcher stream_sorter_toggle'></div>":
return switcher;
case "<div>": {
const tab_id = attributes["data-tab-id"];
assert.deepEqual(
attributes,
[
{
class: "ind-tab",
"data-tab-key": "keyboard-shortcuts",
"data-tab-id": 0,
tabindex: 0,
},
{
class: "ind-tab",
"data-tab-key": "message-formatting",
"data-tab-id": 1,
tabindex: 0,
},
{
class: "ind-tab",
"data-tab-key": "search-operators",
"data-tab-id": 2,
tabindex: 0,
},
][tab_id],
);
return make_tab(tab_id);
}
default:
throw new Error("unknown selector: " + sel);
}
});
let callback_value;
let widget = null;