require 'spec_helper'

describe "Home Screen", :js => true, :type => :feature, :capybara_feature => true  do

  subject { page }

  before(:all) do
    ActiveMusicSession.delete_all
  end

  let(:user) { FactoryGirl.create(:user) }


  shared_examples_for :has_footer do
    it "should have footer elements" do
      should have_selector('#footer-links')
      find('#footer-links').should have_content('about')
      find('#footer-links').should have_content('news')
      find('#footer-links').should have_content('media')
      find('#footer-links').should have_content('contact')
      find('#footer-links').should have_content('privacy')
      find('#footer-links').should have_content('terms of service')
      find('#footer-links').should have_content('help')
    end
  end

  shared_examples_for :go_home do
    it "can go back to home" do
      find('a.home-icon').trigger(:click)
      find('h2', text: 'account')
    end
  end

  shared_examples_for :create_session_homecard do
    describe 'Create Session' do
      before(:each) do
        find('h2', text: 'create session').trigger(:click)
      end

      it { should have_selector('h1',    text: 'create session') }
      it_behaves_like :go_home
    end
  end

  shared_examples_for :find_session_homecard do
    describe 'Find Session' do
      before(:each) do
        find('h2', text: 'find session').trigger(:click)
      end

      it { should have_selector('h1',    text: 'find a session') }
      it_behaves_like :go_home
    end
  end

  shared_examples_for :feed_homecard do
    describe 'Feed' do
      before(:each) do
        find('h2', text: 'feed').trigger(:click)
      end

      it { should have_selector('h1',    text: 'feed') }
      it_behaves_like :go_home
    end
  end

  shared_examples_for :musicians_homecard do
    describe 'Musicians' do
      before(:each) do
        find('h2', text: 'musicians').trigger(:click)
      end

      it { should have_selector('h1',    text: 'musicians') }
      it_behaves_like :go_home
    end
  end

  shared_examples_for :bands_homecard do
    describe 'Bands' do
      before(:each) do
        find('h2', text: 'bands').trigger(:click)
      end

      it { should have_selector('h1',    text: 'bands') }
      it_behaves_like :go_home
    end
  end

  shared_examples_for :profile_homecard do
    describe 'Profile' do
      before(:each) do
        find('h2', text: 'profile').trigger(:click)
      end

      it { should have_selector('h1',    text: 'musician profile') }
      it_behaves_like :go_home
    end
  end

  shared_examples_for :account_homecard do
    describe 'Account' do
      before(:each) do
        find('h2', text: 'account').trigger(:click)
      end

      it { should have_selector('h1',    text: 'my account') }
      it_behaves_like :go_home
    end
  end

  describe 'Home Screen while in Native Client' do
    before(:each) do
      UserMailer.deliveries.clear
      emulate_client
      fast_signin(user, "/client")
    end

    it_behaves_like :has_footer
    it_behaves_like :create_session_homecard
    it_behaves_like :find_session_homecard
    it_behaves_like :feed_homecard
    it_behaves_like :musicians_homecard
    it_behaves_like :bands_homecard
    it_behaves_like :account_homecard
    it_behaves_like :profile_homecard
  end

  describe 'Home Screen while in Browser' do
    before(:each) do
      UserMailer.deliveries.clear
      page.driver.headers = { 'User-Agent' => 'Firefox' }
      fast_signin(user, "/client")
    end

    it_behaves_like :has_footer
    it_behaves_like :create_session_homecard
    it_behaves_like :find_session_homecard
    it_behaves_like :feed_homecard
    it_behaves_like :musicians_homecard
    it_behaves_like :bands_homecard
    it_behaves_like :account_homecard
    it_behaves_like :profile_homecard
  end


end
