# frozen_string_literal: true require "test_helper" class BreadcrumbsTest < Minitest::Test def setup @breadcrumbs = Breadcrumbs.new @inline = Breadcrumbs::Render::Inline.new(@breadcrumbs) end test "returns safe html" do assert @breadcrumbs.render(format: "list").html_safe? end test "adds item" do @breadcrumbs.add "Home" assert_equal 1, @breadcrumbs.items.count @breadcrumbs << "Home" assert_equal 2, @breadcrumbs.items.count end test "renders tag with attributes" do expected = %[Hi!] assert_equal expected, @inline.tag(:span, "Hi!", class: "greetings", id: "hi") end test "renders tag with block" do assert_equal "Hi!", @inline.tag(:span) { "Hi!" } end test "renders tag with block and attributes" do expected = %[Hi!] assert_equal expected, @inline.tag(:span, class: "greetings", id: "hi") { "Hi!" } end test "renders nested tags" do expected = %[Hi!] actual = @inline.tag(:span, class: "greetings") do @inline.tag(:strong, "Hi!", id: "hi") end assert_equal expected, actual end test "renders as list" do @breadcrumbs.add "Home", "/", class: "home" html = Nokogiri::HTML(@breadcrumbs.render) refute_nil html.at("ul.breadcrumbs") assert_nil html.at("ul.breadcrumbs[format=list]") end test "renders as ordered list" do @breadcrumbs.add "Home", "/" html = Nokogiri::HTML(@breadcrumbs.render(format: "ordered_list")) refute_nil html.at("ol.breadcrumbs") end test "renders as list with custom attributes" do @breadcrumbs.add "Home", "/", class: "home" html = Nokogiri::HTML(@breadcrumbs.render(id: "breadcrumbs", class: "top")) refute_nil html.at("ul.top#breadcrumbs") end test "renders as list add items" do @breadcrumbs.add "Home", "/", class: "home" @breadcrumbs.add "About", "/about", class: "about" @breadcrumbs.add "People" html = Nokogiri::HTML(@breadcrumbs.render) items = html.search("li") assert_equal 3, items.count assert_equal "first item-0", items[0]["class"] assert_equal "Home", items[0].inner_text link = items[0].at("a") assert_equal "home", link["class"] assert_equal "/", link["href"] assert_equal "item-1", items[1]["class"] assert_equal "About", items[1].inner_text link = items[1].at("a") assert_equal "about", link["class"] assert_equal "/about", link["href"] assert_equal "last item-2", items[2]["class"] assert_equal "People", items[2].inner_text assert_nil items[2].at("a") refute_nil items[2].at("span") end test "renders inline" do @breadcrumbs.add "Home", "/", class: "home" html = Nokogiri::HTML(@breadcrumbs.render(format: "inline")) assert_nil html.at("ul.breadcrumbs") end test "renders inline add items" do @breadcrumbs.add "Home", "/", class: "home" @breadcrumbs.add "About", "/about", class: "about" @breadcrumbs.add "People" html = @breadcrumbs.render(format: "inline") html = Nokogiri::HTML("