diff --git a/frontend_tests/node_tests/voting_widget.js b/frontend_tests/node_tests/voting_widget.js new file mode 100644 index 0000000000..918dee5fc8 --- /dev/null +++ b/frontend_tests/node_tests/voting_widget.js @@ -0,0 +1,79 @@ +zrequire('voting_widget'); + +set_global('people', {}); + +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; + + const data_holder = voting_widget.poll_data_holder(is_my_poll, question); + + var data = data_holder.get_widget_data(); + + assert.deepEqual(data, { + comments: [], + 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, { + comments: [], + question: 'best plan?', + }); + + const comment_event = { + type: 'new_comment', + idx: 1, + comment: 'release now', + }; + + people.safe_full_names = () => ''; + + data_holder.handle_event(sender_id, comment_event); + data = data_holder.get_widget_data(); + + assert.deepEqual(data, { + comments: [ + { + comment: 'release now', + names: '', + count: 0, + key: '99,1', + }, + ], + question: 'best plan?', + }); + + const vote_event = { + type: 'vote', + key: '99,1', + vote: 1, + }; + + data_holder.handle_event(sender_id, vote_event); + data = data_holder.get_widget_data(); + data = data_holder.get_widget_data(); + + assert.deepEqual(data, { + comments: [ + { + comment: 'release now', + names: '', + count: 1, + key: '99,1', + }, + ], + question: 'best plan?', + }); + +}); diff --git a/static/js/voting_widget.js b/static/js/voting_widget.js index 7f1c466cbd..9403b6baac 100644 --- a/static/js/voting_widget.js +++ b/static/js/voting_widget.js @@ -2,7 +2,7 @@ var voting_widget = (function () { var exports = {}; -var poll_data_holder = function (is_my_poll, question) { +exports.poll_data_holder = function (is_my_poll, question) { // This object just holds data for a poll, although it // works closely with the widget's concept of how data // should be represented for rendering, plus how the @@ -147,7 +147,7 @@ exports.activate = function (opts) { } var is_my_poll = people.is_my_user_id(opts.message.sender_id); - var poll_data = poll_data_holder(is_my_poll, question); + var poll_data = exports.poll_data_holder(is_my_poll, question); function render() { var html = templates.render('poll-widget');