Files
grecia/spec/system/sdg_management/local_targets_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

119 lines
3.9 KiB
Ruby

require "rails_helper"
describe "Local Targets" do
before do
login_as(create(:administrator).user)
Setting["feature.sdg"] = true
end
describe "Index" do
scenario "Visit the index" do
create(:sdg_local_target, code: "1.1.1", title: "Affordable food")
visit sdg_management_goals_path
click_link "Local Targets"
expect(page).to have_title "SDG content - Local Targets"
within("table tr", text: "Affordable food") do
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
expect(page).to have_link "Create local target"
end
scenario "Show local targets grouped by target" do
target_1 = SDG::Target["1.1"]
target_1_local_target = create(:sdg_local_target, code: "1.1.1", target: target_1)
target_2 = SDG::Target["2.1"]
target_2_local_target = create(:sdg_local_target, code: "2.1.1", target: target_2)
visit sdg_management_local_targets_path
expect(target_1.title).to appear_before(target_1_local_target.title)
expect(target_1_local_target.title).to appear_before(target_2.title)
expect(target_2.title).to appear_before(target_2_local_target.title)
end
end
describe "Create" do
scenario "Shows succesful notice when form is fullfilled correctly" do
visit new_sdg_management_local_target_path
target = SDG::Target["1.1"]
select "#{target.code} #{target.title}", from: "Target"
fill_in "Code", with: "1.1.1"
fill_in "Title", with: "Local target title"
fill_in "Description", with: "Local target description"
click_button "Create local target"
expect(page).to have_content("Local target created successfully")
expect(page).to have_content("1.1.1")
end
scenario "Shows form errors when not valid" do
visit new_sdg_management_local_target_path
target = SDG::Target["2.3"]
code_and_title = "#{target.code} #{target.title}"
select code_and_title, from: "Target"
click_button "Create local target"
expect(page).to have_content("errors prevented this local target from being saved.")
expect(page).to have_select("Target", selected: code_and_title)
end
end
describe "Update" do
let!(:local_target) { create(:sdg_local_target, code: "1.1.1") }
scenario "Shows succesful notice when form is fullfilled correctly" do
visit edit_sdg_management_local_target_path(local_target)
fill_in "Title", with: "Local target title update"
click_button "Update local target"
expect(page).to have_content("Local target updated successfully")
expect(page).to have_content("Local target title update")
end
scenario "Shows form errors when changes are not valid" do
visit edit_sdg_management_local_target_path(local_target)
fill_in "Title", with: ""
click_button "Update local target"
expect(page).to have_content("1 error prevented this local target from being saved.")
end
end
describe "Destroy" do
scenario "Shows succesful notice when local target is destroyed successfully" do
create(:sdg_local_target, code: "1.1.1")
visit sdg_management_local_targets_path
accept_confirm { click_button "Delete" }
expect(page).to have_content("Local target deleted successfully")
expect(page).not_to have_content("1.1.1")
end
end
describe "When translation interface feature setting" do
scenario "Is enabled translation interface should be rendered" do
Setting["feature.translation_interface"] = true
visit new_sdg_management_local_target_path
expect(page).to have_css ".globalize-languages"
end
scenario "Is disabled translation interface should be rendered" do
Setting["feature.translation_interface"] = nil
visit new_sdg_management_local_target_path
expect(page).to have_css ".globalize-languages"
end
end
end