2017-11-08 17:12:12 +01:00
|
|
|
zrequire('localstorage');
|
|
|
|
zrequire('drafts');
|
|
|
|
|
2017-06-02 23:12:15 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
set_global('window', {});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
var ls_container = {};
|
|
|
|
set_global('localStorage', {
|
|
|
|
getItem: function (key) {
|
|
|
|
return ls_container[key];
|
|
|
|
},
|
|
|
|
setItem: function (key, val) {
|
|
|
|
ls_container[key] = val;
|
|
|
|
},
|
|
|
|
removeItem: function (key) {
|
|
|
|
delete ls_container[key];
|
|
|
|
},
|
|
|
|
clear: function () {
|
|
|
|
ls_container = {};
|
|
|
|
},
|
|
|
|
});
|
2017-03-29 08:54:26 +02:00
|
|
|
set_global('compose', {});
|
|
|
|
set_global('compose_state', {});
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2017-03-29 08:22:25 +02:00
|
|
|
function stub_timestamp(timestamp, func) {
|
|
|
|
var original_func = Date.prototype.getTime;
|
|
|
|
Date.prototype.getTime = function () {
|
2017-02-22 02:34:05 +01:00
|
|
|
return timestamp;
|
|
|
|
};
|
|
|
|
func();
|
2017-03-29 08:22:25 +02:00
|
|
|
Date.prototype.getTime = original_func;
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var draft_1 = {
|
|
|
|
stream: "stream",
|
|
|
|
subject: "topic",
|
|
|
|
type: "stream",
|
|
|
|
content: "Test Stream Message",
|
|
|
|
};
|
|
|
|
var draft_2 = {
|
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
|
|
|
(function test_draft_model() {
|
|
|
|
var draft_model = drafts.draft_model;
|
|
|
|
var ls = localstorage();
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
(function test_get() {
|
|
|
|
var expected = { id1: draft_1, id2: draft_2 };
|
|
|
|
ls.set("drafts", expected);
|
|
|
|
|
|
|
|
assert.deepEqual(draft_model.get(), expected);
|
|
|
|
}());
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
(function test_get() {
|
|
|
|
ls.set("drafts", { id1: draft_1 });
|
|
|
|
|
|
|
|
assert.deepEqual(draft_model.getDraft("id1"), draft_1);
|
|
|
|
assert.equal(draft_model.getDraft("id2"), false);
|
|
|
|
}());
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
(function test_addDraft() {
|
2017-03-29 08:22:25 +02:00
|
|
|
stub_timestamp(1, function () {
|
|
|
|
var expected = _.clone(draft_1);
|
2017-02-22 02:34:05 +01:00
|
|
|
expected.updatedAt = 1;
|
2017-03-29 08:22:25 +02:00
|
|
|
var id = draft_model.addDraft(_.clone(draft_1));
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
assert.deepEqual(ls.get("drafts")[id], expected);
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
(function test_editDraft() {
|
2017-03-29 08:22:25 +02:00
|
|
|
stub_timestamp(2, function () {
|
2017-10-06 21:36:39 +02:00
|
|
|
ls.set("drafts", { id1: draft_1 });
|
2017-03-29 08:22:25 +02:00
|
|
|
var expected = _.clone(draft_2);
|
2017-02-22 02:34:05 +01:00
|
|
|
expected.updatedAt = 2;
|
2017-03-29 08:22:25 +02:00
|
|
|
draft_model.editDraft("id1", _.clone(draft_2));
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
assert.deepEqual(ls.get("drafts").id1, expected);
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
(function test_deleteDraft() {
|
2017-10-06 21:36:39 +02:00
|
|
|
ls.set("drafts", { id1: draft_1 });
|
2017-02-22 02:34:05 +01:00
|
|
|
draft_model.deleteDraft("id1");
|
|
|
|
|
|
|
|
assert.deepEqual(ls.get("drafts"), {});
|
|
|
|
}());
|
|
|
|
}());
|
2017-03-29 08:54:26 +02:00
|
|
|
|
|
|
|
(function test_snapshot_message() {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
global.compose_state.recipient = function () {
|
|
|
|
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;
|
|
|
|
};
|
2017-04-15 01:15:59 +02:00
|
|
|
global.compose_state.subject = function () {
|
2017-03-29 08:54:26 +02:00
|
|
|
return draft.subject;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
stub_draft(draft_1);
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_1);
|
|
|
|
|
|
|
|
stub_draft(draft_2);
|
|
|
|
assert.deepEqual(drafts.snapshot_message(), draft_2);
|
|
|
|
|
|
|
|
stub_draft({});
|
|
|
|
assert.equal(drafts.snapshot_message(), undefined);
|
|
|
|
}());
|
2017-06-02 23:12:15 +02:00
|
|
|
|
|
|
|
(function test_initialize() {
|
2017-11-26 20:37:44 +01:00
|
|
|
var message_content = $("#compose-textarea");
|
2017-06-02 23:12:15 +02:00
|
|
|
message_content.focusout = function (f) {
|
|
|
|
assert.equal(f, drafts.update_draft);
|
|
|
|
f();
|
|
|
|
};
|
|
|
|
|
|
|
|
global.window.addEventListener = function (event_name, f) {
|
|
|
|
assert.equal(event_name, "beforeunload");
|
|
|
|
var called = false;
|
|
|
|
drafts.update_draft = function () { called = true; };
|
|
|
|
f();
|
|
|
|
assert(called);
|
|
|
|
};
|
|
|
|
|
|
|
|
drafts.initialize();
|
|
|
|
}());
|