Files
nairobi/spec/system/admin/poll/shifts_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

258 lines
8.8 KiB
Ruby

require "rails_helper"
describe "Admin shifts", :admin do
scenario "Show" do
officer = create(:poll_officer)
booth1 = create(:poll_booth)
booth2 = create(:poll_booth)
create(:poll_shift, officer: officer, booth: booth1, date: Date.current)
create(:poll_shift, officer: officer, booth: booth2, date: Time.zone.tomorrow)
visit new_admin_booth_shift_path(booth1)
expect(page).to have_css(".shift", count: 1)
expect(page).to have_content I18n.l(Date.current, format: :long)
expect(page).to have_content officer.name
expect(page).to have_content officer.email
visit new_admin_booth_shift_path(booth2)
expect(page).to have_css(".shift", count: 1)
expect(page).to have_content I18n.l(Time.zone.tomorrow, format: :long)
expect(page).to have_content officer.name
expect(page).to have_content officer.email
end
scenario "Create Vote Collection Shift and Recount & Scrutiny Shift on same date" do
create(:poll)
poll = create(:poll, :current)
booth = create(:poll_booth, polls: [poll, create(:poll, :expired)])
officer = create(:poll_officer)
vote_collection_dates = (Date.current..poll.ends_at.to_date).to_a.map { |date| I18n.l(date, format: :long) }
recount_scrutiny_dates = (poll.ends_at.to_date..poll.ends_at.to_date + 1.week).to_a.map { |date| I18n.l(date, format: :long) }
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_content "This booth has no shifts"
fill_in "search", with: officer.email
click_button "Search"
click_link "Edit shifts"
expect(page).to have_select("shift_date_vote_collection_date", options: ["Select day", *vote_collection_dates])
expect(page).not_to have_select("shift_date_recount_scrutiny_date")
select I18n.l(Date.current, format: :long), from: "shift_date_vote_collection_date"
click_button "Add shift"
expect(page).to have_content "Shift added"
within("#shifts") do
expect(page).to have_css(".shift", count: 1)
expect(page).to have_content(I18n.l(Date.current, format: :long))
expect(page).to have_content("Collect Votes")
expect(page).to have_content(officer.name)
end
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 1)
fill_in "search", with: officer.email
click_button "Search"
click_link "Edit shifts"
select "Recount & Scrutiny", from: "shift_task"
expect(page).to have_select("shift_date_recount_scrutiny_date", options: ["Select day", *recount_scrutiny_dates])
expect(page).not_to have_select("shift_date_vote_collection_date")
select I18n.l(poll.ends_at.to_date + 4.days, format: :long), from: "shift_date_recount_scrutiny_date"
click_button "Add shift"
expect(page).to have_content "Shift added"
within("#shifts") do
expect(page).to have_css(".shift", count: 2)
expect(page).to have_content(I18n.l(poll.ends_at.to_date + 4.days, format: :long))
expect(page).to have_content("Recount & Scrutiny")
expect(page).to have_content(officer.name)
end
end
scenario "Vote Collection Shift and Recount & Scrutiny Shift don't include already assigned dates to officer" do
poll = create(:poll, :current)
booth = create(:poll_booth, polls: [poll])
officer = create(:poll_officer)
create(:poll_shift, :vote_collection_task, officer: officer, booth: booth, date: Date.current)
create(:poll_shift, :recount_scrutiny_task, officer: officer, booth: booth, date: Time.zone.tomorrow)
vote_collection_dates = (Date.current..poll.ends_at.to_date).to_a
.reject { |date| date == Date.current }
.map { |date| I18n.l(date, format: :long) }
recount_scrutiny_dates = (poll.ends_at.to_date..poll.ends_at.to_date + 1.week).to_a
.reject { |date| date == Time.zone.tomorrow }
.map { |date| I18n.l(date, format: :long) }
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 2)
fill_in "search", with: officer.email
click_button "Search"
click_link "Edit shifts"
expect(page).to have_select("shift_date_vote_collection_date", options: ["Select day", *vote_collection_dates])
select "Recount & Scrutiny", from: "shift_task"
expect(page).to have_select("shift_date_recount_scrutiny_date", options: ["Select day", *recount_scrutiny_dates])
end
scenario "Change option from Recount & Scrutinity to Collect Votes" do
booth = create(:poll_booth)
officer = create(:poll_officer)
create(:poll_shift, :vote_collection_task, officer: officer, booth: booth)
create(:poll_shift, :recount_scrutiny_task, officer: officer, booth: booth)
visit new_admin_booth_shift_path(booth, officer_id: officer.id)
select "Recount & Scrutiny", from: "shift_task"
expect(page).to have_select("shift_date_recount_scrutiny_date", options: ["Select day"])
select "Collect Votes", from: "shift_task"
expect(page).to have_select("shift_date_vote_collection_date", options: ["Voting days ended"])
end
scenario "Error on create" do
poll = create(:poll, :current)
booth = create(:poll_booth, polls: [poll])
officer = create(:poll_officer)
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_content "This booth has no shifts"
fill_in "search", with: officer.email
click_button "Search"
click_link "Edit shifts"
click_button "Add shift"
expect(page).to have_content "A date must be selected"
end
scenario "Destroy" do
poll = create(:poll, :current)
booth = create(:poll_booth, polls: [poll])
officer = create(:poll_officer)
shift = create(:poll_shift, officer: officer, booth: booth)
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 1)
within("#shift_#{shift.id}") do
accept_confirm { click_button "Remove" }
end
expect(page).to have_content "Shift removed"
expect(page).to have_css(".shift", count: 0)
end
scenario "Try to destroy with associated recount" do
assignment = create(:poll_booth_assignment)
officer_assignment = create(:poll_officer_assignment, booth_assignment: assignment)
create(:poll_recount, booth_assignment: assignment, officer_assignment: officer_assignment)
officer = officer_assignment.officer
booth = assignment.booth
shift = create(:poll_shift, officer: officer, booth: booth)
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 1)
within("#shift_#{shift.id}") do
accept_confirm { click_button "Remove" }
end
expect(page).not_to have_content "Shift removed"
expect(page).to have_content "Shifts with associated results or recounts cannot be deleted"
expect(page).to have_css(".shift", count: 1)
end
scenario "try to destroy with associated partial results" do
assignment = create(:poll_booth_assignment)
officer_assignment = create(:poll_officer_assignment, booth_assignment: assignment)
create(:poll_partial_result,
booth_assignment: assignment,
officer_assignment: officer_assignment)
officer = officer_assignment.officer
booth = assignment.booth
shift = create(:poll_shift, officer: officer, booth: booth)
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 1)
within("#shift_#{shift.id}") do
accept_confirm { click_button "Remove" }
end
expect(page).not_to have_content "Shift removed"
expect(page).to have_content "Shifts with associated results or recounts cannot be deleted"
expect(page).to have_css(".shift", count: 1)
end
scenario "Destroy an officer" do
booth = create(:poll_booth)
officer = create(:poll_officer)
create(:poll_shift, officer: officer, booth: booth)
officer.destroy!
visit new_admin_booth_shift_path(booth)
expect(page).to have_css(".shift", count: 1)
expect(page).to have_content(officer.name)
expect(page).to have_content(officer.email)
end
scenario "Empty" do
booth = create(:poll_booth)
visit new_admin_booth_shift_path(booth)
expect(page).to have_content "This booth has no shifts"
end
end