Files
grecia/spec/system/admin/poll/questions/options/options_spec.rb
Javi Martín 38b38d1fcc Rename Poll::Question::Answer to Poll::Question::Option
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.
2024-06-13 19:13:01 +02:00

105 lines
3.3 KiB
Ruby

require "rails_helper"
describe "Poll question options", :admin do
let(:future_poll) { create(:poll, :future) }
let(:current_poll) { create(:poll) }
describe "Create" do
scenario "Is possible for a not started poll" do
question = create(:poll_question, poll: future_poll)
visit admin_question_path(question)
click_link "Add answer"
expect(page).to have_link "Go back", href: admin_question_path(question)
fill_in "Answer", with: "The answer is always 42"
fill_in_ckeditor "Description", with: "The Hitchhiker's Guide To The Universe"
click_button "Save"
expect(page).to have_content "Answer created successfully"
expect(page).to have_content "The answer is always 42"
expect(page).to have_content "The Hitchhiker's Guide To The Universe"
end
scenario "Is not possible for an already started poll" do
question = create(:poll_question, poll: current_poll)
visit admin_question_path(question)
expect(page).not_to have_link "Add answer"
expect(page).to have_content "Once the poll has started it will not be possible to create, edit or"
end
scenario "Create second answer and place after the first one" do
question = create(:poll_question, poll: future_poll)
create(:poll_question_option, title: "First", question: question, given_order: 1)
visit admin_question_path(question)
click_link "Add answer"
fill_in "Answer", with: "Second"
fill_in_ckeditor "Description", with: "Description"
click_button "Save"
expect("First").to appear_before("Second")
end
end
scenario "Update" do
question = create(:poll_question, poll: future_poll)
create(:poll_question_option, question: question, title: "Answer title", given_order: 2)
create(:poll_question_option, question: question, title: "Another title", given_order: 1)
visit admin_question_path(question)
within("tr", text: "Answer title") { click_link "Edit" }
expect(page).to have_link "Go back", href: admin_question_path(question)
fill_in "Answer", with: "New title"
click_button "Save"
expect(page).to have_content "Changes saved"
expect(page).to have_content "New title"
visit admin_question_path(question)
expect(page).not_to have_content "Answer title"
expect("Another title").to appear_before("New title")
end
scenario "Destroy" do
option = create(:poll_question_option, poll: future_poll, title: "I'm not useful")
visit admin_question_path(option.question)
within("tr", text: "I'm not useful") do
accept_confirm("Are you sure? This action will delete \"I'm not useful\" and can't be undone.") do
click_button "Delete"
end
end
expect(page).to have_content "Answer deleted successfully"
expect(page).not_to have_content "I'm not useful"
end
scenario "Reorder" do
question = create(:poll_question)
create(:poll_question_option, question: question, title: "First", given_order: 1)
create(:poll_question_option, question: question, title: "Last", given_order: 2)
visit admin_question_path(question)
within("tbody.sortable") do
expect("First").to appear_before("Last")
find("tr", text: "Last").drag_to(find("tr", text: "First"))
expect("Last").to appear_before("First")
end
end
end