diff --git a/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 b/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 index 47f0d5ba5af..11f3f67a0af 100644 --- a/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 +++ b/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 @@ -7,7 +7,9 @@ export default Em.Component.extend({ @computed("poll.voters", "poll.type", "poll.options.[]") options(voters, type) { - const options = this.get("poll.options"); + const options = this.get("poll.options").slice(0).sort((a, b) => { + return a.get("votes") < b.get("votes") ? 1 : 0; + }); let percentages = voters === 0 ? Array(options.length).fill(0) : @@ -35,5 +37,4 @@ export default Em.Component.extend({ return options; } - }); diff --git a/plugins/poll/assets/javascripts/components/poll-voters.js.es6 b/plugins/poll/assets/javascripts/components/poll-voters.js.es6 index ac43bc53ca8..7dcca87b861 100644 --- a/plugins/poll/assets/javascripts/components/poll-voters.js.es6 +++ b/plugins/poll/assets/javascripts/components/poll-voters.js.es6 @@ -40,10 +40,8 @@ export default Ember.Component.extend({ didInsertElement() { this._super(); - Ember.run.schedule("afterRender", () => { - this.set("numOfVotersToShow", Math.round(this.$().width() / 25) * 2); - if (this.get("voterIds").length > 0) this._fetchUsers(); - }); + this.set("numOfVotersToShow", Math.round(this.$().width() / 25) * 2); + if (this.get("voterIds").length > 0) this._fetchUsers(); }, actions: { diff --git a/plugins/poll/test/javascripts/components/poll-results-standard-test.js.es6 b/plugins/poll/test/javascripts/components/poll-results-standard-test.js.es6 new file mode 100644 index 00000000000..ac0c229099f --- /dev/null +++ b/plugins/poll/test/javascripts/components/poll-results-standard-test.js.es6 @@ -0,0 +1,51 @@ +import componentTest from 'helpers/component-test'; +moduleForComponent('poll-results-standard', { integration: true }); + +componentTest('options in descending order', { + template: '{{poll-results-standard poll=poll}}', + + setup(store) { + this.set('poll', { + options: [Em.Object.create({ votes: 5 }), Em.Object.create({ votes: 4 })], + voters: 9 + }); + }, + + test(assert) { + assert.equal(this.$('.option .percentage:eq(0)').text(), '56%'); + assert.equal(this.$('.option .percentage:eq(1)').text(), '44%'); + } +}); + +componentTest('options in ascending order', { + template: '{{poll-results-standard poll=poll sortResults=sortResults}}', + + setup() { + this.set('poll', { + options: [Em.Object.create({ votes: 4 }), Em.Object.create({ votes: 5 })], + voters: 9 + }); + }, + + test(assert) { + assert.equal(this.$('.option .percentage:eq(0)').text(), '56%'); + assert.equal(this.$('.option .percentage:eq(1)').text(), '44%'); + } +}); + +componentTest('multiple options in descending order', { + template: '{{poll-results-standard poll=poll}}', + + setup(store) { + this.set('poll', { + type: 'multiple', + options: [Em.Object.create({ votes: 5 }), Em.Object.create({ votes: 4 })], + voters: 9 + }); + }, + + test(assert) { + assert.equal(this.$('.option .percentage:eq(0)').text(), '55%'); + assert.equal(this.$('.option .percentage:eq(1)').text(), '44%'); + } +});