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

247 lines
9.2 KiB
Ruby

require "rails_helper"
describe "Admin budget headings", :admin do
let(:budget) { create(:budget, :drafting) }
let(:group) { create(:budget_group, budget: budget) }
context "Load" do
let!(:budget) { create(:budget, slug: "budget_slug") }
let!(:group) { create(:budget_group, slug: "group_slug", budget: budget) }
let!(:heading) { create(:budget_heading, slug: "heading_slug", group: group) }
scenario "finds budget, group and heading by slug" do
visit edit_admin_budget_group_heading_path("budget_slug", "group_slug", "heading_slug")
expect(page).to have_content(budget.name)
expect(page).to have_content(group.name)
expect(page).to have_field "Heading name", with: heading.name
end
end
context "Index" do
scenario "Displaying no headings for group" do
visit admin_budget_group_headings_path(budget, group)
expect(page).to have_content "There are no headings."
end
scenario "Displaying headings" do
heading1 = create(:budget_heading, group: group, price: 1000, allow_custom_content: true)
heading2 = create(:budget_heading, group: group, price: 2000, population: 10000)
heading3 = create(:budget_heading, group: group, price: 3000, population: 10000)
visit admin_budget_group_headings_path(budget, group)
expect(page).to have_content "There are 3 headings"
within "#budget_heading_#{heading1.id}" do
expect(page).to have_content(heading1.name)
expect(page).to have_content "€1,000"
expect(page).not_to have_content "10000"
expect(page).to have_content "Yes"
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
within "#budget_heading_#{heading2.id}" do
expect(page).to have_content(heading2.name)
expect(page).to have_content "€2,000"
expect(page).to have_content "10000"
expect(page).to have_content "No"
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
within "#budget_heading_#{heading3.id}" do
expect(page).to have_content(heading3.name)
expect(page).to have_content "€3,000"
expect(page).to have_content "10000"
expect(page).to have_content "No"
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
end
scenario "Delete a heading without investments" do
heading = create(:budget_heading, group: group)
visit admin_budget_group_headings_path(budget, group)
within("#budget_heading_#{heading.id}") { accept_confirm { click_button "Delete" } }
expect(page).to have_content "Heading deleted successfully"
expect(page).not_to have_selector "#budget_heading_#{heading.id}"
end
scenario "Try to delete a heading with investments" do
heading = create(:budget_heading, group: group, name: "Atlantis")
create(:budget_investment, heading: heading)
visit admin_budget_group_headings_path(budget, group)
within(".heading", text: "Atlantis") { accept_confirm { click_button "Delete" } }
expect(page).to have_content "You cannot delete a Heading that has associated investments"
expect(page).to have_content "Atlantis"
end
end
context "New" do
scenario "Create heading" do
visit admin_budget_group_headings_path(budget, group)
click_link "Create new heading"
fill_in "Heading name", with: "All City"
fill_in "Money amount", with: "1000"
fill_in "Population (optional)", with: "10000"
check "Allow content block"
click_button "Create new heading"
expect(page).to have_content "Heading created successfully!"
expect(page).to have_content "All City"
expect(page).to have_content "€1,000"
expect(page).to have_content "10000"
expect(page).to have_content "Yes"
end
scenario "Heading name is mandatory" do
visit new_admin_budget_group_heading_path(budget, group)
click_button "Create new heading"
expect(page).not_to have_content "Heading created successfully!"
expect(page).to have_css(".is-invalid-label", text: "Heading name")
expect(page).to have_content "can't be blank"
end
scenario "Heading amount is mandatory" do
visit new_admin_budget_group_heading_path(budget, group)
click_button "Create new heading"
expect(page).not_to have_content "Heading created successfully!"
expect(page).to have_css(".is-invalid-label", text: "Money amount")
expect(page).to have_content "can't be blank"
end
describe "Max votes is optional" do
scenario "do no show max_ballot_lines field for knapsack budgets" do
visit new_admin_budget_group_heading_path(budget, group)
expect(page).not_to have_field "Votes allowed"
end
scenario "create heading with max_ballot_lines for appoval budgets" do
budget.update!(voting_style: "approval")
visit new_admin_budget_group_heading_path(budget, group)
expect(page).to have_field "Votes allowed", with: 1
fill_in "Heading name", with: "All City"
fill_in "Money amount", with: "1000"
fill_in "Votes allowed", with: 14
click_button "Create new heading"
expect(page).to have_content "Heading created successfully!"
within("tr", text: "All City") { expect(page).to have_content 14 }
end
end
end
context "Edit" do
scenario "Show heading information" do
heading = create(:budget_heading, group: group)
visit admin_budget_group_headings_path(budget, group)
within("#budget_heading_#{heading.id}") { click_link "Edit" }
expect(page).to have_field "Heading name", with: heading.name
expect(page).to have_field "Money amount", with: heading.price
expect(page).to have_field "Population (optional)", with: heading.population
expect(page).to have_field "Longitude (optional)", with: heading.longitude
expect(page).to have_field "Latitude (optional)", with: heading.latitude
expect(find_field("Allow content block")).not_to be_checked
end
scenario "Changing name for current locale will update the slug if budget is in draft phase" do
heading = create(:budget_heading, group: group, name: "Old English Name")
visit edit_admin_budget_group_heading_path(budget, group, heading)
select "Español", from: :add_language
fill_in "Heading name", with: "Spanish name"
click_button "Save heading"
expect(page).to have_content "Heading updated successfully"
visit budget_investments_path(budget, heading_id: "old-english-name")
expect(page).to have_content "Old English Name"
visit edit_admin_budget_group_heading_path(budget, group, heading)
select "English", from: :select_language
fill_in "Heading name", with: "New English Name"
click_button "Save heading"
expect(page).to have_content "Heading updated successfully"
visit budget_investments_path(budget, heading_id: "new-english-name")
expect(page).to have_content "New English Name"
end
end
context "Update" do
let(:heading) do
create(:budget_heading,
group: group,
name: "All City",
price: 1000,
population: 10000,
longitude: 20.50,
latitude: -10.50,
allow_custom_content: true)
end
scenario "Updates group" do
visit edit_admin_budget_group_heading_path(budget, group, heading)
expect(page).to have_field "Heading name", with: "All City"
expect(page).to have_field "Money amount", with: 1000
expect(page).to have_field "Population (optional)", with: 10000
expect(page).to have_field "Longitude (optional)", with: 20.50
expect(page).to have_field "Latitude (optional)", with: -10.50
expect(find_field("Allow content block")).to be_checked
fill_in "Heading name", with: "Districts"
fill_in "Money amount", with: "2000"
fill_in "Population (optional)", with: "20000"
fill_in "Longitude (optional)", with: "-40.47"
fill_in "Latitude (optional)", with: "25.25"
uncheck "Allow content block"
click_button "Save heading"
expect(page).to have_content "Heading updated successfully"
visit edit_admin_budget_group_heading_path(budget, group, heading)
expect(page).to have_field "Heading name", with: "Districts"
expect(page).to have_field "Money amount", with: 2000
expect(page).to have_field "Population (optional)", with: 20000
expect(page).to have_field "Longitude (optional)", with: -40.47
expect(page).to have_field "Latitude (optional)", with: 25.25
expect(find_field("Allow content block")).not_to be_checked
end
scenario "Heading name is already used" do
create(:budget_heading, group: group, name: "Districts")
visit edit_admin_budget_group_heading_path(budget, group, heading)
expect(page).to have_field "Heading name", with: "All City"
fill_in "Heading name", with: "Districts"
click_button "Save heading"
expect(page).not_to have_content "Heading updated successfully"
expect(page).to have_css(".is-invalid-label", text: "Heading name")
expect(page).to have_css("small.form-error", text: "has already been taken")
end
end
end