require 'spec_helper'

describe "JamTrack Landing", :js => true, :type => :feature, :capybara_feature => true do

  let(:user) { FactoryGirl.create(:user, has_redeemable_jamtrack: true) }
  let(:jt_us) { FactoryGirl.create(:jam_track, :name=>'jt_us', sales_region: 'United States', make_track: true, original_artist: "foobar") }
  let(:jt_ww) { FactoryGirl.create(:jam_track, :name=>'jt_ww', sales_region: 'Worldwide', make_track: true, original_artist: "barfoo") }
  let(:jt_rock) { FactoryGirl.create(:jam_track, :name=>'jt_rock', genres: [JamRuby::Genre.find('avante-garde')], make_track: true, original_artist: "badfood") }
  let(:jt_blues) { FactoryGirl.create(:jam_track, :name=>'jt_blues', genres: [JamRuby::Genre.find('blues')], make_track: true, original_artist: "foodbart") }

  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.default_max_wait_time = 30 # these tests are SLOOOOOW
  end


  before(:each) do
    ShoppingCart.delete_all
    JamTrackRight.delete_all
    JamTrack.delete_all
    JamTrackTrack.delete_all
    JamTrackLicensor.delete_all


    stub_const("APP_CONFIG", web_config)
  end

  it "not logged in" do
    jt_rock.touch
    jt_rock.reload
    jt_blues.touch
    jt_blues.reload

    visit '/client#/jamtrack'

    find('h2', text: 'what are jamtracks?')
    if web_config.one_free_jamtrack_per_user
      find('.free-jamtrack')
    else
      find('.no-free-jamtrack')
    end

    # and go ahead and try out some searches
    jk_select(jt_rock.genres[0].description, '#jamtrackLanding .genre-list')
    jk_select(jt_rock.jam_track_tracks[0].instrument.description, '#jamtrackLanding .instrument-list')
    find('.search-by-filter-btn').trigger(:click)
    # should have transitioned to the filter screen
    find('.JamTrackFilterScreen')
    # and the search should have been kicked off and already showing our track
    find('.jamtrack-record[data-jamtrack-id="' + jt_rock.id + '"]')
  end

  it "logged in and has redeemable track" do
    jt_us.touch
    jt_ww.touch

    fast_signin(user, '/client#/jamtrack')

    find('h2', text: 'what are jamtracks?')
    find('.free-jamtrack')
  end

  it "logged in and does not have redeemable track" do
    jt_us.touch
    jt_ww.touch
    user.has_redeemable_jamtrack = false
    user.save!

    fast_signin(user, '/client#/jamtrack')

    find('h2', text: 'what are jamtracks?')
    find('.no-free-jamtrack')
  end
end
