Files
nairobi/spec/components/admin/poll/results/question_component_spec.rb
taitus a29eeaf2e2 Add option_id to partial results and unique index
Similar to what we did in PR "Avoid duplicate records in poll answers" 5539,
specifically in commit 503369166, we want to stop relying on the plain text
"answer" and start using "option_id" to avoid issues with counts across
translations and to add consistency to the poll_partial_results table.

Note that we also moved the `possible_answers` method from Poll::Question to
Poll::Question::Option, since the list of valid answers really comes from the
options of a question and not from the question itself. Tests were updated
to validate answers against the translations of the assigned option.

Additionally, we renamed lambda parameters in validations to improve clarity.
2025-09-26 15:05:34 +02:00

69 lines
2.8 KiB
Ruby

require "rails_helper"
describe Admin::Poll::Results::QuestionComponent do
let(:poll) { create(:poll) }
let(:question) { create(:poll_question, poll: poll, title: "What do you want?") }
let!(:option_yes) { create(:poll_question_option, question: question, title: "Yes") }
let!(:option_no) { create(:poll_question_option, question: question, title: "No") }
it "renders question title and headers" do
render_inline Admin::Poll::Results::QuestionComponent.new(question, poll.partial_results)
expect(page).to have_css "h3", text: "What do you want?"
expect(page).to have_css "th", text: "Answer"
expect(page).to have_css "th", text: "Votes"
end
it "renders one row per option" do
render_inline Admin::Poll::Results::QuestionComponent.new(question, poll.partial_results)
expect(page).to have_css "tbody tr", count: 2
expect(page).to have_css "td", text: "Yes"
expect(page).to have_css "td", text: "No"
end
it "sums votes by option" do
create(:poll_partial_result, question: question, option: option_yes, amount: 2, date: Date.current)
create(:poll_partial_result, question: question, option: option_yes, amount: 1, date: Date.yesterday)
create(:poll_partial_result, question: question, option: option_no, amount: 5)
render_inline Admin::Poll::Results::QuestionComponent.new(question, question.partial_results)
page.find("tr#question_#{question.id}_0_result") do |yes_result|
expect(yes_result).to have_css "td", text: "Yes"
expect(yes_result).to have_css "td", text: "3"
end
page.find("tr#question_#{question.id}_1_result") do |no_result|
expect(no_result).to have_css "td", text: "No"
expect(no_result).to have_css "td", text: "5"
end
end
it "shows 0 when an option has no partial results" do
render_inline Admin::Poll::Results::QuestionComponent.new(question, poll.partial_results)
page.find("tr#question_#{question.id}_0_result") do |yes_result|
expect(yes_result).to have_css "td", text: "Yes"
expect(yes_result).to have_css "td", text: "0"
end
page.find("tr#question_#{question.id}_1_result") do |no_result|
expect(no_result).to have_css "td", text: "No"
expect(no_result).to have_css "td", text: "0"
end
end
it "ignores partial results from other questions" do
other_question = create(:poll_question, poll: poll)
create(:poll_question_option, question: other_question, title: "Yes")
create(:poll_question_option, question: other_question, title: "Irrelevant")
create(:poll_partial_result, question: other_question, answer: "Yes", amount: 9)
create(:poll_partial_result, question: other_question, answer: "Irrelevant", amount: 9)
render_inline Admin::Poll::Results::QuestionComponent.new(question, poll.partial_results)
expect(page).to have_css "td", text: "0", count: 2
end
end