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");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const ui_report = mock_esm("../../static/js/ui_report", {
|
2020-07-10 16:46:59 +02:00
|
|
|
displayed_error: false,
|
2021-02-28 00:58:55 +01:00
|
|
|
|
2020-07-10 16:46:59 +02:00
|
|
|
error: () => {
|
|
|
|
ui_report.displayed_error = true;
|
|
|
|
},
|
2021-03-07 13:57:14 +01:00
|
|
|
});
|
2021-03-23 05:14:54 +01:00
|
|
|
const location = set_global("location", {
|
2019-07-25 09:13:22 +02:00
|
|
|
protocol: "https:",
|
|
|
|
host: "example.com",
|
|
|
|
pathname: "/",
|
|
|
|
});
|
2018-08-05 01:30:23 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const hash_util = zrequire("hash_util");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const people = zrequire("people");
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const {Filter} = zrequire("../js/filter");
|
2020-12-01 23:21:38 +01:00
|
|
|
const narrow_state = zrequire("narrow_state");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const hamlet = {
|
2020-07-03 17:02:30 +02:00
|
|
|
user_id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "hamlet@example.com",
|
|
|
|
full_name: "Hamlet",
|
2018-04-13 23:44:10 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(hamlet);
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const frontend = {
|
2018-04-13 23:44:10 +02:00
|
|
|
stream_id: 99,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "frontend",
|
2018-04-13 23:44:10 +02:00
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(frontend);
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("hash_util", () => {
|
2018-04-13 23:44:10 +02:00
|
|
|
// Test encodeHashComponent
|
2020-07-15 01:29:15 +02:00
|
|
|
const str = "https://www.zulipexample.com";
|
2019-11-02 00:06:25 +01:00
|
|
|
const result1 = hash_util.encodeHashComponent(str);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(result1, "https.3A.2F.2Fwww.2Ezulipexample.2Ecom");
|
2018-04-13 23:44:10 +02:00
|
|
|
|
|
|
|
// Test decodeHashComponent
|
2019-11-02 00:06:25 +01:00
|
|
|
const result2 = hash_util.decodeHashComponent(result1);
|
2018-04-13 23:44:10 +02:00
|
|
|
assert.equal(result2, str);
|
|
|
|
|
|
|
|
// Test encode_operand and decode_operand
|
|
|
|
|
|
|
|
function encode_decode_operand(operator, operand, expected_val) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const encode_result = hash_util.encode_operand(operator, operand);
|
2018-04-13 23:44:10 +02:00
|
|
|
assert.equal(encode_result, expected_val);
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_operand = encode_result;
|
|
|
|
const decode_result = hash_util.decode_operand(operator, new_operand);
|
2018-04-13 23:44:10 +02:00
|
|
|
assert.equal(decode_result, operand);
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let operator = "sender";
|
2019-11-02 00:06:25 +01:00
|
|
|
let operand = hamlet.email;
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
encode_decode_operand(operator, operand, "15-hamlet");
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
operator = "stream";
|
|
|
|
operand = "frontend";
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
encode_decode_operand(operator, operand, "99-frontend");
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
operator = "topic";
|
|
|
|
operand = "testing 123";
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
encode_decode_operand(operator, operand, "testing.20123");
|
2020-07-10 16:46:59 +02:00
|
|
|
|
|
|
|
// Test invalid url decode.
|
|
|
|
const result = hash_util.decodeHashComponent("foo.foo");
|
|
|
|
assert.equal(result, "");
|
|
|
|
assert.equal(ui_report.displayed_error, true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-08-05 01:30:23 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_get_hash_category", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.get_hash_category("streams/subscribed"), "streams");
|
|
|
|
assert.deepEqual(hash_util.get_hash_category("#settings/display-settings"), "settings");
|
|
|
|
assert.deepEqual(hash_util.get_hash_category("#drafts"), "drafts");
|
|
|
|
assert.deepEqual(hash_util.get_hash_category("invites"), "invites");
|
2021-03-23 05:14:54 +01:00
|
|
|
|
|
|
|
location.hash = "#settings/your-account";
|
|
|
|
assert.deepEqual(hash_util.get_current_hash_category(), "settings");
|
2018-12-04 23:48:07 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_get_hash_section", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("streams/subscribed"), "subscribed");
|
|
|
|
assert.equal(hash_util.get_hash_section("#settings/your-account"), "your-account");
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("settings/10/general/"), "10");
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("#drafts"), "");
|
|
|
|
assert.equal(hash_util.get_hash_section(""), "");
|
2021-03-23 05:14:54 +01:00
|
|
|
|
|
|
|
location.hash = "#settings/your-account";
|
|
|
|
assert.deepEqual(hash_util.get_current_hash_section(), "your-account");
|
2018-12-05 20:33:52 +01:00
|
|
|
});
|
|
|
|
|
2021-03-04 13:36:30 +01:00
|
|
|
run_test("build_reload_url", () => {
|
|
|
|
location.hash = "#settings/your-account";
|
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=settings%2Fyour-account");
|
|
|
|
|
|
|
|
location.hash = "#test";
|
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=test");
|
|
|
|
|
|
|
|
location.hash = "#";
|
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=");
|
|
|
|
|
|
|
|
location.hash = "";
|
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=");
|
|
|
|
});
|
|
|
|
|
2021-03-15 06:57:14 +01:00
|
|
|
run_test("test_active_stream", () => {
|
|
|
|
location.hash = "#streams/1/announce";
|
|
|
|
assert.equal(hash_util.active_stream().id, 1);
|
|
|
|
assert.equal(hash_util.active_stream().name, "announce");
|
|
|
|
|
|
|
|
location.hash = "#test/narrow";
|
|
|
|
assert.equal(hash_util.active_stream(), undefined);
|
|
|
|
});
|
|
|
|
|
2021-03-15 09:24:10 +01:00
|
|
|
run_test("test_is_create_new_stream_narrow", () => {
|
|
|
|
location.hash = "#streams/new";
|
|
|
|
assert.equal(hash_util.is_create_new_stream_narrow(), true);
|
|
|
|
|
|
|
|
location.hash = "#some/random/hash";
|
|
|
|
assert.equal(hash_util.is_create_new_stream_narrow(), false);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_parse_narrow", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "99-frontend"]), [
|
|
|
|
{negated: false, operator: "stream", operand: "frontend"},
|
|
|
|
]);
|
2020-03-20 19:56:01 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "-stream", "99-frontend"]), [
|
|
|
|
{negated: true, operator: "stream", operand: "frontend"},
|
|
|
|
]);
|
2018-12-04 23:24:03 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.parse_narrow(["narrow", "BOGUS"]), undefined);
|
2020-03-20 19:56:01 +01:00
|
|
|
|
|
|
|
// For nonexistent streams, we get the full slug.
|
|
|
|
// We possibly should remove the prefix and fix this test.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "42-bogus"]), [
|
|
|
|
{negated: false, operator: "stream", operand: "42-bogus"},
|
|
|
|
]);
|
2018-12-04 23:24:03 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_stream_edit_uri", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "research & development",
|
2018-12-02 19:42:34 +01:00
|
|
|
stream_id: 42,
|
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.stream_edit_uri(sub), "#streams/42/research.20.26.20development");
|
2018-12-02 19:42:34 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_by_conversation_and_time_uri", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-14 19:02:26 +01:00
|
|
|
stream_id: frontend.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "testing",
|
2018-08-05 01:30:23 +02:00
|
|
|
id: 42,
|
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
hash_util.by_conversation_and_time_uri(message),
|
|
|
|
"https://example.com/#narrow/stream/99-frontend/topic/testing/near/42",
|
|
|
|
);
|
2018-08-05 01:30:23 +02:00
|
|
|
|
|
|
|
message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2018-10-18 22:05:43 +02:00
|
|
|
display_recipient: [
|
|
|
|
{
|
2020-01-01 13:02:34 +01:00
|
|
|
id: hamlet.user_id,
|
2018-10-18 22:05:43 +02:00
|
|
|
},
|
|
|
|
],
|
2018-08-05 01:30:23 +02:00
|
|
|
id: 43,
|
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
hash_util.by_conversation_and_time_uri(message),
|
|
|
|
"https://example.com/#narrow/pm-with/15-pm/near/43",
|
|
|
|
);
|
2018-08-05 01:30:23 +02:00
|
|
|
});
|
2020-06-20 05:48:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_search_public_streams_notice_url", () => {
|
2020-06-20 05:48:18 +02:00
|
|
|
function set_uri(uri) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const operators = hash_util.parse_narrow(uri.split("/"));
|
2020-06-20 05:48:18 +02:00
|
|
|
narrow_state.set_current_filter(new Filter(operators));
|
|
|
|
}
|
|
|
|
|
|
|
|
set_uri("#narrow/search/abc");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.search_public_streams_notice_url(), "#narrow/streams/public/search/abc");
|
2020-06-20 05:48:18 +02:00
|
|
|
|
|
|
|
set_uri("#narrow/has/link/has/image/has/attachment");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
hash_util.search_public_streams_notice_url(),
|
|
|
|
"#narrow/streams/public/has/link/has/image/has/attachment",
|
|
|
|
);
|
2020-06-20 05:48:18 +02:00
|
|
|
|
|
|
|
set_uri("#narrow/sender/15");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
hash_util.search_public_streams_notice_url(),
|
|
|
|
"#narrow/streams/public/sender/15-hamlet",
|
|
|
|
);
|
2020-06-20 05:48:18 +02:00
|
|
|
});
|