2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const {mock_esm, 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 channel = mock_esm("../../static/js/channel");
|
|
|
|
const message_store = mock_esm("../../static/js/message_store");
|
|
|
|
const widgetize = mock_esm("../../static/js/widgetize");
|
2021-02-28 01:10:03 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const submessage = zrequire("submessage");
|
|
|
|
|
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,
|
2020-07-20 22:18:43 +02:00
|
|
|
submessages,
|
2018-02-12 15:12:40 +01:00
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
2020-07-20 22:18:43 +02:00
|
|
|
submessages,
|
2018-02-12 15:12:40 +01:00
|
|
|
};
|
|
|
|
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
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2018-02-12 15:12:40 +01:00
|
|
|
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: {
|
2020-07-20 22:18:43 +02:00
|
|
|
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
|
|
|
});
|