Move results specs to Polls::ResultsComponent

Running tests at the component level is faster than at the system level,
so we move tests from system/polls/results_spec.rb to the component.

Note that moving these tests removes vote_for_poll_via_web and the visit
to results_poll_path, but both are already covered in other tests. We
also take the opportunity to reuse the method in another test where
it makes sense.

Additionally, the spec title has been reverted from "Results for polls
with questions but without options" to "renders results for polls with
questions but without answers", as it was before commit 8997ed316c.
This commit is contained in:
taitus
2025-08-21 07:56:11 +02:00
parent 5a69ffc619
commit 2a2edd17d1
5 changed files with 53 additions and 69 deletions

View File

@@ -0,0 +1,49 @@
require "rails_helper"
describe Polls::ResultsComponent do
let(:poll) { create(:poll) }
let(:question_1) { create(:poll_question, poll: poll, title: "Do you like Consul Democracy?") }
let(:option_yes) { create(:poll_question_option, question: question_1, title: "Yes") }
let(:option_no) { create(:poll_question_option, question: question_1, title: "No") }
let(:question_2) { create(:poll_question, poll: poll, title: "What's your favorite color?") }
let(:option_blue) { create(:poll_question_option, question: question_2, title: "Blue") }
let(:option_green) { create(:poll_question_option, question: question_2, title: "Green") }
let(:option_yellow) { create(:poll_question_option, question: question_2, title: "Yellow") }
it "renders results content" do
create_list(:poll_answer, 2, question: question_1, option: option_yes)
create(:poll_answer, question: question_1, option: option_no)
create(:poll_answer, question: question_2, option: option_blue)
create(:poll_answer, question: question_2, option: option_green)
create(:poll_answer, question: question_2, option: option_yellow)
render_inline Polls::ResultsComponent.new(poll)
expect(page).to have_content "Do you like Consul Democracy?"
page.find("#question_#{question_1.id}_results_table") do |table|
expect(table).to have_css "#option_#{option_yes.id}_result", text: "2 (66.67%)", normalize_ws: true
expect(table).to have_css "#option_#{option_no.id}_result", text: "1 (33.33%)", normalize_ws: true
end
expect(page).to have_content "What's your favorite color?"
page.find("#question_#{question_2.id}_results_table") do |table|
expect(table).to have_css "#option_#{option_blue.id}_result", text: "1 (33.33%)", normalize_ws: true
expect(table).to have_css "#option_#{option_green.id}_result", text: "1 (33.33%)", normalize_ws: true
expect(table).to have_css "#option_#{option_yellow.id}_result", text: "1 (33.33%)", normalize_ws: true
end
end
it "renders results for polls with questions but without answers" do
poll = create(:poll, :expired, results_enabled: true)
question = create(:poll_question, poll: poll)
render_inline Polls::ResultsComponent.new(poll)
expect(page).to have_content question.title
end
end

View File

@@ -180,15 +180,11 @@ describe "Polls" do
scenario "Level 2 users answering" do
poll.update!(geozone_restricted_to: [geozone])
create(:poll_question, :yes_no, poll: poll, title: "Do you agree?")
question = create(:poll_question, :yes_no, poll: poll, title: "Do you agree?")
login_as(create(:user, :level_two, geozone: geozone))
visit poll_path(poll)
vote_for_poll_via_web(poll, question => "Yes")
within_fieldset("Do you agree?") { choose "Yes" }
click_button "Vote"
expect(page).to have_content "Thank you for voting!"
expect(page).to have_content "You have already participated in this poll. " \
"If you vote again it will be overwritten."

View File

@@ -1,58 +0,0 @@
require "rails_helper"
describe "Poll Results" do
scenario "List each Poll question" do
user1 = create(:user, :level_two)
user2 = create(:user, :level_two)
user3 = create(:user, :level_two)
poll = create(:poll, results_enabled: true)
question1 = create(:poll_question, poll: poll)
option1 = create(:poll_question_option, question: question1, title: "Yes")
option2 = create(:poll_question_option, question: question1, title: "No")
question2 = create(:poll_question, poll: poll)
option3 = create(:poll_question_option, question: question2, title: "Blue")
option4 = create(:poll_question_option, question: question2, title: "Green")
option5 = create(:poll_question_option, question: question2, title: "Yellow")
login_as user1
vote_for_poll_via_web(poll, question1 => "Yes", question2 => "Blue")
logout
login_as user2
vote_for_poll_via_web(poll, question1 => "Yes", question2 => "Green")
logout
login_as user3
vote_for_poll_via_web(poll, question1 => "No", question2 => "Yellow")
logout
travel_to(poll.ends_at + 1.day)
visit results_poll_path(poll)
expect(page).to have_content(question1.title)
expect(page).to have_content(question2.title)
within("#question_#{question1.id}_results_table") do
expect(find("#option_#{option1.id}_result")).to have_content("2 (66.67%)")
expect(find("#option_#{option2.id}_result")).to have_content("1 (33.33%)")
end
within("#question_#{question2.id}_results_table") do
expect(find("#option_#{option3.id}_result")).to have_content("1 (33.33%)")
expect(find("#option_#{option4.id}_result")).to have_content("1 (33.33%)")
expect(find("#option_#{option5.id}_result")).to have_content("1 (33.33%)")
end
end
scenario "Results for polls with questions but without options" do
poll = create(:poll, :expired, results_enabled: true)
question = create(:poll_question, poll: poll)
visit results_poll_path(poll)
expect(page).to have_content question.title
end
end

View File

@@ -18,12 +18,9 @@ describe "Voter" do
user = create(:user, :level_two)
login_as user
visit poll_path(poll)
within_fieldset("Is this question stupid?") { choose "Yes" }
click_button "Vote"
vote_for_poll_via_web(poll, question => "Yes")
expect(page).to have_content "Thank you for voting!"
expect(page).to have_content "You have already participated in this poll. " \
"If you vote again it will be overwritten."
end