2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const poll_widget = set_global("poll_widget", {});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", "document-stub");
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const narrow_state = set_global("narrow_state", {});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("current_msg_list", {});
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const widgetize = zrequire("widgetize");
|
|
|
|
|
2021-02-23 13:58:55 +01:00
|
|
|
run_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
|
2020-07-15 01:29:15 +02:00
|
|
|
const row = $.create("<stub message row>");
|
|
|
|
row.attr("id", "zhome2909");
|
|
|
|
const message_content = $.create("#zhome2909");
|
|
|
|
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 events = [
|
|
|
|
{
|
|
|
|
data: {
|
2019-01-28 19:13:56 +01:00
|
|
|
option: "First option",
|
2018-07-03 08:59:28 +02:00
|
|
|
idx: 1,
|
2019-01-28 19:13:56 +01:00
|
|
|
type: "new_option",
|
2018-07-03 08:59:28 +02:00
|
|
|
},
|
|
|
|
sender_id: 101,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: {
|
2019-01-28 19:13:56 +01:00
|
|
|
option: "Second option",
|
2018-07-03 08:59:28 +02:00
|
|
|
idx: 1,
|
2019-01-28 19:13:56 +01:00
|
|
|
type: "new_option",
|
2018-07-03 08:59:28 +02:00
|
|
|
},
|
|
|
|
sender_id: 102,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: {
|
2019-01-28 19:13:56 +01:00
|
|
|
option: "Third option",
|
2018-07-03 08:59:28 +02:00
|
|
|
idx: 1,
|
2019-01-28 19:13:56 +01:00
|
|
|
type: "new_option",
|
2018-07-03 08:59:28 +02:00
|
|
|
},
|
|
|
|
sender_id: 102,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const opts = {
|
2020-02-09 04:44:12 +01:00
|
|
|
events: events.slice(),
|
2020-07-15 01:29:15 +02:00
|
|
|
extra_data: "",
|
2018-07-03 08:59:28 +02:00
|
|
|
message: {
|
|
|
|
id: 2001,
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
row,
|
2020-07-15 01:29:15 +02:00
|
|
|
widget_type: "poll",
|
2018-07-03 08:59:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let widget_elem;
|
|
|
|
let is_event_handled;
|
|
|
|
let is_widget_activated;
|
|
|
|
let is_widget_elem_inserted;
|
|
|
|
|
2021-02-23 13:58:55 +01:00
|
|
|
override(poll_widget, "activate", (data) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
is_widget_activated = true;
|
|
|
|
widget_elem = data.elem;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(widget_elem.hasClass("widget-content"));
|
2018-07-03 08:59:28 +02:00
|
|
|
widget_elem.handle_events = (e) => {
|
|
|
|
is_event_handled = true;
|
|
|
|
assert.notDeepStrictEqual(e, events);
|
|
|
|
events.shift();
|
|
|
|
assert.deepStrictEqual(e, events);
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
data.callback("test_data");
|
2021-02-23 13:58:55 +01:00
|
|
|
});
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
message_content.append = (elem) => {
|
|
|
|
is_widget_elem_inserted = true;
|
|
|
|
assert.equal(elem, widget_elem);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(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;
|
2020-02-06 04:47:46 +01:00
|
|
|
assert(!widgetize.widget_contents.has(opts.message.id));
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
widgetize.activate(opts);
|
|
|
|
|
|
|
|
assert(is_widget_elem_inserted);
|
|
|
|
assert(is_widget_activated);
|
|
|
|
assert(is_event_handled);
|
2020-02-06 04:47:46 +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;
|
|
|
|
|
|
|
|
widgetize.activate(opts);
|
|
|
|
|
|
|
|
assert(is_widget_elem_inserted);
|
|
|
|
assert(!is_widget_activated);
|
|
|
|
assert(!is_event_handled);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
widgetize.activate(opts);
|
|
|
|
|
|
|
|
assert(!is_widget_elem_inserted);
|
|
|
|
assert(!is_widget_activated);
|
|
|
|
assert(!is_event_handled);
|
|
|
|
|
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);
|
|
|
|
assert(!is_widget_elem_inserted);
|
|
|
|
assert(!is_widget_activated);
|
|
|
|
assert(!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);
|
|
|
|
assert(!is_widget_elem_inserted);
|
|
|
|
assert(!is_widget_activated);
|
|
|
|
assert(!is_event_handled);
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
widget_elem.handle_events = (e) => {
|
|
|
|
is_event_handled = true;
|
|
|
|
assert.deepEqual(e, [post_activate_event]);
|
|
|
|
};
|
|
|
|
is_event_handled = false;
|
|
|
|
widgetize.handle_event(post_activate_event);
|
|
|
|
assert(is_event_handled);
|
|
|
|
|
|
|
|
is_event_handled = false;
|
|
|
|
post_activate_event.message_id = 1000;
|
|
|
|
widgetize.handle_event(post_activate_event);
|
|
|
|
assert(!is_event_handled);
|
|
|
|
|
|
|
|
/* Test narrow change message update */
|
2021-02-23 13:58:55 +01:00
|
|
|
override(current_msg_list, "get", (idx) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
assert.equal(idx, 2001);
|
|
|
|
return {};
|
2021-02-23 13:58:55 +01:00
|
|
|
});
|
|
|
|
override(current_msg_list, "get_row", (idx) => {
|
2018-07-03 08:59:28 +02:00
|
|
|
assert.equal(idx, 2001);
|
|
|
|
return row;
|
2021-02-23 13:58:55 +01:00
|
|
|
});
|
2018-07-03 08:59:28 +02:00
|
|
|
widgetize.set_widgets_for_list();
|
|
|
|
});
|