From 331f6e8803be23c7824dd547f4b91b66890477c7 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 2 Jan 2022 16:00:41 -0800 Subject: [PATCH] fix: fix multiple-choice poll results (#2101) Fixes #2100 --- src/routes/_components/status/StatusPoll.html | 12 +++---- tests/spec/126-polls.js | 35 +++++++++++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/routes/_components/status/StatusPoll.html b/src/routes/_components/status/StatusPoll.html index df5fb9f3..37708322 100644 --- a/src/routes/_components/status/StatusPoll.html +++ b/src/routes/_components/status/StatusPoll.html @@ -292,13 +292,13 @@ computed: { pollId: ({ originalStatus }) => originalStatus.poll.id, poll: ({ originalStatus, $polls, pollId }) => $polls[pollId] || originalStatus.poll, - options: ({ poll, originalStatusEmojis, $autoplayGifs }) => ( - poll.options.map(({ title, votes_count: votesCount }) => ({ + options: ({ poll, originalStatusEmojis, $autoplayGifs, votersCount }) => ( + poll.options.map(({ title, votes_count: optionsVotesCount }) => ({ title: emojifyText(escapeHtml(title), originalStatusEmojis, $autoplayGifs), - share: poll.votes_count ? Math.round(votesCount / poll.votes_count * 100) : 0 + share: votersCount ? Math.round(optionsVotesCount / votersCount * 100) : 0 })) ), - votesCount: ({ poll }) => poll.votes_count, + votersCount: ({ poll }) => poll.voters_count, voted: ({ poll }) => poll.voted, multiple: ({ poll }) => poll.multiple, expired: ({ poll }) => poll.expired, @@ -314,8 +314,8 @@ !isStatusInOwnThread && $isMobileSize && !expired ), formDisabled: ({ choices }) => !choices.length, - votesText: ({ votesCount }) => ( - formatIntl('intl.voteCount', { count: votesCount }) + votesText: ({ votersCount }) => ( + formatIntl('intl.voteCount', { count: votersCount }) ), computedClass: ({ isStatusInNotification, isStatusInOwnThread, loading, shown }) => ( classname( diff --git a/tests/spec/126-polls.js b/tests/spec/126-polls.js index 785ef767..8f99dfdd 100644 --- a/tests/spec/126-polls.js +++ b/tests/spec/126-polls.js @@ -50,10 +50,10 @@ test('Can vote on multiple-choice polls', async t => { await t .click(getNthStatusPollVoteButton(1)) .expect(getNthStatusPollForm(1).exists).notOk({ timeout: 20000 }) - .expect(getNthStatusPollResult(1, 1).innerText).eql('50% yes') + .expect(getNthStatusPollResult(1, 1).innerText).eql('100% yes') .expect(getNthStatusPollResult(1, 2).innerText).eql('0% no') - .expect(getNthStatusPollResult(1, 3).innerText).eql('50% maybe') - .expect(getNthStatusPollVoteCount(1).innerText).eql('2 votes') + .expect(getNthStatusPollResult(1, 3).innerText).eql('100% maybe') + .expect(getNthStatusPollVoteCount(1).innerText).eql('1 vote') }) test('Can update poll results', async t => { @@ -121,3 +121,32 @@ test('Polls with content warnings', async t => { .expect(getNthStatusPollForm(1).visible).notOk() .expect(getNthStatusContent(1).visible).notOk() }) + +test('Percent total can exceed 100% on multi-choice polls', async t => { + const { poll } = await createPollAs('admin', 'please vote a whole lot', ['yes', 'no', 'maybe', 'huh'], true) + const { id: pollId } = poll + await voteOnPollAs('baz', pollId, [0, 1, 2]) + await voteOnPollAs('ExternalLinks', pollId, [0, 1, 2, 3]) + await sleep(2000) + await loginAsFoobar(t) + await t + .expect(getNthStatusContent(1).innerText).contains('please vote a whole lot') + await sleep(1000) + await t + .click(getNthStatusPollOption(1, 1)) + await sleep(1000) + await t + .click(getNthStatusPollOption(1, 2)) + await sleep(1000) + await t + .click(getNthStatusPollOption(1, 3)) + await sleep(1000) + await t + .click(getNthStatusPollVoteButton(1)) + .expect(getNthStatusPollForm(1).exists).notOk({ timeout: 20000 }) + .expect(getNthStatusPollResult(1, 1).innerText).eql('100% yes') + .expect(getNthStatusPollResult(1, 2).innerText).eql('100% no') + .expect(getNthStatusPollResult(1, 3).innerText).eql('100% maybe') + .expect(getNthStatusPollResult(1, 4).innerText).eql('33% huh') + .expect(getNthStatusPollVoteCount(1).innerText).eql('3 votes') +})