Use Capybara methods to find/click/check links

We applied the Capybara/SpecificMatcher in commit f52a86b46.  However,
this rule doesn't convert methods finding <a> tags to methods finding
links because <a> tags only count as links when they've got the `href`
attribute. For instance, in the `xss_spec.rb` file we check what happens
when clicking on an anchor tag because we're testing that the `href`
attribute has been removed and so we can't use `click_link`.

So, basically, we can't enable a rule to automatically detect when we're
using `have_css` instead of `have_link`, but we should still do it
because `have_link` adds an extra check which affects accessibility
since it makes sure the tag has the `href` attribute and so it's
recognizable as a link by screen readers.
This commit is contained in:
Javi Martín
2023-09-10 22:19:01 +02:00
parent 614b4fbe4c
commit ea913f9332
15 changed files with 42 additions and 41 deletions

View File

@@ -44,9 +44,9 @@ describe Shared::LinkListComponent do
["Home", "/", class: "root"], ["Info", "/info", id: "info"]
)
expect(page).to have_css "a", count: 2
expect(page).to have_css "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
expect(page).to have_link count: 2
expect(page).to have_link "Home", class: "root", count: 1
expect(page).to have_link "Info", id: "info", count: 1
end
it "ignores nil entries" do
@@ -55,8 +55,8 @@ describe Shared::LinkListComponent do
)
expect(page).to have_css "li", count: 2
expect(page).to have_css "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
expect(page).to have_link "Home", class: "root", count: 1
expect(page).to have_link "Info", id: "info", count: 1
end
it "ignores empty entries" do
@@ -65,8 +65,8 @@ describe Shared::LinkListComponent do
)
expect(page).to have_css "li", count: 2
expect(page).to have_css "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
expect(page).to have_link "Home", class: "root", count: 1
expect(page).to have_link "Info", id: "info", count: 1
end
it "accepts an optional condition to check the active element" do
@@ -90,8 +90,8 @@ describe Shared::LinkListComponent do
expect(page).to have_css "li", count: 3
expect(page).to have_css "li[aria-current='true']", count: 1, exact_text: "Info"
expect(page).to have_css "a.root", count: 1, exact_text: "Home"
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
expect(page).to have_link "Home", class: "root", count: 1
expect(page).to have_link "Info", id: "info", count: 1
expect(page).to have_css "a[rel='help']", count: 1, exact_text: "Help"
end
end