2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-06-16 15:58:34 +02:00
|
|
|
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2019-06-12 16:09:24 +02:00
|
|
|
const noop = () => {};
|
|
|
|
|
2021-04-20 16:49:39 +02:00
|
|
|
mock_esm("tippy.js", {
|
2021-06-14 17:43:40 +02:00
|
|
|
default: (arg) => {
|
|
|
|
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("basics", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
common.autofocus("#home");
|
2021-03-13 15:49:01 +01:00
|
|
|
$.get_initialize_function()();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok($("#home").is_focused());
|
2021-03-13 15:49:01 +01:00
|
|
|
$.clear_initialize_function();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-06-25 17:14:45 +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-07-10 01:06:33 +02:00
|
|
|
run_test("adjust_mac_shortcuts non-mac", ({override}) => {
|
|
|
|
override(navigator, "platform", "Windows");
|
2021-02-08 16:51:15 +01:00
|
|
|
|
|
|
|
// The adjust_mac_shortcuts 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_shortcuts("selector-that-does-not-exist");
|
|
|
|
});
|
|
|
|
|
2022-07-12 14:41:07 +02:00
|
|
|
// Test non-default value of adjust_mac_shortcuts boolean parameter:
|
|
|
|
// `kbd_elem = false`.
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("adjust_mac_shortcuts mac non-defaults", ({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", "⌘"],
|
2022-07-13 12:28:12 +02:00
|
|
|
["Alt", "⌘"],
|
2020-07-15 01:29:15 +02:00
|
|
|
["X + Shift", "X + Shift"],
|
|
|
|
["⌘ + Return", "⌘ + Return"],
|
2022-06-21 17:58:02 +02:00
|
|
|
["Enter or Backspace", "Enter or Backspace"],
|
|
|
|
["#stream_name", "#stream_name"],
|
|
|
|
["Ctrl+K", "Ctrl+K"],
|
2019-06-07 11:03:13 +02:00
|
|
|
]);
|
|
|
|
|
2022-06-21 17:58:02 +02:00
|
|
|
const fn_shortcuts = new Set(["Home", "End", "PgUp", "PgDn"]);
|
|
|
|
const inserted_fn_key = "<code>Fn</code> + ";
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(navigator, "platform", "MacIntel");
|
2019-06-07 11:03:13 +02:00
|
|
|
|
2022-07-13 12:51:34 +02:00
|
|
|
const kbd_element = false;
|
|
|
|
|
2021-02-09 12:12:12 +01:00
|
|
|
const test_items = [];
|
2021-02-08 16:51:15 +01:00
|
|
|
let key_no = 1;
|
2019-06-07 11:03:13 +02:00
|
|
|
|
2021-02-09 12:12:12 +01:00
|
|
|
for (const [old_key, mac_key] of keys_to_test_mac) {
|
|
|
|
const test_item = {};
|
2022-01-25 11:36:19 +01:00
|
|
|
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) => {
|
|
|
|
assert.equal($elem, inserted_fn_key);
|
|
|
|
};
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
test_item.$stub = $stub;
|
2021-02-09 12:12:12 +01:00
|
|
|
test_item.mac_key = mac_key;
|
2022-07-13 12:51:34 +02:00
|
|
|
test_item.adds_arrow_key = fn_shortcuts.has(old_key) && kbd_element;
|
2021-02-09 12:12:12 +01:00
|
|
|
test_items.push(test_item);
|
2019-06-07 11:03:13 +02:00
|
|
|
key_no += 1;
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2019-06-07 11:03:13 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const children = test_items.map((test_item) => ({to_$: () => test_item.$stub}));
|
2019-06-10 09:22:55 +02:00
|
|
|
|
2021-02-09 12:12:12 +01:00
|
|
|
$.create(".markdown_content", {children});
|
|
|
|
|
2022-07-12 14:41:07 +02:00
|
|
|
common.adjust_mac_shortcuts(".markdown_content", kbd_element);
|
2021-02-09 12:12:12 +01:00
|
|
|
|
|
|
|
for (const test_item of test_items) {
|
2022-01-25 11:36:19 +01:00
|
|
|
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);
|
2021-02-09 12:12:12 +01:00
|
|
|
}
|
2019-06-07 11:03:13 +02:00
|
|
|
});
|
2021-04-05 09:42:29 +02:00
|
|
|
|
2022-07-12 14:41:07 +02:00
|
|
|
// Test default value of adjust_mac_shortcuts boolean parameter:
|
|
|
|
// `kbd_elem = true`.
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("adjust_mac_shortcuts mac defaults", ({override}) => {
|
2022-06-21 17:58:02 +02:00
|
|
|
const keys_to_test_mac = new Map([
|
|
|
|
["Backspace", "Delete"],
|
|
|
|
["Enter", "Return"],
|
|
|
|
["Home", "←"],
|
|
|
|
["End", "→"],
|
|
|
|
["PgUp", "↑"],
|
|
|
|
["PgDn", "↓"],
|
|
|
|
["Ctrl", "⌘"],
|
2022-07-13 12:28:12 +02:00
|
|
|
["Alt", "⌘"],
|
2022-06-21 17:58:02 +02:00
|
|
|
["[", "["],
|
|
|
|
["X", "X"],
|
|
|
|
]);
|
|
|
|
|
|
|
|
const fn_shortcuts = new Set(["Home", "End", "PgUp", "PgDn"]);
|
|
|
|
const inserted_fn_key = "<kbd>Fn</kbd> + ";
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(navigator, "platform", "MacIntel");
|
2022-06-21 17:58:02 +02:00
|
|
|
|
2022-07-13 12:51:34 +02:00
|
|
|
const kbd_element = true;
|
|
|
|
|
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) => {
|
|
|
|
assert.equal($elem, inserted_fn_key);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
test_item.$stub = $stub;
|
|
|
|
test_item.mac_key = mac_key;
|
2022-07-13 12:51:34 +02:00
|
|
|
test_item.adds_arrow_key = fn_shortcuts.has(old_key) && kbd_element;
|
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}));
|
|
|
|
|
|
|
|
$.create(".hotkeys_table kbd", {children});
|
|
|
|
|
|
|
|
common.adjust_mac_shortcuts(".hotkeys_table kbd");
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
$(password_selector)[0] = () => {};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const ev = {
|
|
|
|
preventDefault: () => {},
|
|
|
|
stopPropagation: () => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
set_attribute("password");
|
|
|
|
common.setup_password_visibility_toggle("#id_password", password_selector);
|
|
|
|
|
|
|
|
const handler = $(password_selector).get_on_handler("click");
|
|
|
|
|
|
|
|
handler(ev);
|
|
|
|
check_assertion("text", "fa-eye", "fa-eye-slash");
|
|
|
|
|
|
|
|
handler(ev);
|
|
|
|
check_assertion("password", "fa-eye-slash", "fa-eye");
|
2021-04-05 09:53:15 +02:00
|
|
|
|
|
|
|
handler(ev);
|
|
|
|
|
|
|
|
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
|
|
|
});
|