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:19:42 +01:00
|
|
|
const {stub_templates} = require("../zjsunit/handlebars");
|
2021-03-11 05:43:45 +01:00
|
|
|
const {mock_cjs, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const poll_widget = zrequire("poll_widget");
|
2018-07-02 16:48:23 +02:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
run_test("PollData my question", () => {
|
2018-07-02 16:48:23 +02:00
|
|
|
const is_my_poll = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
const question = "Favorite color?";
|
2018-07-02 16:48:23 +02:00
|
|
|
|
|
|
|
const sender_id = 99;
|
|
|
|
people.my_current_user_id = () => sender_id;
|
|
|
|
|
2020-07-23 02:04:27 +02:00
|
|
|
const data_holder = new poll_widget.PollData(is_my_poll, question, []);
|
2018-07-02 16:48:23 +02:00
|
|
|
|
2018-07-05 13:33:58 +02:00
|
|
|
let data = data_holder.get_widget_data();
|
2018-07-02 16:48:23 +02:00
|
|
|
|
|
|
|
assert.deepEqual(data, {
|
2019-01-28 19:13:56 +01:00
|
|
|
options: [],
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "Favorite color?",
|
2018-07-02 16:48:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const question_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "question",
|
|
|
|
question: "best plan?",
|
2018-07-02 16:48:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
data_holder.handle_event(sender_id, question_event);
|
|
|
|
data = data_holder.get_widget_data();
|
|
|
|
|
|
|
|
assert.deepEqual(data, {
|
2019-01-28 19:13:56 +01:00
|
|
|
options: [],
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "best plan?",
|
2018-07-02 16:48:23 +02:00
|
|
|
});
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
const option_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "new_option",
|
2018-07-02 16:48:23 +02:00
|
|
|
idx: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
option: "release now",
|
2018-07-02 16:48:23 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
people.safe_full_names = () => "";
|
2018-07-02 16:48:23 +02:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
data_holder.handle_event(sender_id, option_event);
|
2018-07-02 16:48:23 +02:00
|
|
|
data = data_holder.get_widget_data();
|
|
|
|
|
|
|
|
assert.deepEqual(data, {
|
2019-01-28 19:13:56 +01:00
|
|
|
options: [
|
2018-07-02 16:48:23 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
option: "release now",
|
|
|
|
names: "",
|
2018-07-02 16:48:23 +02:00
|
|
|
count: 0,
|
2020-07-15 01:29:15 +02:00
|
|
|
key: "99,1",
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: false,
|
2018-07-02 16:48:23 +02:00
|
|
|
},
|
|
|
|
],
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "best plan?",
|
2018-07-02 16:48:23 +02:00
|
|
|
});
|
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
let vote_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "vote",
|
|
|
|
key: "99,1",
|
2018-07-02 16:48:23 +02:00
|
|
|
vote: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
data_holder.handle_event(sender_id, vote_event);
|
|
|
|
data = data_holder.get_widget_data();
|
|
|
|
|
|
|
|
assert.deepEqual(data, {
|
2019-01-28 19:13:56 +01:00
|
|
|
options: [
|
2018-07-02 16:48:23 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
option: "release now",
|
|
|
|
names: "",
|
2018-07-02 16:48:23 +02:00
|
|
|
count: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
key: "99,1",
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: true,
|
2018-07-02 16:48:23 +02:00
|
|
|
},
|
|
|
|
],
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "best plan?",
|
2018-07-02 16:48:23 +02:00
|
|
|
});
|
|
|
|
|
2018-07-05 13:33:58 +02:00
|
|
|
const invalid_vote_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "vote",
|
|
|
|
key: "98,1",
|
2018-07-05 13:33:58 +02:00
|
|
|
vote: 1,
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", `unknown key for poll: ${invalid_vote_event.key}`);
|
2018-07-05 13:33:58 +02:00
|
|
|
data_holder.handle_event(sender_id, invalid_vote_event);
|
|
|
|
data = data_holder.get_widget_data();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const option_outbound_event = data_holder.handle.new_option.outbound("new option");
|
2019-01-28 19:13:56 +01:00
|
|
|
assert.deepEqual(option_outbound_event, {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "new_option",
|
2018-07-05 13:33:58 +02:00
|
|
|
idx: 2,
|
2020-07-15 01:29:15 +02:00
|
|
|
option: "new option",
|
2018-07-05 13:33:58 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const new_question = "Any new plan?";
|
2018-07-05 13:33:58 +02:00
|
|
|
const question_outbound_event = data_holder.handle.question.outbound(new_question);
|
|
|
|
assert.deepEqual(question_outbound_event, {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "question",
|
2018-07-05 13:33:58 +02:00
|
|
|
question: new_question,
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const vote_outbound_event = data_holder.handle.vote.outbound("99,1");
|
2020-07-16 22:40:18 +02:00
|
|
|
assert.deepEqual(vote_outbound_event, {type: "vote", key: "99,1", vote: -1});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
vote_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "vote",
|
|
|
|
key: "99,1",
|
2018-07-05 13:33:58 +02:00
|
|
|
vote: -1,
|
|
|
|
};
|
|
|
|
|
|
|
|
data_holder.handle_event(sender_id, vote_event);
|
|
|
|
data = data_holder.get_widget_data();
|
|
|
|
|
|
|
|
assert.deepEqual(data, {
|
2019-01-28 19:13:56 +01:00
|
|
|
options: [
|
2018-07-05 13:33:58 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
option: "release now",
|
|
|
|
names: "",
|
2018-07-05 13:33:58 +02:00
|
|
|
count: 0,
|
2020-07-15 01:29:15 +02:00
|
|
|
key: "99,1",
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: false,
|
2018-07-05 13:33:58 +02:00
|
|
|
},
|
|
|
|
],
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "best plan?",
|
2018-07-05 13:33:58 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("activate another person poll", () => {
|
2021-02-26 12:49:16 +01:00
|
|
|
people.is_my_user_id = () => false;
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "widgets/poll_widget") {
|
|
|
|
return "widgets/poll_widget";
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "widgets/poll_widget_results") {
|
|
|
|
return "widgets/poll_widget_results";
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
throw new Error(`Unknown template ${template_name}`);
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const widget_elem = $("<div>").addClass("widget-content");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
let out_data; // Used to check the event data sent to the server
|
2018-07-05 13:33:58 +02:00
|
|
|
const callback = (data) => {
|
|
|
|
out_data = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
elem: widget_elem,
|
2020-07-20 22:18:43 +02:00
|
|
|
callback,
|
2018-07-05 13:33:58 +02:00
|
|
|
message: {
|
|
|
|
sender_id: 100,
|
|
|
|
},
|
2019-01-09 17:55:42 +01:00
|
|
|
extra_data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "What do you want?",
|
2019-01-09 17:55:42 +01:00
|
|
|
},
|
2018-07-05 13:33:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const set_widget_find_result = (selector) => {
|
|
|
|
const elem = $.create(selector);
|
|
|
|
widget_elem.set_find_results(selector, elem);
|
|
|
|
return elem;
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const poll_option = set_widget_find_result("button.poll-option");
|
|
|
|
const poll_option_input = set_widget_find_result("input.poll-option");
|
|
|
|
const widget_option_container = set_widget_find_result("ul.poll-widget");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const poll_question_submit = set_widget_find_result("button.poll-question-check");
|
|
|
|
const poll_edit_question = set_widget_find_result(".poll-edit-question");
|
|
|
|
const poll_question_header = set_widget_find_result(".poll-question-header");
|
|
|
|
const poll_question_container = set_widget_find_result(".poll-question-bar");
|
|
|
|
const poll_option_container = set_widget_find_result(".poll-option-bar");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const poll_vote_button = set_widget_find_result("button.poll-vote");
|
|
|
|
const poll_please_wait = set_widget_find_result(".poll-please-wait");
|
|
|
|
const poll_author_help = set_widget_find_result(".poll-author-help");
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_widget_find_result("button.poll-question-remove");
|
|
|
|
set_widget_find_result("input.poll-question");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2021-02-09 13:21:48 +01:00
|
|
|
poll_widget.activate(opts);
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2021-02-09 13:21:48 +01:00
|
|
|
assert(poll_option_container.visible());
|
|
|
|
assert(poll_question_header.visible());
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2021-02-09 13:21:48 +01:00
|
|
|
assert(!poll_question_container.visible());
|
|
|
|
assert(!poll_question_submit.visible());
|
|
|
|
assert(!poll_edit_question.visible());
|
|
|
|
assert(!poll_please_wait.visible());
|
|
|
|
assert(!poll_author_help.visible());
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(widget_elem.html(), "widgets/poll_widget");
|
|
|
|
assert.equal(widget_option_container.html(), "widgets/poll_widget_results");
|
|
|
|
assert.equal(poll_question_header.text(), "What do you want?");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
{
|
2019-01-28 19:13:56 +01:00
|
|
|
/* Testing data sent to server on adding option */
|
2020-07-15 01:29:15 +02:00
|
|
|
poll_option_input.val("cool choice");
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2020-07-21 00:23:06 +02:00
|
|
|
poll_option.trigger("click");
|
2020-07-16 23:29:01 +02:00
|
|
|
assert.deepEqual(out_data, {type: "new_option", idx: 1, option: "cool choice"});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
poll_option_input.val("");
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2020-07-21 00:23:06 +02:00
|
|
|
poll_option.trigger("click");
|
2018-07-05 13:33:58 +02:00
|
|
|
assert.deepEqual(out_data, undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
const vote_events = [
|
|
|
|
{
|
|
|
|
sender_id: 100,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "new_option",
|
2018-07-05 13:33:58 +02:00
|
|
|
idx: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
option: "release now",
|
2018-07-05 13:33:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sender_id: 100,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "vote",
|
|
|
|
key: "100,1",
|
2018-07-05 13:33:58 +02:00
|
|
|
vote: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
widget_elem.handle_events(vote_events);
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Testing data sent to server on voting */
|
2020-07-15 01:29:15 +02:00
|
|
|
poll_vote_button.attr("data-key", "100,1");
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2020-12-11 04:07:01 +01:00
|
|
|
poll_vote_button.trigger("click");
|
2020-07-16 22:40:18 +02:00
|
|
|
assert.deepEqual(out_data, {type: "vote", key: "100,1", vote: 1});
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const add_question_event = [
|
|
|
|
{
|
|
|
|
sender_id: 100,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "question",
|
|
|
|
question: "best plan?",
|
2018-07-05 13:33:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
widget_elem.handle_events(add_question_event);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("activate own poll", () => {
|
2021-02-26 12:49:16 +01:00
|
|
|
people.is_my_user_id = () => true;
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "widgets/poll_widget") {
|
|
|
|
return "widgets/poll_widget";
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "widgets/poll_widget_results") {
|
|
|
|
return "widgets/poll_widget_results";
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
throw new Error(`Unknown template ${template_name}`);
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const widget_elem = $("<div>").addClass("widget-content");
|
2018-07-05 13:33:58 +02:00
|
|
|
let out_data;
|
|
|
|
const callback = (data) => {
|
|
|
|
out_data = data;
|
|
|
|
};
|
|
|
|
const opts = {
|
|
|
|
elem: widget_elem,
|
2020-07-20 22:18:43 +02:00
|
|
|
callback,
|
2018-07-05 13:33:58 +02:00
|
|
|
message: {
|
|
|
|
sender_id: 100,
|
|
|
|
},
|
|
|
|
extra_data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
question: "Where to go?",
|
2018-07-05 13:33:58 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const set_widget_find_result = (selector) => {
|
|
|
|
const elem = $.create(selector);
|
|
|
|
widget_elem.set_find_results(selector, elem);
|
|
|
|
return elem;
|
|
|
|
};
|
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
set_widget_find_result("button.poll-option");
|
2020-07-15 01:29:15 +02:00
|
|
|
const poll_option_input = set_widget_find_result("input.poll-option");
|
|
|
|
const widget_option_container = set_widget_find_result("ul.poll-widget");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const poll_question_submit = set_widget_find_result("button.poll-question-check");
|
|
|
|
const poll_edit_question = set_widget_find_result(".poll-edit-question");
|
|
|
|
const poll_question_input = set_widget_find_result("input.poll-question");
|
|
|
|
const poll_question_header = set_widget_find_result(".poll-question-header");
|
|
|
|
const poll_question_container = set_widget_find_result(".poll-question-bar");
|
|
|
|
const poll_option_container = set_widget_find_result(".poll-option-bar");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
set_widget_find_result("button.poll-vote");
|
2020-07-15 01:29:15 +02:00
|
|
|
const poll_please_wait = set_widget_find_result(".poll-please-wait");
|
|
|
|
const poll_author_help = set_widget_find_result(".poll-author-help");
|
2019-01-09 17:55:42 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_widget_find_result("button.poll-question-remove");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2021-02-09 13:21:48 +01:00
|
|
|
function assert_visibility() {
|
|
|
|
assert(poll_option_container.visible());
|
|
|
|
assert(poll_question_header.visible());
|
|
|
|
assert(!poll_question_container.visible());
|
|
|
|
assert(poll_edit_question.visible());
|
|
|
|
assert(!poll_please_wait.visible());
|
|
|
|
assert(!poll_author_help.visible());
|
|
|
|
}
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2018-12-17 04:46:27 +01:00
|
|
|
poll_widget.activate(opts);
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2021-02-09 13:21:48 +01:00
|
|
|
assert_visibility();
|
|
|
|
assert(!poll_question_submit.visible());
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(widget_elem.html(), "widgets/poll_widget");
|
|
|
|
assert.equal(widget_option_container.html(), "widgets/poll_widget_results");
|
|
|
|
assert.equal(poll_question_header.text(), "Where to go?");
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
/* Testing data sent to server on editing question */
|
2020-07-15 01:29:15 +02:00
|
|
|
poll_question_input.val("Is it new?");
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2020-07-21 00:23:06 +02:00
|
|
|
poll_question_submit.trigger("click");
|
2020-07-16 23:29:01 +02:00
|
|
|
assert.deepEqual(out_data, {type: "question", question: "Is it new?"});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2021-02-09 13:21:48 +01:00
|
|
|
assert_visibility();
|
|
|
|
assert(poll_question_submit.visible());
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
poll_option_input.val("");
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2020-07-21 00:23:06 +02:00
|
|
|
poll_question_submit.trigger("click");
|
2018-07-05 13:33:58 +02:00
|
|
|
assert.deepEqual(out_data, undefined);
|
|
|
|
}
|
2018-07-02 16:48:23 +02:00
|
|
|
});
|