require 'test_helper'
class HelperMethodsTest < ActionView::TestCase
include Gretel::ViewHelpers
fixtures :all
helper :application
setup do
Gretel.reset!
Gretel::Trail.secret = "128107d341e912db791d98bbe874a8250f784b0a0b4dbc5d5032c0fc1ca7bda9c6ece667bd18d23736ee833ea79384176faeb54d2e0d21012898dde78631cdf1"
end
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 "shows root alone" do
breadcrumb :root
assert_equal %{Home
},
breadcrumbs(:show_root_alone => 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 %{You are here:
Home >
About },
breadcrumbs(:pretext => "You are here: ")
end
test "shows posttext" do
breadcrumb :basic
assert_equal %{Home >
About - text after breadcrumbs
},
breadcrumbs(:posttext => " - text after breadcrumbs")
end
test "autoroot disabled" do
breadcrumb :basic
assert_equal %{About
},
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 %{},
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 %{Home >
Test <strong>bold text</strong> },
breadcrumbs
end
test "safe html" do
breadcrumb :with_safe_html
assert_equal %{},
breadcrumbs
end
test "works with legacy breadcrumb rendering method" do
breadcrumb :basic
assert_equal %{},
breadcrumb
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 "without link" do
breadcrumb :without_link
assert_equal %{Home > Also without link >
Without link },
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 %{},
breadcrumb
end
test "calling breadcrumbs helper twice" do
breadcrumb :with_parent
2.times do
assert_equal %{},
breadcrumbs
end
end
test "trail helper" do
breadcrumb :basic
assert_equal "12hY7tdmRCBzQ_LS0tCi0gLSA6YmFzaWMKICAtIEFib3V0CiAgLSAvYWJvdXQK", breadcrumb_trail
end
test "loading trail" do
params[:trail] = "12hY7tdmRCBzQ_LS0tCi0gLSA6YmFzaWMKICAtIEFib3V0CiAgLSAvYWJvdXQK"
breadcrumb :multiple_links
assert_equal %{},
breadcrumbs
end
test "different trail param" do
Gretel::Trail.trail_param = :mytest
params[:mytest] = "12hY7tdmRCBzQ_LS0tCi0gLSA6YmFzaWMKICAtIEFib3V0CiAgLSAvYWJvdXQK"
breadcrumb :multiple_links
assert_equal %{},
breadcrumbs
end
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