2020-08-01 03:43:15 +02:00
|
|
|
|
"use strict";
|
|
|
|
|
|
2020-07-28 00:26:58 +02:00
|
|
|
|
const XDate = require("xdate");
|
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
set_global("$", global.make_zjquery());
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
zrequire("localstorage");
|
|
|
|
|
zrequire("drafts");
|
|
|
|
|
zrequire("timerender");
|
|
|
|
|
zrequire("stream_color");
|
|
|
|
|
zrequire("colorspace");
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2020-02-12 08:14:40 +01:00
|
|
|
|
const ls_container = new Map();
|
2020-07-15 00:34:28 +02:00
|
|
|
|
const noop = function () {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2018-03-11 22:03:42 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
|
set_global("compose", {});
|
|
|
|
|
set_global("compose_state", {});
|
|
|
|
|
set_global("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
|
|
|
|
},
|
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
|
set_global("people", {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
// Mocking get_by_email function, here we are
|
|
|
|
|
// just returning string before `@` in email
|
2020-07-20 22:18:43 +02:00
|
|
|
|
get_by_email(email) {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
return {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
full_name: email.split("@")[0],
|
2018-03-11 22:03:42 +01:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
|
set_global("markdown", {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
apply_markdown: noop,
|
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
|
set_global("page_params", {
|
2018-03-11 22:03:42 +01:00
|
|
|
|
twenty_four_hour_time: false,
|
|
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2017-03-29 08:22:25 +02:00
|
|
|
|
function stub_timestamp(timestamp, func) {
|
2020-07-27 16:15:02 +02:00
|
|
|
|
function fake_time() {
|
2017-02-22 02:34:05 +01:00
|
|
|
|
return timestamp;
|
2020-07-27 16:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
with_field(Date.prototype, "getTime", fake_time, func);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
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",
|
|
|
|
|
content: "Test Stream Message",
|
|
|
|
|
};
|
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",
|
|
|
|
|
content: "Test Private Message",
|
|
|
|
|
};
|
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
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_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
|
|
|
|
});
|
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("draft_model", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
(function test_get() {
|
2020-07-16 22:40:18 +02:00
|
|
|
|
const expected = {id1: draft_1, id2: draft_2};
|
2017-02-22 02:34:05 +01:00
|
|
|
|
ls.set("drafts", expected);
|
|
|
|
|
|
|
|
|
|
assert.deepEqual(draft_model.get(), expected);
|
2020-07-16 22:35:58 +02:00
|
|
|
|
})();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
(function test_get() {
|
2020-07-16 22:40:18 +02:00
|
|
|
|
ls.set("drafts", {id1: draft_1});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
|
|
assert.deepEqual(draft_model.getDraft("id1"), draft_1);
|
|
|
|
|
assert.equal(draft_model.getDraft("id2"), false);
|
2020-07-16 22:35:58 +02:00
|
|
|
|
})();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
(function test_addDraft() {
|
2020-07-02 01:45:54 +02:00
|
|
|
|
stub_timestamp(1, () => {
|
2020-07-16 22:40:18 +02:00
|
|
|
|
const expected = {...draft_1};
|
2018-05-07 03:30:13 +02:00
|
|
|
|
expected.updatedAt = 1;
|
2020-07-16 22:40:18 +02:00
|
|
|
|
const id = draft_model.addDraft({...draft_1});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2018-05-07 03:30:13 +02:00
|
|
|
|
assert.deepEqual(ls.get("drafts")[id], expected);
|
|
|
|
|
});
|
2020-07-16 22:35:58 +02:00
|
|
|
|
})();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
(function test_editDraft() {
|
2020-07-02 01:45:54 +02:00
|
|
|
|
stub_timestamp(2, () => {
|
2020-07-16 22:40:18 +02:00
|
|
|
|
ls.set("drafts", {id1: draft_1});
|
|
|
|
|
const expected = {...draft_2};
|
2018-05-07 03:30:13 +02:00
|
|
|
|
expected.updatedAt = 2;
|
2020-07-16 22:40:18 +02:00
|
|
|
|
draft_model.editDraft("id1", {...draft_2});
|
2018-05-07 03:30:13 +02:00
|
|
|
|
|
|
|
|
|
assert.deepEqual(ls.get("drafts").id1, expected);
|
|
|
|
|
});
|
2020-07-16 22:35:58 +02:00
|
|
|
|
})();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
(function test_deleteDraft() {
|
2020-07-16 22:40:18 +02:00
|
|
|
|
ls.set("drafts", {id1: draft_1});
|
2018-05-07 03:30:13 +02:00
|
|
|
|
draft_model.deleteDraft("id1");
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
2018-05-07 03:30:13 +02:00
|
|
|
|
assert.deepEqual(ls.get("drafts"), {});
|
2020-07-16 22:35:58 +02:00
|
|
|
|
})();
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-03-29 08:54:26 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("snapshot_message", () => {
|
2017-03-29 08:54:26 +02:00
|
|
|
|
function stub_draft(draft) {
|
2017-04-24 20:35:26 +02:00
|
|
|
|
global.compose_state.get_message_type = function () {
|
2017-03-29 08:54:26 +02:00
|
|
|
|
return draft.type;
|
|
|
|
|
};
|
2017-04-24 20:35:26 +02:00
|
|
|
|
global.compose_state.composing = function () {
|
|
|
|
|
return !!draft.type;
|
|
|
|
|
};
|
2017-04-15 01:15:59 +02:00
|
|
|
|
global.compose_state.message_content = function () {
|
2017-03-29 08:54:26 +02:00
|
|
|
|
return draft.content;
|
|
|
|
|
};
|
2019-12-02 17:53:55 +01:00
|
|
|
|
global.compose_state.private_message_recipient = function () {
|
2017-03-29 08:54:26 +02:00
|
|
|
|
return draft.private_message_recipient;
|
|
|
|
|
};
|
2017-04-15 01:15:59 +02:00
|
|
|
|
global.compose_state.stream_name = function () {
|
2017-03-29 08:54:26 +02:00
|
|
|
|
return draft.stream;
|
|
|
|
|
};
|
2018-11-04 17:04:17 +01:00
|
|
|
|
global.compose_state.topic = function () {
|
2018-12-16 17:36:41 +01:00
|
|
|
|
return draft.topic;
|
2017-03-29 08:54:26 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stub_draft(draft_1);
|
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_1);
|
|
|
|
|
|
|
|
|
|
stub_draft(draft_2);
|
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_2);
|
|
|
|
|
|
2018-08-21 23:04:30 +02:00
|
|
|
|
stub_draft(short_msg);
|
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), undefined);
|
|
|
|
|
|
2017-03-29 08:54:26 +02:00
|
|
|
|
stub_draft({});
|
|
|
|
|
assert.equal(drafts.snapshot_message(), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("initialize", () => {
|
2017-06-02 23:12:15 +02:00
|
|
|
|
global.window.addEventListener = function (event_name, f) {
|
|
|
|
|
assert.equal(event_name, "beforeunload");
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let called = false;
|
2020-07-15 00:34:28 +02:00
|
|
|
|
drafts.update_draft = function () {
|
|
|
|
|
called = true;
|
|
|
|
|
};
|
2017-06-02 23:12:15 +02:00
|
|
|
|
f();
|
|
|
|
|
assert(called);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
drafts.initialize();
|
2020-07-21 00:23:06 +02:00
|
|
|
|
|
|
|
|
|
const message_content = $("#compose-textarea");
|
|
|
|
|
assert.equal(message_content.get_on_handler("focusout"), drafts.update_draft);
|
|
|
|
|
message_content.trigger("focusout");
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2018-02-15 17:38:31 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_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",
|
|
|
|
|
content: "Test Stream Message",
|
|
|
|
|
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",
|
|
|
|
|
content: "Test Private Message",
|
|
|
|
|
updatedAt: new Date().setDate(-30),
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
|
const ls = localstorage();
|
2018-02-15 17:38:31 +01:00
|
|
|
|
localStorage.clear();
|
2019-11-02 00:06:25 +01:00
|
|
|
|
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
|
|
|
|
|
2020-07-26 16:38:46 +02:00
|
|
|
|
run_test("format_drafts", (override) => {
|
|
|
|
|
override("drafts.remove_old_drafts", noop);
|
2019-02-20 15:45:17 +01:00
|
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
|
draft_1.updatedAt = new Date(1549958107000).getTime(); // 2/12/2019 07:55:07 AM (UTC+0)
|
2018-03-11 22:03:42 +01:00
|
|
|
|
draft_2.updatedAt = new Date(1549958107000).setDate(-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",
|
|
|
|
|
content: "Test Stream Message 2",
|
|
|
|
|
updatedAt: new Date(1549958107000).setDate(-10),
|
|
|
|
|
};
|
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",
|
|
|
|
|
content: "Test Private Message 2",
|
|
|
|
|
updatedAt: new Date(1549958107000).setDate(-5),
|
|
|
|
|
};
|
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",
|
|
|
|
|
content: "Test Private Message 3",
|
|
|
|
|
updatedAt: new Date(1549958107000).setDate(-2),
|
|
|
|
|
};
|
|
|
|
|
|
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",
|
|
|
|
|
raw_content: "Test Stream Message",
|
|
|
|
|
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-07-15 01:29:15 +02:00
|
|
|
|
recipients: "aaron",
|
|
|
|
|
raw_content: "Test Private Message",
|
|
|
|
|
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-07-15 01:29:15 +02:00
|
|
|
|
recipients: "aaron",
|
|
|
|
|
raw_content: "Test Private Message 3",
|
|
|
|
|
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-07-15 01:29:15 +02:00
|
|
|
|
recipients: "aaron",
|
|
|
|
|
raw_content: "Test Private Message 2",
|
|
|
|
|
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",
|
|
|
|
|
raw_content: "Test Stream Message 2",
|
|
|
|
|
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();
|
2018-03-11 22:03:42 +01:00
|
|
|
|
localStorage.clear();
|
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;
|
2018-03-11 22:03:42 +01:00
|
|
|
|
timerender.render_now = function (time) {
|
|
|
|
|
return stub_render_now(time, new XDate(1549958107000));
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
|
global.stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
assert.equal(template_name, "draft_table_body");
|
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
|
|
|
|
|
2020-07-26 16:38:46 +02:00
|
|
|
|
override("drafts.open_overlay", noop);
|
2018-12-16 21:10:20 +01:00
|
|
|
|
drafts.set_initial_element = noop;
|
2019-04-18 21:11:30 +02:00
|
|
|
|
$("#drafts_table .draft-row").length = 0;
|
|
|
|
|
|
2018-12-16 21:10:20 +01:00
|
|
|
|
drafts.launch();
|
2018-03-11 22:03:42 +01:00
|
|
|
|
timerender.render_now = stub_render_now;
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|