require 'test_helper' class HelperMethodsTest < ActionView::TestCase include Gretel::ViewHelpers fixtures :all helper :application setup do Gretel.reset! end # Breadcrumb generation test "shows basic breadcrumb" do breadcrumb :basic assert_equal %{}, breadcrumbs end test "shows breadcrumb with root" do breadcrumb :with_root assert_equal %{}, breadcrumbs end test "shows breadcrumb with parent" do breadcrumb :with_parent assert_equal %{}, breadcrumbs end test "shows breadcrumb with autopath" do breadcrumb :with_autopath, projects(:one) assert_equal %{}, breadcrumbs end test "shows breadcrumb with parent object" do breadcrumb :with_parent_object, issues(:one) assert_equal %{}, breadcrumbs end test "shows multiple links" do breadcrumb :multiple_links assert_equal %{}, breadcrumbs end test "shows multiple links with parent" do breadcrumb :multiple_links_with_parent assert_equal %{}, breadcrumbs end test "shows semantic breadcrumb" do breadcrumb :with_root assert_equal %{}, breadcrumbs(semantic: true) end test "doesn't show root alone" do breadcrumb :root assert_equal "", breadcrumbs end test "displays single fragment" do breadcrumb :root assert_equal %{}, breadcrumbs(display_single_fragment: true) end test "displays single non-root fragment" do breadcrumb :basic assert_equal %{}, breadcrumbs(autoroot: false, display_single_fragment: true) end test "shows no breadcrumb" do assert_equal "", breadcrumbs end test "links current breadcrumb" do breadcrumb :with_root assert_equal %{}, breadcrumbs(link_current: true) end test "shows pretext" do breadcrumb :basic assert_equal %{}, breadcrumbs(pretext: "You are here: ") end test "shows posttext" do breadcrumb :basic assert_equal %{}, breadcrumbs(posttext: " - text after breadcrumbs") end test "autoroot disabled" do breadcrumb :basic assert_equal "", breadcrumbs(autoroot: false) end test "shows separator" do breadcrumb :with_root assert_equal %{}, breadcrumbs(separator: " » ") end test "shows element id" do breadcrumb :basic assert_equal %{}, breadcrumbs(id: "custom_id") end test "shows custom container class" do breadcrumb :basic assert_equal %{
HomeAbout
}, breadcrumbs(class: "custom_class") end test "shows custom current class" do breadcrumb :basic assert_equal %{}, breadcrumbs(current_class: "custom_current_class") end test "unsafe html" do breadcrumb :with_unsafe_html assert_equal %{}, breadcrumbs end test "safe html" do breadcrumb :with_safe_html assert_equal %{}, breadcrumbs end test "yields a block containing breadcrumb links array" do breadcrumb :multiple_links_with_parent out = breadcrumbs do |links| links.map { |link| [link.key, link.text, link.url] } end assert_equal [[:root, "Home", "/"], [:basic, "About", "/about"], [:multiple_links_with_parent, "Contact", "/about/contact"], [:multiple_links_with_parent, "Contact form", "/about/contact/form"]], out end test "parent breadcrumb" do breadcrumb :multiple_links_with_parent parent = parent_breadcrumb assert_equal [:multiple_links_with_parent, "Contact", "/about/contact"], [parent.key, parent.text, parent.url] end test "yields parent breadcrumb" do breadcrumb :multiple_links_with_parent out = parent_breadcrumb do |parent| [parent.key, parent.text, parent.url] end assert_equal [:multiple_links_with_parent, "Contact", "/about/contact"], out end test "parent breadcrumb returns nil if not present" do breadcrumb :basic assert_nil parent_breadcrumb(autoroot: false) end test "parent breadcrumb yields only if present" do breadcrumb :basic out = parent_breadcrumb(autoroot: false) do "yielded" end assert_nil out end test "sets current on last link in array" do breadcrumb :multiple_links_with_parent out = breadcrumbs do |links| links.map(&:current?) end assert_equal [false, false, false, true], out end test "without link" do breadcrumb :without_link assert_equal %{}, breadcrumbs end test "view context" do breadcrumb :using_view_helper assert_equal %{}, breadcrumbs end test "multiple arguments" do breadcrumb :with_multiple_arguments, "One", "Two", "Three" assert_equal %{}, breadcrumbs end test "with_breadcrumb" do breadcrumb :basic assert_equal %{}, breadcrumbs with_breadcrumb(:with_parent_object, issues(:one)) do assert_equal %{}, breadcrumbs end assert_equal %{}, breadcrumbs end test "calling breadcrumbs helper twice" do breadcrumb :with_parent 2.times do assert_equal %{}, breadcrumbs end end test "breadcrumb not found" do assert_raises ArgumentError do breadcrumb :nonexistent breadcrumbs end end test "current link url is set to fullpath" do self.request = OpenStruct.new(fullpath: "/testpath?a=1&b=2") breadcrumb :basic assert_equal "/testpath?a=1&b=2", breadcrumbs { |links| links.last.url } end test "calling the breadcrumb method with wrong arguments" do assert_nothing_raised do breadcrumb :basic, test: 1 end assert_raises ArgumentError do breadcrumb end assert_raises ArgumentError do breadcrumb(pretext: "bla") end end # Styles test "default style" do breadcrumb :basic assert_equal %{}, breadcrumbs end test "ordered list style" do breadcrumb :basic assert_equal %{}, breadcrumbs(style: :ol) end test "unordered list style" do breadcrumb :basic assert_equal %{}, breadcrumbs(style: :ul) end test "bootstrap style" do breadcrumb :basic assert_equal %{}, breadcrumbs(style: :bootstrap) end test "custom container and fragment tags" do breadcrumb :basic assert_equal %{HomeAbout}, breadcrumbs(container_tag: :c, fragment_tag: :f) end test "custom semantic container and fragment tags" do breadcrumb :basic assert_equal %{About}, breadcrumbs(container_tag: :c, fragment_tag: :f, semantic: true) end test "unknown style" do breadcrumb :basic assert_raises ArgumentError do breadcrumbs(style: :nonexistent) end end test "register style" do Gretel.register_style :test_style, { container_tag: :one, fragment_tag: :two } breadcrumb :basic assert_equal %{HomeAbout}, breadcrumbs(style: :test_style) end # Configuration reload test "reload configuration when file is changed" do path = setup_loading_from_tmp_folder Gretel.reload_environments << "test" File.open(path.join("site.rb"), "w") do |f| f.write <<-EOT crumb :root do link "Home (loaded)", root_path end crumb :about do link "About (loaded)", about_path end EOT end breadcrumb :about assert_equal %{}, breadcrumbs sleep 1 # File change interval is 1 second File.open(path.join("site.rb"), "w") do |f| f.write <<-EOT crumb :root do link "Home (reloaded)", "/test" end crumb :about do link "About (reloaded)", "/reloaded" end EOT end breadcrumb :about assert_equal %{}, breadcrumbs end test "reload configuration when file is added" do path = setup_loading_from_tmp_folder Gretel.reload_environments << "test" File.open(path.join("site.rb"), "w") do |f| f.write <<-EOT crumb :root do link "Home (loaded)", root_path end EOT end assert_raises ArgumentError do breadcrumb :about breadcrumbs end File.open(path.join("pages.rb"), "w") do |f| f.write <<-EOT crumb :about do link "About (loaded)", about_path end EOT end breadcrumb :about assert_equal %{}, breadcrumbs end test "reload configuration when file is deleted" do path = setup_loading_from_tmp_folder Gretel.reload_environments << "test" File.open(path.join("site.rb"), "w") do |f| f.write <<-EOT crumb :root do link "Home (loaded)", root_path end crumb :about do link "About (loaded)", about_path end EOT end File.open(path.join("pages.rb"), "w") do |f| f.write <<-EOT crumb :contact do link "Contact (loaded)", "/contact" parent :about end EOT end breadcrumb :contact assert_equal %{}, breadcrumbs File.delete path.join("pages.rb") assert_raises ArgumentError do breadcrumb :contact breadcrumbs end breadcrumb :about assert_equal %{}, breadcrumbs end test "reloads only in development environment" do path = setup_loading_from_tmp_folder assert_equal ["development"], Gretel.reload_environments File.open(path.join("site.rb"), "w") do |f| f.write <<-EOT crumb :root do link "Home (loaded)", root_path end crumb :about do link "About (loaded)", about_path end EOT end breadcrumb :about assert_equal %{}, breadcrumbs sleep 1 File.open(path.join("site.rb"), "w") do |f| f.write <<-EOT crumb :root do link "Home (reloaded)", "/test" end crumb :about do link "About (reloaded)", "/reloaded" end EOT end breadcrumb :about assert_equal %{}, breadcrumbs end private def setup_loading_from_tmp_folder path = Rails.root.join("tmp", "testcrumbs") FileUtils.rm_rf path FileUtils.mkdir_p path Gretel.breadcrumb_paths = [path.join("*.rb")] path end end