require 'spec_helper' include ActionView::Helpers::UrlHelper class TrejoHelperTestClass; end describe Trejo::ViewHelpers do before do @trejo = TrejoHelperTestClass.new @trejo.extend Trejo::ViewHelpers end describe '#nav_item' do context 'by default' do context 'when the link path matches the requested path' do before do request = stub fullpath: '/home' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Home', '/home') end it 'returns a selected nav item' do expect(@nav_item).to eq('Home') end end context 'when the link path does not match the requested path' do before do request = stub fullpath: '/home' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Blog', '/blog') end it 'returns an unselected nav item' do expect(@nav_item).to eq('Blog') end end context 'ignores query parameters' do context 'and when the link path matches the requested path' do before do request = stub fullpath: '/home?page=2' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Home', '/home') end it 'returns a selected nav item' do expect(@nav_item).to eq('Home') end end end context 'assumes the link path is the root' do context 'and when the requested path includes a nested path' do before do request = stub fullpath: '/home/index' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Home', '/home') end it 'returns a selected nav item' do expect(@nav_item).to eq('Home') end end context 'and when the requested path includes a different root path' do before do request = stub fullpath: '/blog/index' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Home', '/home') end it 'returns an unselected nav item' do expect(@nav_item).to eq('Home') end end context 'and when query parameters are present' do before do request = stub fullpath: '/home/index?foo=bar&walter=sobchak' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Home', '/home') end it 'returns a selected nav item' do expect(@nav_item).to eq('Home') end end end end context 'when the selection criteria is a regex' do before do request = stub fullpath: '/home?page=2' @trejo.stub(:request).and_return(request) end context 'when the requested path matches the criteria' do before do @nav_item = @trejo.nav_item('Home', '/home', selected: /^\/home/) end it 'returns a selected nav item' do expect(@nav_item).to eq('Home') end end context 'when the requested path does not match the criteria' do before do @nav_item = @trejo.nav_item('Blog', '/blog', selected: /^\/blog/) end it 'returns a unselected nav item' do expect(@nav_item).to eq('Blog') end end end context 'when a css class is supplied' do before do request = stub fullpath: '/home' @trejo.stub(:request).and_return(request) @nav_item = @trejo.nav_item('Home', '/home', class: 'current-section') end it 'returns a selected nav item with that class' do expect(@nav_item).to eq('Home') end end end end