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-14 10:30:19 +02:00
|
|
|
|
const {
|
|
|
|
|
mock_esm,
|
|
|
|
|
set_global,
|
|
|
|
|
zrequire,
|
|
|
|
|
with_overrides,
|
|
|
|
|
mock_template,
|
|
|
|
|
} = 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");
|
2021-03-25 22:35:45 +01:00
|
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2020-02-12 08:14:40 +01:00
|
|
|
|
const ls_container = new Map();
|
2021-02-23 14:37:26 +01:00
|
|
|
|
const noop = () => {};
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
|
const localStorage = set_global("localStorage", {
|
2020-07-20 22:18:43 +02:00
|
|
|
|
getItem(key) {
|
2020-02-12 08:14:40 +01:00
|
|
|
|
return ls_container.get(key);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
|
setItem(key, val) {
|
2020-02-12 08:14:40 +01:00
|
|
|
|
ls_container.set(key, val);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
|
removeItem(key) {
|
2020-02-12 08:14:40 +01:00
|
|
|
|
ls_container.delete(key);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
|
clear() {
|
2020-02-12 08:14:40 +01:00
|
|
|
|
ls_container.clear();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
},
|
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
|
const compose_state = mock_esm("../../static/js/compose_state");
|
|
|
|
|
mock_esm("../../static/js/markdown", {
|
2021-03-06 17:37:51 +01:00
|
|
|
|
apply_markdown: noop,
|
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
|
mock_esm("../../static/js/stream_data", {
|
2020-07-20 22:18:43 +02:00
|
|
|
|
get_color() {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
return "#FFFFFF";
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
|
|
|
|
});
|
2021-03-25 22:35:45 +01:00
|
|
|
|
page_params.twenty_four_hour_time = false;
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-06-14 10:30:19 +02:00
|
|
|
|
const render_draft_table_body = mock_template("draft_table_body.hbs");
|
|
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
|
const {localstorage} = zrequire("localstorage");
|
|
|
|
|
const drafts = zrequire("drafts");
|
|
|
|
|
const timerender = zrequire("timerender");
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const legacy_draft = {
|
2018-12-16 17:36:41 +01:00
|
|
|
|
stream: "stream",
|
|
|
|
|
subject: "lunch",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "whatever",
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const compose_args_for_legacy_draft = {
|
2018-12-16 17:36:41 +01:00
|
|
|
|
stream: "stream",
|
2018-11-15 19:14:16 +01:00
|
|
|
|
topic: "lunch",
|
2018-12-16 17:36:41 +01:00
|
|
|
|
type: "stream",
|
|
|
|
|
content: "whatever",
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_1 = {
|
2017-02-22 02:34:05 +01:00
|
|
|
|
stream: "stream",
|
2018-12-16 17:36:41 +01:00
|
|
|
|
topic: "topic",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_2 = {
|
2017-02-22 02:34:05 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
2017-03-29 08:54:26 +02:00
|
|
|
|
reply_to: "aaron@zulip.com",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message",
|
2017-02-22 02:34:05 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const short_msg = {
|
2018-08-21 23:04:30 +02:00
|
|
|
|
stream: "stream",
|
|
|
|
|
subject: "topic",
|
|
|
|
|
type: "stream",
|
|
|
|
|
content: "a",
|
|
|
|
|
};
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
|
run_test(label, ({override}) => {
|
2021-06-04 19:27:45 +02:00
|
|
|
|
$("#draft_overlay").css = () => {};
|
2021-03-14 10:30:49 +01:00
|
|
|
|
localStorage.clear();
|
2021-06-16 14:38:37 +02:00
|
|
|
|
f({override});
|
2021-03-14 10:30:49 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test("legacy", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
|
assert.deepEqual(drafts.restore_message(legacy_draft), compose_args_for_legacy_draft);
|
2018-12-16 17:36:41 +01:00
|
|
|
|
});
|
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("draft_model add", ({override}) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
2021-02-14 12:55:03 +01:00
|
|
|
|
assert.equal(ls.get("draft"), undefined);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-02-14 12:55:03 +01:00
|
|
|
|
override(Date, "now", () => 1);
|
|
|
|
|
const expected = {...draft_1};
|
|
|
|
|
expected.updatedAt = 1;
|
|
|
|
|
const id = draft_model.addDraft({...draft_1});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
|
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
test("draft_model edit", () => {
|
2021-02-14 12:55:03 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
assert.equal(ls.get("draft"), undefined);
|
|
|
|
|
let id;
|
|
|
|
|
|
|
|
|
|
with_overrides((override) => {
|
|
|
|
|
override(Date, "now", () => 1);
|
|
|
|
|
const expected = {...draft_1};
|
|
|
|
|
expected.updatedAt = 1;
|
|
|
|
|
id = draft_model.addDraft({...draft_1});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
with_overrides((override) => {
|
|
|
|
|
override(Date, "now", () => 2);
|
|
|
|
|
const expected = {...draft_2};
|
|
|
|
|
expected.updatedAt = 2;
|
|
|
|
|
draft_model.editDraft(id, {...draft_2});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("draft_model delete", ({override}) => {
|
2021-02-14 12:55:03 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
assert.equal(ls.get("draft"), undefined);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-02-14 12:55:03 +01:00
|
|
|
|
override(Date, "now", () => 1);
|
|
|
|
|
const expected = {...draft_1};
|
|
|
|
|
expected.updatedAt = 1;
|
|
|
|
|
const id = draft_model.addDraft({...draft_1});
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), expected);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2021-02-14 12:55:03 +01:00
|
|
|
|
draft_model.deleteDraft(id);
|
|
|
|
|
assert.deepEqual(draft_model.getDraft(id), false);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-03-29 08:54:26 +02:00
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("snapshot_message", ({override}) => {
|
2021-02-13 14:07:47 +01:00
|
|
|
|
let curr_draft;
|
|
|
|
|
|
|
|
|
|
function map(field, f) {
|
|
|
|
|
override(compose_state, field, f);
|
2017-03-29 08:54:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
map("get_message_type", () => curr_draft.type);
|
|
|
|
|
map("composing", () => Boolean(curr_draft.type));
|
|
|
|
|
map("message_content", () => curr_draft.content);
|
|
|
|
|
map("stream_name", () => curr_draft.stream);
|
|
|
|
|
map("topic", () => curr_draft.topic);
|
|
|
|
|
map("private_message_recipient", () => curr_draft.private_message_recipient);
|
|
|
|
|
|
|
|
|
|
curr_draft = draft_1;
|
2017-03-29 08:54:26 +02:00
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_1);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = draft_2;
|
2017-03-29 08:54:26 +02:00
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_2);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = short_msg;
|
2018-08-21 23:04:30 +02:00
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), undefined);
|
|
|
|
|
|
2021-02-13 14:07:47 +01:00
|
|
|
|
curr_draft = {};
|
2017-03-29 08:54:26 +02:00
|
|
|
|
assert.equal(drafts.snapshot_message(), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("initialize", ({override}) => {
|
2021-02-23 14:37:26 +01:00
|
|
|
|
window.addEventListener = (event_name, f) => {
|
2017-06-02 23:12:15 +02:00
|
|
|
|
assert.equal(event_name, "beforeunload");
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
|
override(drafts, "update_draft", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
|
called = true;
|
2021-02-28 00:37:57 +01:00
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
|
f();
|
2021-06-10 08:32:54 +02:00
|
|
|
|
assert.ok(called);
|
2017-06-02 23:12:15 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
drafts.initialize();
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2018-02-15 17:38:31 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
test("remove_old_drafts", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_3 = {
|
2018-02-15 17:38:31 +01:00
|
|
|
|
stream: "stream",
|
|
|
|
|
subject: "topic",
|
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message",
|
2018-02-15 17:38:31 +01:00
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_4 = {
|
2018-02-15 17:38:31 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "aaron@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message",
|
2018-02-15 17:38:31 +01:00
|
|
|
|
updatedAt: new Date().setDate(-30),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
|
|
|
|
const data = {id3: draft_3, id4: draft_4};
|
2018-02-15 17:38:31 +01:00
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
assert.deepEqual(draft_model.get(), data);
|
|
|
|
|
|
|
|
|
|
drafts.remove_old_drafts();
|
|
|
|
|
assert.deepEqual(draft_model.get(), {id3: draft_3});
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
test("format_drafts", ({override}) => {
|
2021-02-11 01:23:23 +01:00
|
|
|
|
override(drafts, "remove_old_drafts", noop);
|
2019-02-20 15:45:17 +01:00
|
|
|
|
|
2021-03-14 10:30:49 +01:00
|
|
|
|
function feb12() {
|
|
|
|
|
return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function date(offset) {
|
|
|
|
|
return feb12().setDate(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const draft_1 = {
|
|
|
|
|
stream: "stream",
|
|
|
|
|
topic: "topic",
|
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: feb12().getTime(),
|
|
|
|
|
};
|
|
|
|
|
const draft_2 = {
|
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "aaron@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-1),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_3 = {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
stream: "stream 2",
|
|
|
|
|
subject: "topic",
|
|
|
|
|
type: "stream",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test stream message 2",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-10),
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_4 = {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "iago@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message 2",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-5),
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_5 = {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
private_message_recipient: "aaron@zulip.com",
|
|
|
|
|
reply_to: "zoe@zulip.com",
|
|
|
|
|
type: "private",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
content: "Test private message 3",
|
2021-03-14 10:30:49 +01:00
|
|
|
|
updatedAt: date(-2),
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const expected = [
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id1",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
|
stream: "stream",
|
|
|
|
|
stream_color: "#FFFFFF",
|
|
|
|
|
dark_background: "",
|
|
|
|
|
topic: "topic",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test stream message",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "7:55 AM",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id2",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: false,
|
2020-08-20 21:24:06 +02:00
|
|
|
|
recipients: "aaron@zulip.com",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test private message",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 30",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id5",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: false,
|
2020-08-20 21:24:06 +02:00
|
|
|
|
recipients: "aaron@zulip.com",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test private message 3",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 29",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id4",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: false,
|
2020-08-20 21:24:06 +02:00
|
|
|
|
recipients: "aaron@zulip.com",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test private message 2",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 26",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
|
draft_id: "id3",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
is_stream: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
|
stream: "stream 2",
|
|
|
|
|
stream_color: "#FFFFFF",
|
|
|
|
|
dark_background: "",
|
|
|
|
|
topic: "topic",
|
2021-05-10 07:02:14 +02:00
|
|
|
|
raw_content: "Test stream message 2",
|
2020-07-15 01:29:15 +02:00
|
|
|
|
time_stamp: "Jan 21",
|
2018-03-11 22:03:42 +01:00
|
|
|
|
},
|
2018-12-16 21:15:32 +01:00
|
|
|
|
];
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
$("#drafts_table").append = noop;
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
2020-07-16 22:40:18 +02:00
|
|
|
|
const data = {id1: draft_1, id2: draft_2, id3: draft_3, id4: draft_4, id5: draft_5};
|
2018-03-11 22:03:42 +01:00
|
|
|
|
ls.set("drafts", data);
|
|
|
|
|
assert.deepEqual(draft_model.get(), data);
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const stub_render_now = timerender.render_now;
|
2021-03-07 17:01:11 +01:00
|
|
|
|
override(timerender, "render_now", (time) => stub_render_now(time, new Date(1549958107000)));
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2021-06-14 10:30:19 +02:00
|
|
|
|
override(render_draft_table_body, "f", (data) => {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
// Tests formatting and sorting of drafts
|
|
|
|
|
assert.deepEqual(data.drafts, expected);
|
2020-07-15 01:29:15 +02:00
|
|
|
|
return "<draft table stub>";
|
2019-07-11 05:06:20 +02:00
|
|
|
|
});
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2021-02-11 01:23:23 +01:00
|
|
|
|
override(drafts, "open_overlay", noop);
|
2021-03-13 20:04:46 +01:00
|
|
|
|
override(drafts, "set_initial_element", noop);
|
2019-04-18 21:11:30 +02:00
|
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
|
$.create("#drafts_table .draft-row", {children: []});
|
2018-12-16 21:10:20 +01:00
|
|
|
|
drafts.launch();
|
2021-02-28 01:14:36 +01:00
|
|
|
|
timerender.__Rewire__("render_now", stub_render_now);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|