# encoding: utf-8 #Credit for this goes to https://github.com/julescopeland/Rails-Bootstrap-Navbar require 'spec_helper' require 'action_view' require 'active_support' require_relative '../../../app/helpers/railsstrap/navbar_helper' include ActionView::Helpers include ActionView::Context include Railsstrap::NavbarHelper describe Railsstrap::NavbarHelper, :type => :helper do before do allow(self).to receive(:uri_state) { :inactive } allow(self).to receive(:root_url) { '/' } end describe "nav_bar" do it "should return a basic railsstrap navbar" do expect(nav_bar.gsub(/\s/, '').downcase) .to eql(BASIC_NAVBAR.gsub(/\s/, '').downcase) end it "should set the fixed position to top" do expect(nav_bar(:fixed => :top).gsub(/\s/, '').downcase) .to eql(FIXED_TOP_NAVBAR.gsub(/\s/, '').downcase) end it "should set the static position to top" do expect(nav_bar(:static => :top).gsub(/\s/, '').downcase) .to eql(STATIC_TOP_NAVBAR.gsub(/\s/, '').downcase) end it "should set the fixed position to bottom" do expect(nav_bar(:fixed => :bottom).gsub(/\s/, '').downcase) .to eql(FIXED_BOTTOM_NAVBAR.gsub(/\s/, '').downcase) end it "should set the style to inverse" do expect(nav_bar(:inverse => true).gsub(/\s/, '').downcase) .to eql(INVERSE_NAVBAR.gsub(/\s/, '').downcase) end it "should add the brand name and link it to the home page" do expect(nav_bar(:brand => "Ninety Ten").gsub(/\s/, '').downcase) .to eql(NAVBAR_WITH_BRAND.gsub(/\s/, '').downcase) end it "should be able to set the brand link url" do expect(nav_bar(:brand => "Ninety Ten", :brand_link => "http://www.ninetyten.com").gsub(/\s/, '').downcase) .to eql(NAVBAR_WITH_BRAND_AND_LINK.gsub(/\s/, '').downcase) end it "should be able to set the brand link url without a turbolink" do expect(nav_bar(:brand => "Ninety Ten", :brand_link => "http://www.ninetyten.com", :no_turbolink => true).gsub(/\s/, '').downcase) .to eql(NAVBAR_WITH_BRAND_AND_LINK_TBLINK.gsub(/\s/, '').downcase) end it "should add the buttons etc for a responsive layout with no block passed" do expect(nav_bar(:responsive => true).gsub(/\s/, '').downcase).to eql(RESPONSIVE_NAVBAR.gsub(/\s/, '').downcase) end it "should add the buttons etc for a responsive layout with block passed" do ele = nav_bar(:responsive => true) do '

Passing a block

