require "test_helper" class BreadcrumbsTest < Test::Unit::TestCase def setup @breadcrumbs = Breadcrumbs.new @inline = Breadcrumbs::Render::Inline.new(@breadcrumbs) end def test_return_safe_html html_mock = mock html_mock.expects(:html_safe).once Breadcrumbs::Render::List.any_instance.stubs(:render).returns(html_mock) @breadcrumbs.render(:format => :list) end def test_add_item @breadcrumbs.add "Home" assert_equal 1, @breadcrumbs.items.count @breadcrumbs << "Home" assert_equal 2, @breadcrumbs.items.count end def test_tag assert_equal "Hi!", @inline.tag(:span, "Hi!") end def test_tag_with_attributes expected = %[Hi!] assert_equal expected, @inline.tag(:span, "Hi!", :class => "greetings", :id => "hi") end def test_tag_with_block assert_equal "Hi!", @inline.tag(:span) { "Hi!" } end def test_tag_with_block_and_attributes expected = %[Hi!] assert_equal expected, @inline.tag(:span, :class => "greetings", :id => "hi") { "Hi!" } end def test_nested_tags expected = %[Hi!] actual = @inline.tag(:span, :class => "greetings") { tag(:strong, "Hi!", :id => "hi") } assert_equal expected, actual end def test_render_as_list @breadcrumbs.add "Home", "/", :class => "home" html = Nokogiri::HTML(@breadcrumbs.render) assert_not_nil html.at("ul.breadcrumbs") end def test_render_as_ordered_list @breadcrumbs.add "Home", "/" html = Nokogiri::HTML(@breadcrumbs.render(:format => :ordered_list)) assert_not_nil html.at("ol.breadcrumbs") end def test_render_as_list_with_custom_attributes @breadcrumbs.add "Home", "/", :class => "home" html = Nokogiri::HTML(@breadcrumbs.render(:id => "breadcrumbs", :class => "top")) assert_not_nil html.at("ul.top#breadcrumbs") end def test_render_as_list_add_items @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") assert_not_nil items[2].at("span") end def test_render_inline @breadcrumbs.add "Home", "/", :class => "home" html = Nokogiri::HTML(@breadcrumbs.render(:format => :inline)) assert_nil html.at("ul.breadcrumbs") end def test_render_inline_add_items @breadcrumbs.add "Home", "/", :class => "home" @breadcrumbs.add "About", "/about", :class => "about" @breadcrumbs.add "People" html = @breadcrumbs.render(:format => :inline) html = Nokogiri::HTML("