2018-02-12 15:12:40 +01:00
|
|
|
zrequire('submessage');
|
|
|
|
|
|
|
|
set_global('channel', {});
|
2018-05-30 20:36:09 +02:00
|
|
|
set_global('widgetize', {});
|
2018-05-30 22:52:11 +02:00
|
|
|
set_global('message_store', {});
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2018-05-30 20:29:49 +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 = [
|
2018-02-12 15:12:40 +01:00
|
|
|
{id: 222, sender_id: 99, content: '84'},
|
|
|
|
{id: 9, sender_id: 33, content: '42'},
|
|
|
|
];
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-30 20:29:49 +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, {
|
|
|
|
url: '/json/submessage',
|
|
|
|
data: {
|
|
|
|
message_id: message_id,
|
|
|
|
msg_type: 'whatever',
|
|
|
|
content: '{"foo":32}',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
callback({
|
|
|
|
msg_type: 'whatever',
|
|
|
|
data: {foo: 32},
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(was_posted);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-05-30 20:36:09 +02:00
|
|
|
|
|
|
|
run_test('handle_event', () => {
|
|
|
|
const message = {
|
|
|
|
id: 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
const event = {
|
2018-05-30 22:52:11 +02:00
|
|
|
id: 11,
|
2018-05-30 20:36:09 +02:00
|
|
|
msg_type: 'widget',
|
|
|
|
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,
|
|
|
|
data: 'some_data',
|
|
|
|
});
|
2018-05-30 22:52:11 +02:00
|
|
|
|
|
|
|
assert.deepEqual(message.submessages[0], event);
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|