require 'rails_helper'

RSpec.describe Dunlop::ActiveClassHelper, type: :helper do
  subject { helper }

  describe '#active_class' do
    describe 'normal routes' do
      before do
        allow( subject ).to receive(:controller_path).and_return 'admin/users'
        allow( subject ).to receive(:action_name).and_return 'index'
      end

      it 'returns active on match with full spec' do
        expect( subject.active_class 'admin/users#index' ).to eq 'active'
      end

      it 'returns active on match with controller only spec' do
        expect( subject.active_class 'admin/users' ).to eq 'active'
      end

      it 'returns active when only the first part of the controller is requested' do
        expect( subject.active_class 'admin' ).to eq 'active'
      end

      it 'returns active on match with action only spec' do
        expect( subject.active_class '#index' ).to eq 'active'
      end

      it 'returns nil on match with incorrect full spec' do
        expect( subject.active_class 'admin/users#show' ).to be nil
      end

      it 'returns active on match with incorrect controller only spec' do
        expect( subject.active_class 'pages' ).to be nil
      end

      it 'returns active on match with incorrect action only spec' do
        expect( subject.active_class '#show' ).to be nil
      end

      it 'returns nil for a different controller with the same action with full spec' do
        expect( subject.active_class 'pages#index' ).to be nil
      end
    end

    describe "regular expression arguments" do
      it "works with regular expressions" do
        allow( subject ).to receive(:controller_path).and_return 'admin/users'
        allow( subject ).to receive(:action_name).and_return 'index'
        subject.active_class(/#index/).should             eq 'active'
        subject.active_class(/admin\/users#index/).should eq 'active'
        subject.active_class(/^admin.*#index$/).should    eq 'active'
        subject.active_class(/#other|#index/).should      eq 'active'

        subject.active_class(/#other|#yet_another/).should be nil
        subject.active_class(/admin\/users#show/).should   be nil
      end
    end

    describe "except option" do
      before do
        allow( subject ).to receive(:controller_path).and_return 'admin/users'
        allow( subject ).to receive(:action_name).and_return 'index'
      end

      it "works with single statement" do
        subject.active_class('admin/users', except: '#index').should be nil
        subject.active_class('admin/users', except: '#show').should  eq 'active'
      end

      it "works with regexp" do
        subject.active_class('admin/users', except: /#index$/).should be nil
        subject.active_class('admin/users', except: /#show$/).should  eq 'active'
        subject.active_class('admin/users', except: /#show|#index/).should be nil
      end

      it "works with array spec" do
        subject.active_class('admin/users', except: ['admin']).should  be nil
        subject.active_class('admin/users', except: ['#index']).should be nil
        subject.active_class('admin/users', except: ['bogus', /users/]).should be nil # users matches
      end
    end

    describe 'HighVoltage' do
      before do
        allow( subject ).to receive(:controller_path).and_return 'high_voltage/pages'
        allow( subject ).to receive(:action_name).and_return 'show'
      end

      it "works with high_voltage when there is a match" do
        expect( subject ).to receive(:params).and_return(id: 'changelog')
        expect( subject.active_class 'pages#changelog' ).to eq 'active'
      end

      it "works with high_voltage when there is no match" do
        expect( subject ).to receive(:params).and_return(id: 'about')
        expect( subject.active_class 'pages#changelog' ).to be nil
      end
    end
  end
end
