Files
nairobi/spec/components/polls/form_component_spec.rb
Javi Martín a7e1b42b6c Use checkboxes and radio buttons on poll forms
Our original interface to vote in a poll had a few issues:

* Since there was no button to send the form, it wasn't clear that
  selecting an option would automatically store it in the database.
* The interface was almost identical for single-choice questions and
  multiple-choice questions, which made it hard to know which type of
  question we were answering.
* Adding other type of questions, like open answers, was hard since we
  would have to add a different submit button for each answer.

So we're now using radio buttons for single-choice questions and
checkboxes for multiple-choice questions, which are the native controls
designed for these purposes, and a button to send the whole form.

Since we don't have a database table for poll ballots like we have for
budget ballots, we're adding a new `Poll::WebVote` model to manage poll
ballots. We're using WebVote instead of Ballot or Vote because they
could be mistaken with other vote classes.

Note that browsers don't allow removing answers with radio buttons, so
once somebody has voted in a single-choice question, they can't remove
the vote unless they manually edit their HTML. This is the same behavior
we had before commit 7df0e9a96.

As mentioned in c2010f975, we're now adding the `ChangeByZero` rubocop
rule, since we've removed the test that used `and change`.
2025-08-14 13:06:37 +02:00

71 lines
2.0 KiB
Ruby

require "rails_helper"
describe Polls::FormComponent do
let(:user) { create(:user, :level_two) }
let(:poll) { create(:poll) }
let(:web_vote) { Poll::WebVote.new(poll, user) }
before { create(:poll_question, :yes_no, poll: poll) }
it "renders disabled fields when the user has already voted in a booth" do
create(:poll_voter, :from_booth, poll: poll, user: user)
sign_in(user)
render_inline Polls::FormComponent.new(web_vote)
page.find("fieldset[disabled]") do |fieldset|
expect(fieldset).to have_field "Yes"
expect(fieldset).to have_field "No"
end
expect(page).to have_button "Vote", disabled: true
end
context "expired poll" do
let(:poll) { create(:poll, :expired) }
it "renders disabled fields when the poll has expired" do
sign_in(user)
render_inline Polls::FormComponent.new(web_vote)
page.find("fieldset[disabled]") do |fieldset|
expect(fieldset).to have_field "Yes"
expect(fieldset).to have_field "No"
end
expect(page).to have_button "Vote", disabled: true
end
end
context "geozone restricted poll" do
let(:poll) { create(:poll, geozone_restricted: true) }
let(:geozone) { create(:geozone) }
it "renders disabled fields for users from another geozone" do
poll.geozones << geozone
sign_in(user)
render_inline Polls::FormComponent.new(web_vote)
page.find("fieldset[disabled]") do |fieldset|
expect(fieldset).to have_field "Yes"
expect(fieldset).to have_field "No"
end
expect(page).to have_button "Vote", disabled: true
end
it "renders enabled fields for same-geozone users" do
poll.geozones << geozone
sign_in(create(:user, :level_two, geozone: geozone))
render_inline Polls::FormComponent.new(web_vote)
expect(page).not_to have_css "fieldset[disabled]"
expect(page).to have_field "Yes"
expect(page).to have_field "No"
expect(page).to have_button "Vote"
end
end
end