Files
nairobi/spec/components/shared/link_list_component_spec.rb
Javi Martín ea913f9332 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.
2023-09-11 14:10:41 +02:00

98 lines
2.8 KiB
Ruby

require "rails_helper"
describe Shared::LinkListComponent do
it "renders nothing with an empty list" do
render_inline Shared::LinkListComponent.new
expect(page).not_to be_rendered
end
it "returns nothing with a list of nil elements" do
render_inline Shared::LinkListComponent.new(nil, nil)
expect(page).not_to be_rendered
end
it "generates a list of links" do
render_inline Shared::LinkListComponent.new(
["Home", "/"], ["Info", "/info"], class: "menu"
)
expect(page).to be_rendered with: <<~HTML
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="/info">Info</a></li>
</ul>
HTML
end
it "accepts anchor tags" do
render_inline Shared::LinkListComponent.new(
'<a href="/">Home</a>'.html_safe, ["Info", "/info"], class: "menu"
)
expect(page).to be_rendered with: <<~HTML
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="/info">Info</a></li>
</ul>
HTML
end
it "accepts options for links" do
render_inline Shared::LinkListComponent.new(
["Home", "/", class: "root"], ["Info", "/info", id: "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
render_inline Shared::LinkListComponent.new(
["Home", "/", class: "root"], nil, ["Info", "/info", id: "info"]
)
expect(page).to have_css "li", 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 empty entries" do
render_inline Shared::LinkListComponent.new(
["Home", "/", class: "root"], "", ["Info", "/info", id: "info"]
)
expect(page).to have_css "li", count: 2
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
render_inline Shared::LinkListComponent.new(
["Home", "/", false],
["Info", "/info", true],
["Help", "/help"]
)
expect(page).to have_css "li", count: 3
expect(page).to have_css "li[aria-current='true']", count: 1, exact_text: "Info"
end
it "allows passing both the active condition and link options" do
render_inline Shared::LinkListComponent.new(
["Home", "/", false, class: "root"],
["Info", "/info", true, id: "info"],
["Help", "/help", rel: "help"]
)
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_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