A list of links is a very common pattern in the web, and we use it in many places. Here we're applying it to one of the most simple ones; the help page. Generally speaking, I'm not a big fan of helpers, but there are methods which IMHO qualify as helpers when: * They do not deal with application objects but mainly strings and arrays * They return text or an HTML tag * Their logic is simple and splitting it into several methods is not necessary Many Rails helpers, like `tag`, follow these principles.
47 lines
1.3 KiB
Ruby
47 lines
1.3 KiB
Ruby
require "rails_helper"
|
|
|
|
describe LinkListHelper do
|
|
describe "#link_list" do
|
|
it "returns an empty string with an empty list" do
|
|
expect(helper.link_list).to eq ""
|
|
end
|
|
|
|
it "returns nothing with a list of nil elements" do
|
|
expect(helper.link_list(nil, nil)).to eq ""
|
|
end
|
|
|
|
it "generates a list of links" do
|
|
list = helper.link_list(["Home", "/"], ["Info", "/info"], class: "menu")
|
|
|
|
expect(list).to eq '<ul class="menu"><li><a href="/">Home</a></li><li><a href="/info">Info</a></li></ul>'
|
|
expect(list).to be_html_safe
|
|
end
|
|
|
|
it "accepts options for links" do
|
|
render helper.link_list(["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 helper.link_list(["Home", "/", class: "root"], nil, ["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
|
|
end
|
|
|
|
attr_reader :content
|
|
|
|
def render(content)
|
|
@content = content
|
|
end
|
|
|
|
def page
|
|
Capybara::Node::Simple.new(content)
|
|
end
|
|
end
|