2018-12-17 04:46:27 +01:00
|
|
|
zrequire('poll_widget');
|
2018-07-02 16:48:23 +02:00
|
|
|
|
2018-07-05 13:33:58 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
set_global('i18n', global.stub_i18n);
|
|
|
|
|
2018-07-02 16:48:23 +02:00
|
|
|
set_global('people', {});
|
2019-01-28 21:50:35 +01:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
const noop = () => {};
|
|
|
|
const return_false = () => false;
|
|
|
|
const return_true = () => true;
|
2018-07-02 16:48:23 +02:00
|
|
|
|
|
|
|
run_test('poll_data_holder my question', () => {
|
|
|
|
const is_my_poll = true;
|
|
|
|
const question = 'Favorite color?';
|
|
|
|
|
|
|
|
const sender_id = 99;
|
|
|
|
people.my_current_user_id = () => sender_id;
|
|
|
|
|
2018-12-17 04:46:27 +01:00
|
|
|
const data_holder = poll_widget.poll_data_holder(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: [],
|
2018-07-02 16:48:23 +02:00
|
|
|
question: 'Favorite color?',
|
|
|
|
});
|
|
|
|
|
|
|
|
const question_event = {
|
|
|
|
type: 'question',
|
|
|
|
question: 'best plan?',
|
|
|
|
};
|
|
|
|
|
|
|
|
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: [],
|
2018-07-02 16:48:23 +02:00
|
|
|
question: 'best plan?',
|
|
|
|
});
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
const option_event = {
|
|
|
|
type: 'new_option',
|
2018-07-02 16:48:23 +02:00
|
|
|
idx: 1,
|
2019-01-28 19:13:56 +01:00
|
|
|
option: 'release now',
|
2018-07-02 16:48:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
people.safe_full_names = () => '';
|
|
|
|
|
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
|
|
|
{
|
2019-01-28 19:13:56 +01:00
|
|
|
option: 'release now',
|
2018-07-02 16:48:23 +02:00
|
|
|
names: '',
|
|
|
|
count: 0,
|
|
|
|
key: '99,1',
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: false,
|
2018-07-02 16:48:23 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
question: 'best plan?',
|
|
|
|
});
|
|
|
|
|
2018-07-05 13:33:58 +02:00
|
|
|
let vote_event = {
|
2018-07-02 16:48:23 +02:00
|
|
|
type: 'vote',
|
|
|
|
key: '99,1',
|
|
|
|
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
|
|
|
{
|
2019-01-28 19:13:56 +01:00
|
|
|
option: 'release now',
|
2018-07-02 16:48:23 +02:00
|
|
|
names: '',
|
|
|
|
count: 1,
|
|
|
|
key: '99,1',
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: true,
|
2018-07-02 16:48:23 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
question: 'best plan?',
|
|
|
|
});
|
|
|
|
|
2018-07-05 13:33:58 +02:00
|
|
|
const invalid_vote_event = {
|
|
|
|
type: 'vote',
|
|
|
|
key: '98,1',
|
|
|
|
vote: 1,
|
|
|
|
};
|
|
|
|
|
2019-01-28 21:50:35 +01:00
|
|
|
blueslip.set_test_data('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();
|
2019-01-28 21:50:35 +01:00
|
|
|
assert.equal(blueslip.get_test_logs('warn').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
const option_outbound_event = data_holder.handle.new_option.outbound('new option');
|
|
|
|
assert.deepEqual(option_outbound_event, {
|
|
|
|
type: 'new_option',
|
2018-07-05 13:33:58 +02:00
|
|
|
idx: 2,
|
2019-01-28 19:13:56 +01:00
|
|
|
option: 'new option',
|
2018-07-05 13:33:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const new_question = 'Any new plan?';
|
|
|
|
const question_outbound_event = data_holder.handle.question.outbound(new_question);
|
|
|
|
assert.deepEqual(question_outbound_event, {
|
|
|
|
type: 'question',
|
|
|
|
question: new_question,
|
|
|
|
});
|
|
|
|
|
|
|
|
const vote_outbound_event = data_holder.handle.vote.outbound('99,1');
|
|
|
|
assert.deepEqual(vote_outbound_event, { type: 'vote', key: '99,1', vote: -1 });
|
|
|
|
|
|
|
|
vote_event = {
|
|
|
|
type: 'vote',
|
|
|
|
key: '99,1',
|
|
|
|
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
|
|
|
{
|
2019-01-28 19:13:56 +01:00
|
|
|
option: 'release now',
|
2018-07-05 13:33:58 +02:00
|
|
|
names: '',
|
|
|
|
count: 0,
|
|
|
|
key: '99,1',
|
2019-02-27 07:20:12 +01:00
|
|
|
current_user_vote: false,
|
2018-07-05 13:33:58 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
question: 'best plan?',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('activate another person poll', () => {
|
|
|
|
people.is_my_user_id = return_false;
|
2019-07-11 05:06:20 +02:00
|
|
|
global.stub_templates((template_name) => {
|
2019-07-12 00:52:56 +02:00
|
|
|
if (template_name === 'widgets/poll_widget') {
|
|
|
|
return 'widgets/poll_widget';
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2019-07-12 00:52:56 +02:00
|
|
|
if (template_name === 'widgets/poll_widget_results') {
|
|
|
|
return 'widgets/poll_widget_results';
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
const widget_elem = $('<div>').addClass('widget-content');
|
|
|
|
|
|
|
|
let out_data; // Used to check the event data sent to the server
|
|
|
|
const callback = (data) => {
|
|
|
|
out_data = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
elem: widget_elem,
|
|
|
|
callback: callback,
|
|
|
|
message: {
|
|
|
|
sender_id: 100,
|
|
|
|
},
|
2019-01-09 17:55:42 +01:00
|
|
|
extra_data: {
|
|
|
|
question: 'What do you want?',
|
|
|
|
},
|
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;
|
|
|
|
};
|
|
|
|
|
2019-01-28 19:13:56 +01: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
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
const poll_question_submit = set_widget_find_result('button.poll-question-check');
|
|
|
|
const poll_edit_question = set_widget_find_result('.poll-edit-question');
|
2018-07-05 13:33:58 +02:00
|
|
|
const poll_question_header = set_widget_find_result('.poll-question-header');
|
|
|
|
const poll_question_container = set_widget_find_result('.poll-question-bar');
|
2019-01-28 19:13:56 +01:00
|
|
|
const poll_option_container = set_widget_find_result('.poll-option-bar');
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
const poll_vote_button = set_widget_find_result('button.poll-vote');
|
2019-01-09 17:55:42 +01:00
|
|
|
const poll_please_wait = set_widget_find_result('.poll-please-wait');
|
|
|
|
const poll_author_help = set_widget_find_result('.poll-author-help');
|
|
|
|
|
|
|
|
set_widget_find_result('button.poll-question-remove');
|
|
|
|
set_widget_find_result('input.poll-question');
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
let option_button_callback;
|
2018-07-05 13:33:58 +02:00
|
|
|
let vote_button_callback;
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option.on = (event, func) => {
|
2018-07-05 13:33:58 +02:00
|
|
|
assert.equal(event, 'click');
|
2019-01-28 19:13:56 +01:00
|
|
|
option_button_callback = func;
|
2018-07-05 13:33:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
poll_vote_button.on = (event, func) => {
|
|
|
|
assert.equal(event, 'click');
|
|
|
|
vote_button_callback = func;
|
|
|
|
};
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
poll_question_header.toggle = (show) => {
|
|
|
|
assert(show);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_edit_question.toggle = (show) => {
|
|
|
|
assert(!show);
|
|
|
|
};
|
|
|
|
|
|
|
|
var show_submit = false;
|
|
|
|
poll_question_submit.toggle = (show) => {
|
|
|
|
assert.equal(show, show_submit);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_question_container.toggle = (show) => {
|
|
|
|
assert(!show);
|
|
|
|
};
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option_container.toggle = (show) => {
|
2019-01-09 17:55:42 +01:00
|
|
|
assert.equal(show, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_please_wait.toggle = (show) => {
|
|
|
|
assert.equal(show, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_author_help.toggle = (show) => {
|
|
|
|
assert(!show);
|
|
|
|
};
|
|
|
|
|
2018-12-17 04:46:27 +01:00
|
|
|
poll_widget.activate(opts);
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2019-07-12 00:52:56 +02:00
|
|
|
assert.equal(widget_elem.html(), 'widgets/poll_widget');
|
|
|
|
assert.equal(widget_option_container.html(), 'widgets/poll_widget_results');
|
2019-01-09 17:55:42 +01:00
|
|
|
assert.equal(poll_question_header.text(), 'What do you want?');
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
const e = {
|
|
|
|
stopPropagation: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2019-01-28 19:13:56 +01:00
|
|
|
/* Testing data sent to server on adding option */
|
|
|
|
poll_option_input.val('cool choice');
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2019-01-28 19:13:56 +01:00
|
|
|
option_button_callback(e);
|
|
|
|
assert.deepEqual(out_data, { type: 'new_option', idx: 1, option: 'cool choice' });
|
2018-07-05 13:33:58 +02:00
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option_input.val('');
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
2019-01-28 19:13:56 +01:00
|
|
|
option_button_callback(e);
|
2018-07-05 13:33:58 +02:00
|
|
|
assert.deepEqual(out_data, undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
const vote_events = [
|
|
|
|
{
|
|
|
|
sender_id: 100,
|
|
|
|
data: {
|
2019-01-28 19:13:56 +01:00
|
|
|
type: 'new_option',
|
2018-07-05 13:33:58 +02:00
|
|
|
idx: 1,
|
2019-01-28 19:13:56 +01:00
|
|
|
option: 'release now',
|
2018-07-05 13:33:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sender_id: 100,
|
|
|
|
data: {
|
|
|
|
type: 'vote',
|
|
|
|
key: '100,1',
|
|
|
|
vote: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
widget_elem.handle_events(vote_events);
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Testing data sent to server on voting */
|
|
|
|
poll_vote_button.attr('data-key', '100,1');
|
|
|
|
const e = {
|
|
|
|
stopPropagation: noop,
|
|
|
|
target: poll_vote_button,
|
|
|
|
};
|
|
|
|
out_data = undefined;
|
|
|
|
vote_button_callback(e);
|
|
|
|
assert.deepEqual(out_data, { type: 'vote', key: '100,1', vote: 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
const add_question_event = [
|
|
|
|
{
|
|
|
|
sender_id: 100,
|
|
|
|
data: {
|
|
|
|
type: 'question',
|
|
|
|
question: 'best plan?',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
widget_elem.handle_events(add_question_event);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('activate own poll', () => {
|
|
|
|
$.clear_all_elements();
|
|
|
|
|
|
|
|
people.is_my_user_id = return_true;
|
2019-07-11 05:06:20 +02:00
|
|
|
global.stub_templates((template_name) => {
|
2019-07-12 00:52:56 +02:00
|
|
|
if (template_name === 'widgets/poll_widget') {
|
|
|
|
return 'widgets/poll_widget';
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2019-07-12 00:52:56 +02:00
|
|
|
if (template_name === 'widgets/poll_widget_results') {
|
|
|
|
return 'widgets/poll_widget_results';
|
2018-07-05 13:33:58 +02:00
|
|
|
}
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
const widget_elem = $('<div>').addClass('widget-content');
|
|
|
|
let out_data;
|
|
|
|
const callback = (data) => {
|
|
|
|
out_data = data;
|
|
|
|
};
|
|
|
|
const opts = {
|
|
|
|
elem: widget_elem,
|
|
|
|
callback: callback,
|
|
|
|
message: {
|
|
|
|
sender_id: 100,
|
|
|
|
},
|
|
|
|
extra_data: {
|
|
|
|
question: 'Where to go?',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const set_widget_find_result = (selector) => {
|
|
|
|
const elem = $.create(selector);
|
|
|
|
widget_elem.set_find_results(selector, elem);
|
|
|
|
return elem;
|
|
|
|
};
|
|
|
|
|
2019-01-28 19:13:56 +01: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
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
const poll_question_submit = set_widget_find_result('button.poll-question-check');
|
|
|
|
const poll_edit_question = set_widget_find_result('.poll-edit-question');
|
2018-07-05 13:33:58 +02:00
|
|
|
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');
|
2019-01-28 19:13:56 +01:00
|
|
|
const poll_option_container = set_widget_find_result('.poll-option-bar');
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
const poll_vote_button = set_widget_find_result('button.poll-vote');
|
2019-01-09 17:55:42 +01:00
|
|
|
const poll_please_wait = set_widget_find_result('.poll-please-wait');
|
|
|
|
const poll_author_help = set_widget_find_result('.poll-author-help');
|
|
|
|
|
|
|
|
set_widget_find_result('button.poll-question-remove');
|
2018-07-05 13:33:58 +02:00
|
|
|
|
|
|
|
let question_button_callback;
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
poll_question_submit.on = (event, func) => {
|
2018-07-05 13:33:58 +02:00
|
|
|
assert.equal(event, 'click');
|
|
|
|
question_button_callback = func;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Following event handler are already tested and doesn't make sense
|
|
|
|
// to test them again
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option.on = noop;
|
2018-07-05 13:33:58 +02:00
|
|
|
poll_vote_button.on = noop;
|
|
|
|
|
2019-01-09 17:55:42 +01:00
|
|
|
poll_question_header.toggle = (show) => {
|
|
|
|
assert(show);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_edit_question.toggle = (show) => {
|
|
|
|
assert(show);
|
|
|
|
};
|
|
|
|
|
|
|
|
var show_submit = false;
|
|
|
|
poll_question_submit.toggle = (show) => {
|
|
|
|
assert.equal(show, show_submit);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_question_container.toggle = (show) => {
|
|
|
|
assert(!show);
|
|
|
|
};
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option_container.toggle = (show) => {
|
2019-01-09 17:55:42 +01:00
|
|
|
assert(show);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_please_wait.toggle = (show) => {
|
|
|
|
assert(!show);
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_author_help.toggle = (show) => {
|
|
|
|
assert(!show);
|
|
|
|
};
|
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
|
|
|
|
2019-07-12 00:52:56 +02:00
|
|
|
assert.equal(widget_elem.html(), 'widgets/poll_widget');
|
|
|
|
assert.equal(widget_option_container.html(), 'widgets/poll_widget_results');
|
2018-07-05 13:33:58 +02:00
|
|
|
assert.equal(poll_question_header.text(), 'Where to go?');
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Testing data sent to server on editing question */
|
|
|
|
const e = {
|
|
|
|
stopPropagation: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
poll_question_input.val('Is it new?');
|
|
|
|
out_data = undefined;
|
2019-01-09 17:55:42 +01:00
|
|
|
show_submit = true;
|
2018-07-05 13:33:58 +02:00
|
|
|
question_button_callback(e);
|
|
|
|
assert.deepEqual(out_data, { type: 'question', question: 'Is it new?' });
|
|
|
|
|
2019-01-28 19:13:56 +01:00
|
|
|
poll_option_input.val('');
|
2018-07-05 13:33:58 +02:00
|
|
|
out_data = undefined;
|
|
|
|
question_button_callback(e);
|
|
|
|
assert.deepEqual(out_data, undefined);
|
|
|
|
}
|
2018-07-02 16:48:23 +02:00
|
|
|
});
|