2020-07-15 01:29:15 +02:00
|
|
|
set_global("$", global.make_zjquery());
|
|
|
|
set_global("poll_widget", {});
|
|
|
|
set_global("tictactoe_widget", {});
|
|
|
|
set_global("todo_widget", {});
|
|
|
|
set_global("zform", {});
|
|
|
|
set_global("document", "document-stub");
|
2018-07-03 08:59:28 +02:00
|
|
|
|
|
|
|
const return_true = () => true;
|
|
|
|
const return_false = () => false;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("widgetize");
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("narrow_state", {});
|
|
|
|
set_global("current_msg_list", {});
|
2018-07-03 08:59:28 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("activate", () => {
|
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
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
narrow_state.active = return_false;
|
|
|
|
|
|
|
|
let widget_elem;
|
|
|
|
let is_event_handled;
|
|
|
|
let is_widget_activated;
|
|
|
|
let is_widget_elem_inserted;
|
|
|
|
|
2018-12-17 04:46:27 +01:00
|
|
|
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");
|
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);
|
|
|
|
|
|
|
|
narrow_state.active = return_true;
|
|
|
|
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");
|
2018-07-03 08:59:28 +02:00
|
|
|
narrow_state.active = return_false;
|
|
|
|
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
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
current_msg_list.get = (idx) => {
|
|
|
|
assert.equal(idx, 2001);
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
current_msg_list.get_row = (idx) => {
|
|
|
|
assert.equal(idx, 2001);
|
|
|
|
return row;
|
|
|
|
};
|
|
|
|
widgetize.set_widgets_for_list();
|
|
|
|
});
|