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", {});
|
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";
|
|
|
|
|
|
|
|
const input = $.create("input");
|
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
let removed;
|
|
|
|
input.remove = () => {
|
|
|
|
removed = true;
|
|
|
|
};
|
|
|
|
|
2021-02-21 12:18:01 +01:00
|
|
|
override(document, "createElement", () => input);
|
|
|
|
override(document, "execCommand", noop);
|
|
|
|
|
|
|
|
$("body").append = noop;
|
|
|
|
$(input).val = (arg) => {
|
|
|
|
assert.equal(arg, admin_emails_val);
|
|
|
|
return {
|
|
|
|
trigger: noop,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-09 14:37:01 +01:00
|
|
|
const elem = {};
|
|
|
|
let faded_in = false;
|
|
|
|
let faded_out = false;
|
|
|
|
|
2019-06-12 16:09:24 +02: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
|
|
|
};
|
|
|
|
elem.fadeOut = (val) => {
|
|
|
|
assert.equal(val, 250);
|
2021-02-09 14:37:01 +01:00
|
|
|
faded_out = true;
|
2019-06-12 16:09:24 +02:00
|
|
|
};
|
|
|
|
elem.fadeIn = (val) => {
|
|
|
|
assert.equal(val, 1000);
|
2021-02-09 14:37:01 +01:00
|
|
|
faded_in = true;
|
2019-06-12 16:09:24 +02:00
|
|
|
};
|
2020-07-15 01:29:15 +02: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
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("adjust_mac_shortcuts non-mac", ({override}) => {
|
2021-03-07 17:01:11 +01:00
|
|
|
override(common, "has_mac_keyboard", () => false);
|
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");
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("adjust_mac_shortcuts 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"],
|
|
|
|
["Home", "Fn + ←"],
|
|
|
|
["End", "Fn + →"],
|
|
|
|
["PgUp", "Fn + ↑"],
|
|
|
|
["PgDn", "Fn + ↓"],
|
|
|
|
["X + Shift", "X + Shift"],
|
|
|
|
["⌘ + Return", "⌘ + Return"],
|
|
|
|
["Enter or Backspace", "Return or Delete"],
|
|
|
|
["Ctrl", "⌘"],
|
|
|
|
["Ctrl + Shift", "⌘ + Shift"],
|
|
|
|
["Ctrl + Backspace + End", "⌘ + Delete + Fn + →"],
|
2019-06-07 11:03:13 +02:00
|
|
|
]);
|
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
override(common, "has_mac_keyboard", () => true);
|
2019-06-07 11:03:13 +02:00
|
|
|
|
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 = {};
|
|
|
|
const stub = $.create("hotkey_" + key_no);
|
|
|
|
stub.text(old_key);
|
|
|
|
assert.equal(stub.hasClass("mac-cmd-key"), false);
|
|
|
|
test_item.stub = stub;
|
|
|
|
test_item.mac_key = mac_key;
|
|
|
|
test_item.is_cmd_key = old_key.includes("Ctrl");
|
|
|
|
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
|
|
|
|
2021-02-09 12:12:12 +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});
|
|
|
|
|
|
|
|
const require_cmd = true;
|
|
|
|
common.adjust_mac_shortcuts(".markdown_content", require_cmd);
|
|
|
|
|
|
|
|
for (const test_item of test_items) {
|
|
|
|
assert.equal(test_item.stub.hasClass("mac-cmd-key"), test_item.is_cmd_key);
|
|
|
|
assert.equal(test_item.stub.text(), test_item.mac_key);
|
|
|
|
}
|
2019-06-07 11:03:13 +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
|
|
|
});
|