require 'spec_helper'

describe "Fixxpert Rewards" do
	let!(:fixxpert) { FactoryGirl.create :fixxpert }

	describe "Reward for invite" do
		let(:invite) {create :invite, inviter: fixxpert, invitee_email: "sample+1@email.com"}

		before do

			puts fixxpert.persisted?

			visit signup_path({:id => invite.id})

			fill_in "user_name", with: "Sample Name"
			fill_in "user_password", with: "admin" 
			fill_in "user_password_confirmation", with: "admin"
			choose 'Customer'

			click_button "Create Account"

			invite.reload
			fixxpert.reload
		end

		it "Fixxpert should get reward" do
			fixxpert.points.should eq 5
		end

		it "Fixxpert should be linked to the invite" do
			invite.invitee.should eq User.last
		end

		describe "Reward for first expert session" do

			let(:invited_customer) { User.last }

			before do
				visit '/users/'+invited_customer.activation_token+'/activate'

				login_as_user invited_customer

		    click_link 'Fixxpert'
		    click_link 'Find a fixxpert'
		    click_link "show_fixxpert_#{fixxpert.id}"
		    
		    click_link 'request_expert_session_button'

		    # Look for next sunday because there are a validation
		    # for dates and it shold be in the future
		    date = next_sunday


		    # If next_sunday is in next month, click on the calendar
		    if date.month > Date.today.month
		      click_link 'calendar_next_month'
		    end

		    sleep(2)
		    find(:xpath, "//a[@data-date='#{date.to_s}']").click
		    select '30', from: 'expert_session_length'
		    find(:xpath, "//div[@data-block='25200']").click
		    click_button 'submit'
		    sleep 4
		    
	  		fixxpert.reload
	  	end

	  	it "Inviter should get points", :js => true do
	  		fixxpert.points.should eq 45
	  	end

		end
	end
end