2020-07-15 01:29:15 +02:00
|
|
|
zrequire("submessage");
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("channel", {});
|
|
|
|
set_global("widgetize", {});
|
|
|
|
set_global("message_store", {});
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_message_events", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let msg = {};
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
submessages: [],
|
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const submessages = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{id: 222, sender_id: 99, content: "84"},
|
|
|
|
{id: 9, sender_id: 33, content: "42"},
|
2018-02-12 15:12:40 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
locally_echoed: true,
|
|
|
|
submessages: submessages,
|
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
submessages: submessages,
|
|
|
|
};
|
|
|
|
assert.deepEqual(submessage.get_message_events(msg), [
|
|
|
|
{sender_id: 33, data: 42},
|
|
|
|
{sender_id: 99, data: 84},
|
|
|
|
]);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("make_server_callback", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = 444;
|
|
|
|
const callback = submessage.make_server_callback(message_id);
|
|
|
|
let was_posted;
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
channel.post = function (opts) {
|
|
|
|
was_posted = true;
|
|
|
|
assert.deepEqual(opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/submessage",
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {
|
|
|
|
message_id: message_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "whatever",
|
2018-02-12 15:12:40 +01:00
|
|
|
content: '{"foo":32}',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
callback({
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "whatever",
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {foo: 32},
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(was_posted);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-05-30 20:36:09 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("handle_event", () => {
|
2018-05-30 20:36:09 +02:00
|
|
|
const message = {
|
|
|
|
id: 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
const event = {
|
2018-05-30 22:52:11 +02:00
|
|
|
id: 11,
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "widget",
|
2018-05-30 20:36:09 +02:00
|
|
|
sender_id: 99,
|
|
|
|
message_id: message.id,
|
2018-05-30 21:12:16 +02:00
|
|
|
content: '"some_data"',
|
2018-05-30 20:36:09 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let args;
|
2018-05-30 20:36:09 +02:00
|
|
|
widgetize.handle_event = (opts) => {
|
|
|
|
args = opts;
|
|
|
|
};
|
|
|
|
|
2018-05-30 22:52:11 +02:00
|
|
|
message_store.get = (msg_id) => {
|
|
|
|
assert.equal(msg_id, message.id);
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2018-05-30 20:36:09 +02:00
|
|
|
submessage.handle_event(event);
|
|
|
|
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
sender_id: 99,
|
|
|
|
message_id: 42,
|
2020-07-15 01:29:15 +02:00
|
|
|
data: "some_data",
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|
2018-05-30 22:52:11 +02:00
|
|
|
|
|
|
|
assert.deepEqual(message.submessages[0], event);
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|