2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
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;
|
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"));
|
|
|
|
$widget_elem.handle_events = (e) => {
|
2021-02-28 18:48:44 +01:00
|
|
|
is_event_handled = true;
|
|
|
|
assert.notDeepStrictEqual(e, events);
|
|
|
|
events.shift();
|
|
|
|
assert.deepStrictEqual(e, events);
|
|
|
|
};
|
|
|
|
data.callback("test_data");
|
|
|
|
},
|
2021-03-15 16:33:31 +01:00
|
|
|
};
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2024-01-17 07:53:40 +01:00
|
|
|
const message_lists = mock_esm("../src/message_lists", {current: {}, home: {id: 1}});
|
2023-02-22 23:04:10 +01:00
|
|
|
const narrow_state = mock_esm("../src/narrow_state");
|
|
|
|
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;
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("activate", ({override}) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
// Both widgetize.activate and widgetize.handle_event are tested
|
|
|
|
// here to use the "caching" of widgets
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $.create("<stub message row>");
|
2024-01-17 07:53:40 +01:00
|
|
|
$row.attr("id", "message-row-1-2909");
|
|
|
|
const $message_content = $.create("#message-row-1-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
|
|
|
|
2021-02-23 13:58:55 +01:00
|
|
|
let narrow_active;
|
|
|
|
override(narrow_state, "active", () => narrow_active);
|
|
|
|
|
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;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!widgetize.widget_contents.has(opts.message.id));
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$message_content.set_find_results(".widget-content", false);
|
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);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal(widgetize.widget_contents.get(opts.message.id), $widget_elem);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
is_widget_elem_inserted = false;
|
|
|
|
is_widget_activated = false;
|
|
|
|
is_event_handled = false;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$message_content.set_find_results(".widget-content", false);
|
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);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2021-02-23 13:58:55 +01:00
|
|
|
narrow_active = true;
|
2018-07-03 08:59:28 +02:00
|
|
|
is_widget_elem_inserted = false;
|
|
|
|
is_widget_activated = false;
|
|
|
|
is_event_handled = false;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$message_content.set_find_results(".widget-content", false);
|
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);
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "unknown widget_type");
|
2021-02-23 13:58:55 +01:00
|
|
|
narrow_active = false;
|
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 */
|
|
|
|
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,
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
$widget_elem.handle_events = (e) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
is_event_handled = true;
|
|
|
|
assert.deepEqual(e, [post_activate_event]);
|
|
|
|
};
|
|
|
|
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
|
|
|
|
|
|
|
/* Test narrow change message update */
|
2021-03-30 02:21:21 +02:00
|
|
|
override(message_lists.current, "get", (idx) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
assert.equal(idx, 2001);
|
|
|
|
return {};
|
2021-02-23 13:58:55 +01:00
|
|
|
});
|
2021-03-30 02:21:21 +02:00
|
|
|
override(message_lists.current, "get_row", (idx) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
assert.equal(idx, 2001);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $row;
|
2021-02-23 13:58:55 +01:00
|
|
|
});
|
2018-07-03 08:59:28 +02:00
|
|
|
widgetize.set_widgets_for_list();
|
|
|
|
});
|