2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-02-22 23:04:10 +01:00
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-04-20 16:49:39 +02:00
|
|
|
mock_esm("tippy.js", {
|
2022-11-17 23:33:43 +01:00
|
|
|
default(arg) {
|
2021-06-14 17:43:40 +02:00
|
|
|
arg._tippy = {setContent: noop};
|
|
|
|
return arg._tippy;
|
2021-04-20 16:49:39 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-21 12:18:01 +01:00
|
|
|
set_global("document", {});
|
2022-07-10 01:06:33 +02:00
|
|
|
const navigator = set_global("navigator", {});
|
2017-05-23 01:15:43 +02:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const common = zrequire("common");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("phrase_match", () => {
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(common.phrase_match("tes", "test"));
|
|
|
|
assert.ok(common.phrase_match("Tes", "test"));
|
|
|
|
assert.ok(common.phrase_match("Tes", "Test"));
|
|
|
|
assert.ok(common.phrase_match("tes", "Stream Test"));
|
2018-06-25 17:14:45 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!common.phrase_match("tests", "test"));
|
|
|
|
assert.ok(!common.phrase_match("tes", "hostess"));
|
2018-06-25 17:14:45 +02:00
|
|
|
});
|
2019-06-12 16:09:24 +02:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("copy_data_attribute_value", ({override}) => {
|
2021-02-21 12:18:01 +01:00
|
|
|
const admin_emails_val = "iago@zulip.com";
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $input = $.create("input");
|
2021-02-21 12:18:01 +01:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
let removed;
|
2022-01-25 11:36:19 +01:00
|
|
|
$input.remove = () => {
|
2021-02-25 16:07:04 +01:00
|
|
|
removed = true;
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
override(document, "createElement", () => $input);
|
2021-02-21 12:18:01 +01:00
|
|
|
override(document, "execCommand", noop);
|
|
|
|
|
|
|
|
$("body").append = noop;
|
2022-08-18 20:49:42 +02:00
|
|
|
$input.val = (arg) => {
|
2021-02-21 12:18:01 +01:00
|
|
|
assert.equal(arg, admin_emails_val);
|
|
|
|
return {
|
|
|
|
trigger: noop,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = {};
|
2021-02-09 14:37:01 +01:00
|
|
|
let faded_in = false;
|
|
|
|
let faded_out = false;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.data = (key) => {
|
2021-02-09 14:37:01 +01:00
|
|
|
assert.equal(key, "admin-emails");
|
|
|
|
return admin_emails_val;
|
2019-06-12 16:09:24 +02:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.fadeOut = (val) => {
|
2019-06-12 16:09:24 +02:00
|
|
|
assert.equal(val, 250);
|
2021-02-09 14:37:01 +01:00
|
|
|
faded_out = true;
|
2019-06-12 16:09:24 +02:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.fadeIn = (val) => {
|
2019-06-12 16:09:24 +02:00
|
|
|
assert.equal(val, 1000);
|
2021-02-09 14:37:01 +01:00
|
|
|
faded_in = true;
|
2019-06-12 16:09:24 +02:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
common.copy_data_attribute_value($elem, "admin-emails");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(removed);
|
|
|
|
assert.ok(faded_in);
|
|
|
|
assert.ok(faded_out);
|
2019-06-12 16:09:24 +02:00
|
|
|
});
|
2019-06-07 11:03:13 +02:00
|
|
|
|
2022-09-11 22:09:18 +02:00
|
|
|
run_test("adjust_mac_kbd_tags non-mac", ({override}) => {
|
2022-07-10 01:06:33 +02:00
|
|
|
override(navigator, "platform", "Windows");
|
2021-02-08 16:51:15 +01:00
|
|
|
|
2022-09-11 22:09:18 +02:00
|
|
|
// The adjust_mac_kbd_tags has a really simple guard
|
2021-02-08 16:51:15 +01:00
|
|
|
// at the top, and we just test the early-return behavior
|
|
|
|
// by trying to pass it garbage.
|
2022-09-11 22:09:18 +02:00
|
|
|
common.adjust_mac_kbd_tags("selector-that-does-not-exist");
|
2021-02-08 16:51:15 +01:00
|
|
|
});
|
|
|
|
|
2022-09-11 22:09:18 +02:00
|
|
|
run_test("adjust_mac_kbd_tags mac", ({override}) => {
|
2019-06-07 11:03:13 +02:00
|
|
|
const keys_to_test_mac = new Map([
|
2020-07-15 01:29:15 +02:00
|
|
|
["Backspace", "Delete"],
|
|
|
|
["Enter", "Return"],
|
2022-06-21 17:58:02 +02:00
|
|
|
["Home", "←"],
|
|
|
|
["End", "→"],
|
|
|
|
["PgUp", "↑"],
|
|
|
|
["PgDn", "↓"],
|
|
|
|
["Ctrl", "⌘"],
|
2024-01-25 16:02:04 +01:00
|
|
|
["Alt", "⌥"],
|
2022-06-21 17:58:02 +02:00
|
|
|
["#stream_name", "#stream_name"],
|
|
|
|
["Ctrl+K", "Ctrl+K"],
|
|
|
|
["[", "["],
|
|
|
|
["X", "X"],
|
2024-03-27 22:47:02 +01:00
|
|
|
["data-mac-following-key", "data-mac-following-key"],
|
2022-06-21 17:58:02 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
const fn_shortcuts = new Set(["Home", "End", "PgUp", "PgDn"]);
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(navigator, "platform", "MacIntel");
|
2024-04-04 00:53:08 +02:00
|
|
|
$("<span>").contents = () => $("<contents-stub>");
|
2022-06-21 17:58:02 +02:00
|
|
|
|
|
|
|
const test_items = [];
|
|
|
|
let key_no = 1;
|
|
|
|
|
|
|
|
for (const [old_key, mac_key] of keys_to_test_mac) {
|
|
|
|
const test_item = {};
|
|
|
|
const $stub = $.create("hotkey_" + key_no);
|
|
|
|
$stub.text(old_key);
|
2022-07-13 12:51:34 +02:00
|
|
|
assert.equal($stub.hasClass("arrow-key"), false);
|
2022-06-21 17:58:02 +02:00
|
|
|
if (fn_shortcuts.has(old_key)) {
|
|
|
|
$stub.before = ($elem) => {
|
2024-04-04 00:53:08 +02:00
|
|
|
assert.equal($elem.selector, "<kbd>");
|
2022-06-21 17:58:02 +02:00
|
|
|
};
|
|
|
|
}
|
2024-03-27 22:47:02 +01:00
|
|
|
if (old_key === "data-mac-following-key") {
|
|
|
|
$stub.attr("data-mac-following-key", "⌥");
|
2024-04-04 00:53:08 +02:00
|
|
|
$stub.after = ($plus, $elem) => {
|
|
|
|
assert.equal($plus.selector, "<contents-stub>");
|
2024-03-27 22:47:02 +01:00
|
|
|
assert.equal($elem.selector, "<kbd>");
|
|
|
|
assert.equal($elem.text(), $stub.attr("data-mac-following-key"));
|
|
|
|
};
|
|
|
|
}
|
2022-06-21 17:58:02 +02:00
|
|
|
test_item.$stub = $stub;
|
|
|
|
test_item.mac_key = mac_key;
|
2022-08-29 17:29:27 +02:00
|
|
|
test_item.adds_arrow_key = fn_shortcuts.has(old_key);
|
2022-06-21 17:58:02 +02:00
|
|
|
test_items.push(test_item);
|
|
|
|
key_no += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const children = test_items.map((test_item) => ({to_$: () => test_item.$stub}));
|
|
|
|
|
2022-08-29 17:29:27 +02:00
|
|
|
$.create(".markdown kbd", {children});
|
2022-06-21 17:58:02 +02:00
|
|
|
|
2022-09-11 22:09:18 +02:00
|
|
|
common.adjust_mac_kbd_tags(".markdown kbd");
|
2022-06-21 17:58:02 +02:00
|
|
|
|
|
|
|
for (const test_item of test_items) {
|
|
|
|
assert.equal(test_item.$stub.text(), test_item.mac_key);
|
2022-07-13 12:51:34 +02:00
|
|
|
assert.equal(test_item.$stub.hasClass("arrow-key"), test_item.adds_arrow_key);
|
2022-06-21 17:58:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-11 22:09:18 +02:00
|
|
|
run_test("adjust_mac_tooltip_keys non-mac", ({override}) => {
|
|
|
|
override(navigator, "platform", "Windows");
|
|
|
|
|
|
|
|
// The adjust_mac_tooltip_keys has a really simple guard
|
|
|
|
// at the top, and we just test the early-return behavior
|
|
|
|
// by trying to pass it garbage.
|
|
|
|
common.adjust_mac_tooltip_keys("not-an-array");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test default values of adjust_mac_tooltip_keys
|
|
|
|
// Expected values
|
|
|
|
run_test("adjust_mac_tooltip_keys mac expected", ({override}) => {
|
|
|
|
const keys_to_test_mac = new Map([
|
|
|
|
[["Backspace"], ["Delete"]],
|
|
|
|
[["Enter"], ["Return"]],
|
|
|
|
[["Home"], ["Fn", "←"]],
|
|
|
|
[["End"], ["Fn", "→"]],
|
|
|
|
[["PgUp"], ["Fn", "↑"]],
|
|
|
|
[["PgDn"], ["Fn", "↓"]],
|
|
|
|
[["Ctrl"], ["⌘"]],
|
|
|
|
]);
|
|
|
|
|
|
|
|
override(navigator, "platform", "MacIntel");
|
|
|
|
|
|
|
|
const test_items = [];
|
|
|
|
|
|
|
|
for (const [old_key, mac_key] of keys_to_test_mac) {
|
|
|
|
const test_item = {};
|
|
|
|
common.adjust_mac_tooltip_keys(old_key);
|
|
|
|
|
|
|
|
test_item.mac_key = mac_key;
|
|
|
|
test_item.adjusted_key = old_key;
|
|
|
|
test_items.push(test_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const test_item of test_items) {
|
|
|
|
assert.deepStrictEqual(test_item.mac_key, test_item.adjusted_key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test non-default values of adjust_mac_tooltip_keys
|
|
|
|
// Random values
|
|
|
|
run_test("adjust_mac_tooltip_keys mac random", ({override}) => {
|
|
|
|
const keys_to_test_mac = new Map([
|
|
|
|
[
|
|
|
|
["Ctrl", "["],
|
|
|
|
["⌘", "["],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
["Ctrl", "K"],
|
|
|
|
["⌘", "K"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
["Shift", "G"],
|
|
|
|
["Shift", "G"],
|
|
|
|
],
|
|
|
|
[["Space"], ["Space"]],
|
|
|
|
]);
|
|
|
|
|
|
|
|
override(navigator, "platform", "MacIntel");
|
|
|
|
|
|
|
|
const test_items = [];
|
|
|
|
|
|
|
|
for (const [old_key, mac_key] of keys_to_test_mac) {
|
|
|
|
const test_item = {};
|
|
|
|
common.adjust_mac_tooltip_keys(old_key);
|
|
|
|
|
|
|
|
test_item.mac_key = mac_key;
|
|
|
|
test_item.adjusted_key = old_key;
|
|
|
|
test_items.push(test_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const test_item of test_items) {
|
|
|
|
assert.deepStrictEqual(test_item.mac_key, test_item.adjusted_key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-05 09:42:29 +02:00
|
|
|
run_test("show password", () => {
|
2021-04-20 16:49:39 +02:00
|
|
|
const password_selector = "#id_password ~ .password_visibility_toggle";
|
|
|
|
|
2023-12-14 23:51:33 +01:00
|
|
|
$(password_selector)[0] = noop;
|
2021-04-05 09:42:29 +02:00
|
|
|
|
|
|
|
function set_attribute(type) {
|
|
|
|
$("#id_password").attr("type", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_assertion(type, present_class, absent_class) {
|
|
|
|
assert.equal($("#id_password").attr("type"), type);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok($(password_selector).hasClass(present_class));
|
|
|
|
assert.ok(!$(password_selector).hasClass(absent_class));
|
2021-04-05 09:42:29 +02:00
|
|
|
}
|
|
|
|
|
2024-03-01 02:29:45 +01:00
|
|
|
const click_ev = {
|
|
|
|
preventDefault() {},
|
|
|
|
stopPropagation() {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const key_ev = {
|
|
|
|
key: "Enter",
|
2022-11-17 23:33:43 +01:00
|
|
|
preventDefault() {},
|
|
|
|
stopPropagation() {},
|
2021-04-05 09:42:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
set_attribute("password");
|
|
|
|
common.setup_password_visibility_toggle("#id_password", password_selector);
|
|
|
|
|
2024-03-01 02:29:45 +01:00
|
|
|
const click_handler = $(password_selector).get_on_handler("click");
|
|
|
|
|
|
|
|
const key_handler = $(password_selector).get_on_handler("keydown");
|
|
|
|
|
|
|
|
click_handler(click_ev);
|
|
|
|
check_assertion("text", "fa-eye", "fa-eye-slash");
|
|
|
|
|
|
|
|
click_handler(click_ev);
|
|
|
|
check_assertion("password", "fa-eye-slash", "fa-eye");
|
2021-04-05 09:42:29 +02:00
|
|
|
|
2024-03-01 02:29:45 +01:00
|
|
|
key_handler(key_ev);
|
2021-04-05 09:42:29 +02:00
|
|
|
check_assertion("text", "fa-eye", "fa-eye-slash");
|
|
|
|
|
2024-03-01 02:29:45 +01:00
|
|
|
key_handler(key_ev);
|
2021-04-05 09:42:29 +02:00
|
|
|
check_assertion("password", "fa-eye-slash", "fa-eye");
|
2021-04-05 09:53:15 +02:00
|
|
|
|
2024-03-01 02:29:45 +01:00
|
|
|
click_handler(click_ev);
|
2021-04-05 09:53:15 +02:00
|
|
|
|
|
|
|
common.reset_password_toggle_icons("#id_password", password_selector);
|
|
|
|
check_assertion("password", "fa-eye-slash", "fa-eye");
|
2021-04-05 09:42:29 +02:00
|
|
|
});
|