Show errors when submitting too many answers
This could be the case when JavaScript is disabled. Note that, in `Poll/WebVote` we're calling `given_answers` inside a transaction. Putting this code before the transaction resulted in a test failing sometimes, probably because of a bug that might be possible to reproduce by doing simultaneous requests.
This commit is contained in:
@@ -53,31 +53,36 @@ describe Polls::FormComponent do
|
||||
context "geozone restricted poll" do
|
||||
let(:poll) { create(:poll, geozone_restricted: true) }
|
||||
let(:geozone) { create(:geozone) }
|
||||
before { poll.geozones << geozone }
|
||||
|
||||
it "renders disabled fields for users from another geozone" do
|
||||
poll.geozones << geozone
|
||||
sign_in(user)
|
||||
context "user from another geozone" do
|
||||
let(:user) { create(:user, :level_two) }
|
||||
before { sign_in(user) }
|
||||
|
||||
render_inline Polls::FormComponent.new(web_vote)
|
||||
it "renders disabled fields" do
|
||||
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"
|
||||
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
|
||||
|
||||
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))
|
||||
context "user from the same geozone" do
|
||||
let(:user) { create(:user, :level_two, geozone: geozone) }
|
||||
before { sign_in(user) }
|
||||
|
||||
render_inline Polls::FormComponent.new(web_vote)
|
||||
it "renders enabled answers" do
|
||||
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"
|
||||
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
|
||||
end
|
||||
|
||||
@@ -5,11 +5,14 @@ describe Polls::Questions::QuestionComponent do
|
||||
let(:question) { create(:poll_question, :yes_no, poll: poll) }
|
||||
let(:option_yes) { question.question_options.find_by(title: "Yes") }
|
||||
let(:option_no) { question.question_options.find_by(title: "No") }
|
||||
let(:user) { User.new }
|
||||
let(:web_vote) { Poll::WebVote.new(poll, user) }
|
||||
let(:form) { ConsulFormBuilder.new(:web_vote, web_vote, ApplicationController.new.view_context, {}) }
|
||||
|
||||
it "renders more information links when any question option has additional information" do
|
||||
allow_any_instance_of(Poll::Question::Option).to receive(:with_read_more?).and_return(true)
|
||||
|
||||
render_inline Polls::Questions::QuestionComponent.new(question)
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, form: form)
|
||||
|
||||
page.find("#poll_question_#{question.id}") do |poll_question|
|
||||
expect(poll_question).to have_content "Read more about"
|
||||
@@ -20,13 +23,13 @@ describe Polls::Questions::QuestionComponent do
|
||||
end
|
||||
|
||||
it "renders answers in given order" do
|
||||
render_inline Polls::Questions::QuestionComponent.new(question)
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, form: form)
|
||||
|
||||
expect("Yes").to appear_before("No")
|
||||
end
|
||||
|
||||
it "renders disabled answers when given the disabled parameter" do
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, disabled: true)
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, form: form, disabled: true)
|
||||
|
||||
page.find("fieldset[disabled]") do |fieldset|
|
||||
expect(fieldset).to have_field "Yes"
|
||||
@@ -39,7 +42,7 @@ describe Polls::Questions::QuestionComponent do
|
||||
before { sign_in(user) }
|
||||
|
||||
it "renders radio buttons for single-choice questions" do
|
||||
render_inline Polls::Questions::QuestionComponent.new(question)
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, form: form)
|
||||
|
||||
expect(page).to have_field "Yes", type: :radio
|
||||
expect(page).to have_field "No", type: :radio
|
||||
@@ -47,7 +50,9 @@ describe Polls::Questions::QuestionComponent do
|
||||
end
|
||||
|
||||
it "renders checkboxes for multiple-choice questions" do
|
||||
render_inline Polls::Questions::QuestionComponent.new(create(:poll_question_multiple, :abc))
|
||||
question = create(:poll_question_multiple, :abc, poll: poll)
|
||||
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, form: form)
|
||||
|
||||
expect(page).to have_field "Answer A", type: :checkbox
|
||||
expect(page).to have_field "Answer B", type: :checkbox
|
||||
@@ -59,7 +64,7 @@ describe Polls::Questions::QuestionComponent do
|
||||
it "selects the option when users have already voted" do
|
||||
create(:poll_answer, author: user, question: question, option: option_yes)
|
||||
|
||||
render_inline Polls::Questions::QuestionComponent.new(question)
|
||||
render_inline Polls::Questions::QuestionComponent.new(question, form: form)
|
||||
|
||||
expect(page).to have_field "Yes", type: :radio, checked: true
|
||||
expect(page).to have_field "No", type: :radio, checked: false
|
||||
|
||||
@@ -28,15 +28,15 @@ describe Poll::WebVote do
|
||||
end
|
||||
|
||||
it "updates a poll_voter with user and poll data" do
|
||||
create(:poll_answer, question: question, author: user, option: option_yes)
|
||||
answer = create(:poll_answer, question: question, author: user, option: option_yes)
|
||||
|
||||
web_vote.update(question.id.to_s => { option_id: option_no.id.to_s })
|
||||
|
||||
expect(poll.reload.voters.size).to eq 1
|
||||
expect(question.reload.answers.size).to eq 1
|
||||
expect(question.answers.first).to eq answer.reload
|
||||
|
||||
voter = poll.voters.first
|
||||
answer = question.answers.first
|
||||
|
||||
expect(answer.author).to eq user
|
||||
expect(answer.option).to eq option_no
|
||||
|
||||
@@ -71,4 +71,33 @@ describe "Poll Votation Type" do
|
||||
expect(page).to have_field "Answer B", type: :checkbox, checked: false
|
||||
expect(page).to have_field "Answer C", type: :checkbox, checked: true
|
||||
end
|
||||
|
||||
scenario "Too many answers", :no_js do
|
||||
create(:poll_question_multiple, :abcde, poll: poll, max_votes: 2, title: "Which ones are correct?")
|
||||
|
||||
visit poll_path(poll)
|
||||
check "Answer A"
|
||||
check "Answer B"
|
||||
check "Answer D"
|
||||
click_button "Vote"
|
||||
|
||||
within_fieldset("Which ones are correct?") do
|
||||
expect(page).to have_content "you've selected 3 answers, but the maximum you can select is 2"
|
||||
expect(page).to have_field "Answer A", type: :checkbox, checked: true
|
||||
expect(page).to have_field "Answer B", type: :checkbox, checked: true
|
||||
expect(page).to have_field "Answer C", type: :checkbox, checked: false
|
||||
expect(page).to have_field "Answer D", type: :checkbox, checked: true
|
||||
expect(page).to have_field "Answer E", type: :checkbox, checked: false
|
||||
end
|
||||
|
||||
expect(page).not_to have_content "Thank you for voting!"
|
||||
|
||||
visit poll_path(poll)
|
||||
|
||||
expect(page).not_to have_content "but the maximum you can select"
|
||||
|
||||
within_fieldset("Which ones are correct?") do
|
||||
expect(page).to have_field type: :checkbox, checked: false, count: 5
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user