require 'spec_helper'

describe "affiliate visit tracking", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  let(:partner) { FactoryGirl.create(:affiliate_partner) }
  let(:affiliate_params) { partner.affiliate_query_params }

  before(:each) do
    AffiliateReferralVisit.delete_all
  end

  it "tracks" do
    visit '/?' + affiliate_params

    should_be_at_root
    AffiliateReferralVisit.count.should eq(1)
    visit = AffiliateReferralVisit.first
    visit.visited_url.should eq('/?' + affiliate_params)
    visit.affiliate_partner_id.should eq(partner.id)
    visit.first_visit.should be true

    download_url = '/downloads?' + affiliate_params
    visit download_url
    find('h2.create-account-header')


    AffiliateReferralVisit.count.should eq(2)
    visit = AffiliateReferralVisit.find_by_visited_url(download_url)
    visit.affiliate_partner_id.should eq(partner.id)
    visit.first_visit.should be false
  end

end