'.html_safe end expect(ele.gsub(/\s/, '').downcase).to eql(RESPONSIVE_NAVBAR_WITH_BLOCK.gsub(/\s/, '').downcase) end it "should render contained items" do ele = nav_bar do menu_group do menu_item("Home", "/") + menu_item("Products", "/products") end end expect(ele.gsub(/\s/, '').downcase).to eql(PLAIN_NAVBAR_WITH_ITEM.gsub(/\s/, '').downcase) end it "should still render the brand name even with other options turned on" do ele = nav_bar(:brand => "Something") do menu_group do menu_item "Home", "/" end end expect(ele.gsub(/\s/, '').downcase).to eql(BRANDED_NAVBAR_WITH_ITEM.gsub(/\s/, '').downcase) end end describe "menu_group" do it "should return a ul with the class 'nav'" do ele = menu_group do menu_item("Home", "/") + menu_item("Products", "/products") end expect(ele).to eql '' end it "should return a ul with class .navbar-left when passed the {:pull => :left} option" do ele = menu_group(:pull => :left) do menu_item("Home", "/") end expect(ele).to eql('') end end describe "menu_item" do it "should return a link within an li tag" do allow(self).to receive(:current_page?) { false } expect(menu_item("Home", "/")).to eql('
  • Home
  • ') end it "should return the link with class 'active' if on current page" do allow(self).to receive(:uri_state) { :active } expect(menu_item("Home", "/")).to eql('
  • Home
  • ') end it "should pass any other options through to the link_to method" do allow(self).to receive_message_chain("uri_state").and_return(:active) expect(menu_item("Log out", "/users/sign_out", :class => "home_link", :method => :delete)).to eql('
  • Log out
  • ') end it "should pass a block but no name if a block is present" do allow(self).to receive(:current_page?) { false } expect(menu_item("/"){content_tag("i", "", :class => "icon-home") + " Home"}).to eql('
  • Home
  • ') end it "should work with just a block" do allow(self).to receive(:current_page?) { false } expect(menu_item{ content_tag("i", "", :class => "icon-home") + " Home" }).to eql('
  • Home
  • ') end it "should return the link with class 'active' if on current page with a block" do allow(self).to receive(:uri_state) { :active } expect(menu_item("/"){ content_tag("i", "", :class => "icon-home") + " Home" }).to eql('
  • Home
  • ') end end describe "drop_down" do it "should do render the proper drop down code" do ele = drop_down "Products" do menu_item "Latest", "/" end expect(ele).to have_tag(:li, with: {class: 'dropdown'}) expect(ele).to have_tag(:a, with: {class: 'dropdown-toggle'}) end end describe "drop_down_with_submenu" do it "should do render the proper drop down code" do ele = drop_down_with_submenu "Products" do drop_down_submenu "Latest" do menu_item "Option1", "/" end end expect(ele).to have_tag(:li, with: {class: 'dropdown'}) expect(ele).to have_tag(:a, with: {class: 'dropdown-toggle'}) expect(ele).to have_tag(:ul, with: {class: 'dropdown-menu'}) end end describe "drop_down_divider" do it "should render
  • " do expect(drop_down_divider).to match '
  • ' end end describe "drop_down_header" do it "should render
  • " do expect(drop_down_header('Home')).to match '' end end describe "menu_divider" do it "should render
  • " do expect(menu_divider).to match '
  • ' end end describe "menu_text" do it "should render text within p tags with class 'navbar-text" do expect(menu_text("Strapline!")).to match("

    Strapline!

    ") end it "should be able to be pulled right or left" do expect(menu_text("I am being pulled right", :pull => :right)).to match("

    I am being pulled right

    ") end it "should be able to cope with arbitrary options being passed to the p tag" do expect(menu_text("I am classy!", :class => "classy", :id => "classy_text")).to match("

    I am classy!

    ") end it "should be able to cope with a block too" do ele = menu_text("I have been rendered programmatically!") expect(ele).to have_tag(:p, with: {class: "navbar-text"}) expect(ele).to match "I have been rendered programmatically!" end end describe "rendering forms ok" do it "should not escape anything unexpectedly" do expect( nav_bar do form_tag "/", :method => 'get' do |f| f.text_field :search, "stub" end end ).to have_tag(:form) end end describe "default navbar" do it "renders a navbar" do expect(nav_bar { 'foo' }).to have_tag(:nav, with: { class: 'navbar navbar-default', role: 'navigation' }, text: /foo/) end end end # HTML output BASIC_NAVBAR = <<-HTML HTML FIXED_TOP_NAVBAR = <<-HTML HTML STATIC_TOP_NAVBAR = <<-HTML HTML FIXED_BOTTOM_NAVBAR = <<-HTML HTML INVERSE_NAVBAR = <<-HTML HTML NAVBAR_WITH_BRAND = <<-HTML HTML NAVBAR_WITH_BRAND_AND_LINK = <<-HTML HTML NAVBAR_WITH_BRAND_AND_LINK_TBLINK = <<-HTML HTML RESPONSIVE_NAVBAR = <<-HTML HTML RESPONSIVE_NAVBAR_WITH_BLOCK = <<-HTML HTML PLAIN_NAVBAR_WITH_ITEM = <<-HTML HTML BRANDED_NAVBAR_WITH_ITEM = <<-HTML HTML DROPDOWN_MENU = <<-HTML HTML DROPDOWN_MENU_WITH_SUBMENU = <<-HTML HTML PLAIN_NAVBAR_WITH_FORM = <<-HTML HTML