Having a class named `Poll::Question::Answer` and another class named `Poll::Answer` was so confusing that no developer working on the project has ever been capable of remembering which is which for more than a few seconds. Furthermore, we're planning to add open answers to polls, and we might add a reference from the `poll_answers` table to the `poll_question_answers` table to property differentiate between open answers and closed answers. Having yet another thing named answer would be more than what our brains can handle (we know it because we did this once in a prototype). So we're renaming `Poll::Question::Answer` to `Poll::Question::Option`. Hopefully that'll make it easier to remember. The name is also (more or less) consistent with the `Legislation::QuestionOption` class, which is similar. We aren't changing the table or columns names for now in order to avoid possible issues when upgrading (old code running with the new database tables/columns after running the migrations but before deployment has finished, for instance). We might do it in the future. I've tried not to change the internationalization keys either so existing translations would still be valid. However, since we have to change the keys in `activerecord.yml` so methods like `human_attribute_name` keep working, I'm also changing them in places where similar keys were used (like `poll_question_answer` or `poll/question/answer`). Note that it isn't clear whether we should use `option` or `question_option` in some cases. In order to keep things simple, we're using `option` where we were using `answer` and `question_option` where we were using `question_answer`. Also note we're adding tests for the admin menu component, since at first I forgot to change the `answers` reference there and all tests passed.
113 lines
3.9 KiB
Ruby
113 lines
3.9 KiB
Ruby
require "rails_helper"
|
|
|
|
describe Polls::Questions::OptionsComponent do
|
|
include Rails.application.routes.url_helpers
|
|
let(:poll) { create(:poll) }
|
|
let(:question) { create(:poll_question, :yes_no, poll: poll) }
|
|
|
|
it "renders answers in given order" do
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect("Yes").to appear_before("No")
|
|
end
|
|
|
|
it "renders buttons to vote question answers" do
|
|
sign_in(create(:user, :verified))
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_button "Yes"
|
|
expect(page).to have_button "No"
|
|
expect(page).to have_css "button[aria-pressed='false']", count: 2
|
|
end
|
|
|
|
it "renders button to destroy current user answers" do
|
|
user = create(:user, :verified)
|
|
create(:poll_answer, author: user, question: question, answer: "Yes")
|
|
sign_in(user)
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_button "You have voted Yes"
|
|
expect(page).to have_button "Vote No"
|
|
expect(page).to have_css "button[aria-pressed='true']", text: "Yes"
|
|
end
|
|
|
|
it "renders disabled buttons when max votes is reached" do
|
|
user = create(:user, :verified)
|
|
question = create(:poll_question_multiple, :abc, max_votes: 2, author: user)
|
|
create(:poll_answer, author: user, question: question, answer: "Answer A")
|
|
create(:poll_answer, author: user, question: question, answer: "Answer C")
|
|
sign_in(user)
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_button "You have voted Answer A"
|
|
expect(page).to have_button "Vote Answer B", disabled: true
|
|
expect(page).to have_button "You have voted Answer C"
|
|
end
|
|
|
|
it "when user is not signed in, renders answers links pointing to user sign in path" do
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_link "Yes", href: new_user_session_path
|
|
expect(page).to have_link "No", href: new_user_session_path
|
|
end
|
|
|
|
it "when user is not verified, renders answers links pointing to user verification in path" do
|
|
sign_in(create(:user))
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_link "Yes", href: verification_path
|
|
expect(page).to have_link "No", href: verification_path
|
|
end
|
|
|
|
it "when user already voted in booth it renders disabled answers" do
|
|
user = create(:user, :level_two)
|
|
create(:poll_voter, :from_booth, poll: poll, user: user)
|
|
sign_in(user)
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_css "span.disabled", text: "Yes"
|
|
expect(page).to have_css "span.disabled", text: "No"
|
|
end
|
|
|
|
it "user cannot vote when poll expired it renders disabled answers" do
|
|
question = create(:poll_question, :yes_no, poll: create(:poll, :expired))
|
|
sign_in(create(:user, :level_two))
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_css "span.disabled", text: "Yes"
|
|
expect(page).to have_css "span.disabled", text: "No"
|
|
end
|
|
|
|
describe "geozone" do
|
|
let(:poll) { create(:poll, geozone_restricted: true) }
|
|
let(:geozone) { create(:geozone) }
|
|
let(:question) { create(:poll_question, :yes_no, poll: poll) }
|
|
|
|
it "when geozone which is not theirs it renders disabled answers" do
|
|
poll.geozones << geozone
|
|
sign_in(create(:user, :level_two))
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_css "span.disabled", text: "Yes"
|
|
expect(page).to have_css "span.disabled", text: "No"
|
|
end
|
|
|
|
it "reading a same-geozone poll it renders buttons to vote question answers" do
|
|
poll.geozones << geozone
|
|
sign_in(create(:user, :level_two, geozone: geozone))
|
|
|
|
render_inline Polls::Questions::OptionsComponent.new(question)
|
|
|
|
expect(page).to have_button "Yes"
|
|
expect(page).to have_button "No"
|
|
end
|
|
end
|
|
end
|