This way it's easier to refactor it and/or change it.
Back in commit c156621a4 I wrote:
> Generally speaking, I'm not a big fan of helpers, but there are
> methods which IMHO qualify as helpers when (...) many Rails helpers,
> like `tag`, follow these principles.
It's time to modify these criteria a little bit. In some situations,
it's great to have a helper method so it can be easily used in view
(like `link_to`). However, from the maintenance point of view, helper
methods are usually messy because extracting methods requires making
sure there isn't another helper method with that name.
So we can use the best part of these worlds and provide a helper so it
can be easily called from the view, but internally make that helper
render a component and enjoy the advantages associated with using an
isolated Ruby class.
94 lines
3.0 KiB
Ruby
94 lines
3.0 KiB
Ruby
require "rails_helper"
|
|
|
|
describe Shared::LinkListComponent, type: :component do
|
|
it "renders nothing with an empty list" do
|
|
render_inline Shared::LinkListComponent.new
|
|
|
|
expect(page).not_to have_css "ul"
|
|
end
|
|
|
|
it "returns nothing with a list of nil elements" do
|
|
render_inline Shared::LinkListComponent.new(nil, nil)
|
|
|
|
expect(page).not_to have_css "ul"
|
|
end
|
|
|
|
it "generates a list of links" do
|
|
render_inline Shared::LinkListComponent.new(
|
|
["Home", "/"], ["Info", "/info"], class: "menu"
|
|
)
|
|
list = page.find("body").native.inner_html
|
|
|
|
expect(list).to eq '<ul class="menu">' + "\n" +
|
|
'<li><a href="/">Home</a></li>' + "\n" +
|
|
'<li><a href="/info">Info</a></li>' + "\n</ul>"
|
|
end
|
|
|
|
it "accepts anchor tags" do
|
|
render_inline Shared::LinkListComponent.new(
|
|
'<a href="/">Home</a>'.html_safe, ["Info", "/info"], class: "menu"
|
|
)
|
|
list = page.find("body").native.inner_html
|
|
|
|
expect(list).to eq '<ul class="menu">' + "\n" +
|
|
'<li><a href="/">Home</a></li>' + "\n" +
|
|
'<li><a href="/info">Info</a></li>' + "\n</ul>"
|
|
end
|
|
|
|
it "accepts options for links" do
|
|
render_inline Shared::LinkListComponent.new(
|
|
["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"
|
|
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_css "a.root", count: 1, exact_text: "Home"
|
|
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
|
|
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_css "a.root", count: 1, exact_text: "Home"
|
|
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
|
|
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_css "a.root", count: 1, exact_text: "Home"
|
|
expect(page).to have_css "a#info", count: 1, exact_text: "Info"
|
|
expect(page).to have_css "a[rel='help']", count: 1, exact_text: "Help"
|
|
end
|
|
end
|