2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-15 16:33:31 +01:00
|
|
|
const sample_events = [
|
2021-02-28 18:48:44 +01:00
|
|
|
{
|
|
|
|
data: {
|
|
|
|
option: "First option",
|
|
|
|
idx: 1,
|
|
|
|
type: "new_option",
|
|
|
|
},
|
|
|
|
sender_id: 101,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
option: "Second option",
|
|
|
|
idx: 1,
|
|
|
|
type: "new_option",
|
|
|
|
},
|
|
|
|
sender_id: 102,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
option: "Third option",
|
|
|
|
idx: 1,
|
|
|
|
type: "new_option",
|
|
|
|
},
|
|
|
|
sender_id: 102,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-03-15 16:33:31 +01:00
|
|
|
let events;
|
2022-01-25 11:36:19 +01:00
|
|
|
let $widget_elem;
|
2024-05-08 16:16:22 +02:00
|
|
|
let handle_events;
|
2021-02-28 18:48:44 +01:00
|
|
|
let is_event_handled;
|
|
|
|
let is_widget_activated;
|
2021-03-15 16:33:31 +01:00
|
|
|
|
|
|
|
const fake_poll_widget = {
|
2021-02-28 18:48:44 +01:00
|
|
|
activate(data) {
|
|
|
|
is_widget_activated = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
$widget_elem = data.$elem;
|
|
|
|
assert.ok($widget_elem.hasClass("widget-content"));
|
2024-05-08 16:16:22 +02:00
|
|
|
handle_events = (e) => {
|
2021-02-28 18:48:44 +01:00
|
|
|
is_event_handled = true;
|
|
|
|
assert.deepStrictEqual(e, events);
|
|
|
|
};
|
|
|
|
data.callback("test_data");
|
2024-05-08 16:16:22 +02:00
|
|
|
return handle_events;
|
2021-02-28 18:48:44 +01:00
|
|
|
},
|
2021-03-15 16:33:31 +01:00
|
|
|
};
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2024-02-13 03:44:04 +01:00
|
|
|
const message_lists = mock_esm("../src/message_lists", {current: {id: 2}, home: {id: 1}});
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/poll_widget", fake_poll_widget);
|
2021-03-15 16:33:31 +01:00
|
|
|
|
|
|
|
set_global("document", "document-stub");
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const widgetize = zrequire("widgetize");
|
2023-10-06 19:22:44 +02:00
|
|
|
const widgets = zrequire("widgets");
|
|
|
|
|
|
|
|
widgets.initialize();
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2021-03-15 16:33:31 +01:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-03-15 16:33:31 +01:00
|
|
|
events = [...sample_events];
|
2022-01-25 11:36:19 +01:00
|
|
|
$widget_elem = undefined;
|
2024-05-08 16:16:22 +02:00
|
|
|
handle_events = undefined;
|
2021-03-15 16:33:31 +01:00
|
|
|
is_event_handled = false;
|
|
|
|
is_widget_activated = false;
|
|
|
|
widgetize.clear_for_testing();
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-03-15 16:33:31 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-08 16:16:22 +02:00
|
|
|
test("activate", ({override}) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
// Both widgetize.activate and widgetize.handle_event are tested
|
2024-05-08 16:16:22 +02:00
|
|
|
// here to use the "caching" of widget event handlers.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $.create("<stub message row>");
|
2024-05-08 16:16:22 +02:00
|
|
|
$row.length = 1;
|
2024-02-13 03:44:04 +01:00
|
|
|
$row.attr("id", `message-row-${message_lists.current.id}-2909`);
|
|
|
|
const $message_content = $.create(`#message-row-${message_lists.current.id}-2909`);
|
2022-01-25 11:36:19 +01:00
|
|
|
$row.set_find_results(".message_content", $message_content);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
const opts = {
|
2023-03-02 01:58:25 +01:00
|
|
|
events: [...events],
|
2020-07-15 01:29:15 +02:00
|
|
|
extra_data: "",
|
2018-07-03 08:59:28 +02:00
|
|
|
message: {
|
|
|
|
id: 2001,
|
|
|
|
},
|
2022-11-17 23:33:43 +01:00
|
|
|
post_to_server(data) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(data.msg_type, "widget");
|
|
|
|
assert.equal(data.data, "test_data");
|
2018-07-03 08:59:28 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$row,
|
2020-07-15 01:29:15 +02:00
|
|
|
widget_type: "poll",
|
2018-07-03 08:59:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let is_widget_elem_inserted;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$message_content.append = ($elem) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
is_widget_elem_inserted = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($elem, $widget_elem);
|
|
|
|
assert.ok($elem.hasClass("widget-content"));
|
2018-07-03 08:59:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
is_widget_elem_inserted = false;
|
|
|
|
is_widget_activated = false;
|
|
|
|
is_event_handled = false;
|
2024-05-08 16:16:22 +02:00
|
|
|
assert.ok(!widgetize.widget_event_handlers.has(opts.message.id));
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
widgetize.activate(opts);
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(is_widget_elem_inserted);
|
|
|
|
assert.ok(is_widget_activated);
|
|
|
|
assert.ok(is_event_handled);
|
2024-05-08 16:16:22 +02:00
|
|
|
assert.equal(widgetize.widget_event_handlers.get(opts.message.id), handle_events);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.current = undefined;
|
2018-07-03 08:59:28 +02:00
|
|
|
is_widget_elem_inserted = false;
|
|
|
|
is_widget_activated = false;
|
|
|
|
is_event_handled = false;
|
|
|
|
|
|
|
|
widgetize.activate(opts);
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!is_widget_elem_inserted);
|
|
|
|
assert.ok(!is_widget_activated);
|
|
|
|
assert.ok(!is_event_handled);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "unknown widget_type");
|
2018-07-03 08:59:28 +02:00
|
|
|
is_widget_elem_inserted = false;
|
|
|
|
is_widget_activated = false;
|
|
|
|
is_event_handled = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.widget_type = "invalid_widget";
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
widgetize.activate(opts);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!is_widget_elem_inserted);
|
|
|
|
assert.ok(!is_widget_activated);
|
|
|
|
assert.ok(!is_event_handled);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(blueslip.get_test_logs("warn")[0].more_info, "invalid_widget");
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-11-09 11:22:48 +01:00
|
|
|
opts.widget_type = "tictactoe";
|
|
|
|
|
|
|
|
widgetize.activate(opts);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!is_widget_elem_inserted);
|
|
|
|
assert.ok(!is_widget_activated);
|
|
|
|
assert.ok(!is_event_handled);
|
2020-11-09 11:22:48 +01:00
|
|
|
|
2018-07-03 08:59:28 +02:00
|
|
|
/* Testing widgetize.handle_events */
|
2024-05-08 16:16:22 +02:00
|
|
|
message_lists.current = {id: 2};
|
2018-07-03 08:59:28 +02:00
|
|
|
const post_activate_event = {
|
|
|
|
data: {
|
|
|
|
idx: 1,
|
2019-01-28 19:13:56 +01:00
|
|
|
type: "new_option",
|
2018-07-03 08:59:28 +02:00
|
|
|
},
|
|
|
|
message_id: 2001,
|
|
|
|
sender_id: 102,
|
|
|
|
};
|
2024-05-08 16:16:22 +02:00
|
|
|
handle_events = (e) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
is_event_handled = true;
|
|
|
|
assert.deepEqual(e, [post_activate_event]);
|
|
|
|
};
|
2024-05-08 16:16:22 +02:00
|
|
|
widgetize.widget_event_handlers.set(2001, handle_events);
|
|
|
|
override(message_lists.current, "get_row", (idx) => {
|
|
|
|
assert.equal(idx, 2001);
|
|
|
|
return $row;
|
|
|
|
});
|
2018-07-03 08:59:28 +02:00
|
|
|
is_event_handled = false;
|
|
|
|
widgetize.handle_event(post_activate_event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(is_event_handled);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
is_event_handled = false;
|
|
|
|
post_activate_event.message_id = 1000;
|
|
|
|
widgetize.handle_event(post_activate_event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!is_event_handled);
|
2018-07-03 08:59:28 +02:00
|
|
|
});
|