Files
nairobi/spec/system/admin/poll/questions_spec.rb
Javi Martín 5311daadfe Use a button for non-GET table actions
Links acting like buttons have a few disadvantages.

First, screen readers will announce them as "links". Screen reader users
usually associate links with "things that get you somewhere" and buttons
with "things that perform an action". So when something like "Delete,
link" is announced, they'll probably think this is a link which will
take them to another page where they can delete a record.

Furthermore, the URL of the link for the "destroy" action might be the
same as the URL for the "show" action (only one is accessed with a
DELETE request and the other one with a GET request). That means screen
readers could announce the link like "Delete, visited link", which is
very confusing.

They also won't work when opening links in a new tab, since opening
links in a new tab always results in a GET request to the URL the link
points to.

Finally, submit buttons work without JavaScript enabled, so they'll work
even if the JavaScript in the page hasn't loaded (for whatever reason).

For all these reasons (and probably many more), using a button to send
forms is IMHO superior to using links.

There's one disadvantage, though. Using `button_to` we create a <form>
tag, which means we'll generate invalid HTML if the table is inside
another form. If we run into this issue, we need to use `button_tag`
with a `form` attribute and then generate a form somewhere else inside
the HTML (with `content_for`).

Note we're using `button_to` with a block so it generates a <button>
tag. Using it in a different way the text would result in an <input />
tag, and input elements can't have pseudocontent added via CSS.

The following code could be a starting point to use the `button_tag`
with a `form` attribute. One advantage of this approach is screen
readers wouldn't announce "leaving form" while navigating through these
buttons. However, it doesn't work in Internet Explorer.

```
ERB:

<% content_for(:hidden_content, form_tag(path, form_options) {}) %>
<%= button_tag text, button_options %>

Ruby:

def form_id
  path.gsub("/", "_")
end

def form_options
  { id: form_id, method: options[:method] }
end

def button_options
  html_options.except(:method).merge(form: form_id)
end

Layout:

<%= content_for :hidden_content %> # Right before the `</body>`
```
2021-09-20 20:27:37 +02:00

194 lines
5.9 KiB
Ruby

require "rails_helper"
describe "Admin poll questions", :admin do
scenario "Index" do
poll1 = create(:poll)
poll2 = create(:poll)
poll3 = create(:poll)
proposal = create(:proposal)
question1 = create(:poll_question, poll: poll1)
question2 = create(:poll_question, poll: poll2)
question3 = create(:poll_question, poll: poll3, proposal: proposal)
visit admin_poll_path(poll1)
expect(page).to have_content(poll1.name)
within("#poll_question_#{question1.id}") do
expect(page).to have_content(question1.title)
expect(page).to have_link "Edit answers"
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
visit admin_poll_path(poll2)
expect(page).to have_content(poll2.name)
within("#poll_question_#{question2.id}") do
expect(page).to have_content question2.title
expect(page).to have_link "Edit answers"
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
visit admin_poll_path(poll3)
expect(page).to have_content(poll3.name)
within("#poll_question_#{question3.id}") do
expect(page).to have_content question3.title
expect(page).to have_link "(See proposal)", href: proposal_path(question3.proposal)
expect(page).to have_link "Edit answers"
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
end
scenario "Show" do
geozone = create(:geozone)
poll = create(:poll, geozone_restricted: true, geozone_ids: [geozone.id])
question = create(:poll_question, poll: poll)
visit admin_poll_path(poll)
click_link "Edit answers"
expect(page).to have_content(question.title)
expect(page).to have_content(question.author.name)
end
scenario "Create" do
poll = create(:poll, name: "Movies")
title = "Star Wars: Episode IV - A New Hope"
visit admin_poll_path(poll)
click_link "Create question"
expect(page).to have_content("Create question to poll Movies")
expect(page).to have_selector("input[id='poll_question_poll_id'][value='#{poll.id}']",
visible: :hidden)
fill_in "Question", with: title
click_button "Save"
expect(page).to have_content(title)
end
scenario "Create from proposal" do
create(:poll, name: "Proposals")
proposal = create(:proposal)
visit admin_proposal_path(proposal)
expect(page).not_to have_content("This proposal has reached the required supports")
click_link "Add this proposal to a poll to be voted"
expect(page).to have_current_path(new_admin_question_path, ignore_query: true)
expect(page).to have_field("Question", with: proposal.title)
select "Proposals", from: "poll_question_poll_id"
click_button "Save"
expect(page).to have_content(proposal.title)
end
scenario "Create from successful proposal" do
create(:poll, name: "Proposals")
proposal = create(:proposal, :successful)
visit admin_proposal_path(proposal)
expect(page).to have_content("This proposal has reached the required supports")
click_link "Add this proposal to a poll to be voted"
expect(page).to have_current_path(new_admin_question_path, ignore_query: true)
expect(page).to have_field("Question", with: proposal.title)
select "Proposals", from: "poll_question_poll_id"
click_button "Save"
expect(page).to have_content(proposal.title)
visit admin_questions_path
expect(page).to have_content(proposal.title)
end
scenario "Update" do
poll = create(:poll)
question1 = create(:poll_question, poll: poll)
visit admin_poll_path(poll)
within("#poll_question_#{question1.id}") do
click_link "Edit"
end
old_title = question1.title
new_title = "Potatoes are great and everyone should have one"
fill_in "Question", with: new_title
click_button "Save"
expect(page).to have_content "Changes saved"
expect(page).to have_content new_title
expect(page).not_to have_content(old_title)
end
scenario "Destroy" do
poll = create(:poll)
question1 = create(:poll_question, poll: poll)
question2 = create(:poll_question, poll: poll)
visit admin_poll_path(poll)
within("#poll_question_#{question1.id}") do
accept_confirm { click_button "Delete" }
end
expect(page).not_to have_content(question1.title)
expect(page).to have_content(question2.title)
end
pending "Mark all city by default when creating a poll question from a successful proposal"
context "Poll select box" do
scenario "translates the poll name in options" do
poll = create(:poll, name_en: "Name in English", name_es: "Nombre en Español")
proposal = create(:proposal)
visit admin_proposal_path(proposal)
click_link "Add this proposal to a poll to be voted"
expect(page).to have_select("poll_question_poll_id", options: ["Select Poll", poll.name_en])
select "Español", from: "Language:"
expect(page).to have_select("poll_question_poll_id",
options: ["Seleccionar votación", poll.name_es])
end
scenario "uses fallback if name is not translated to current locale" do
unless globalize_french_fallbacks.first == :es
skip("Spec only useful when French falls back to Spanish")
end
poll = create(:poll, name_en: "Name in English", name_es: "Nombre en Español")
proposal = create(:proposal)
visit admin_proposal_path(proposal)
click_link "Add this proposal to a poll to be voted"
expect(page).to have_select("poll_question_poll_id", options: ["Select Poll", poll.name_en])
select "Français", from: "Language:"
expect(page).to have_select("poll_question_poll_id",
options: ["Sélectionner un vote", poll.name_es])
end
end
def globalize_french_fallbacks
Globalize.fallbacks(:fr).reject { |locale| locale.match(/fr/) }
end
end