require 'spec_helper'

describe 'Stutdent create a review' do

  it 'should not view the new review form if no expert sessions for this fixxpert' do
    customer = create :customer
    fixxpert = create :fixxpert

    login_as_user customer
    click_link 'Fixxpert'
    click_link 'Find a fixxpert'
    click_link "show_fixxpert_#{fixxpert.id}"
    page.should have_no_css '#new_review'
  end

  it 'should not view the new review form if already post a review for this fixxpert' do
    customer = create :customer
    fixxpert = create :fixxpert
    subtrade = create :subtrade
    expert_session = create :expert_session, fixxpert: fixxpert, customer: customer, status: 'confirmed'
    review = create :review, user: customer, for_user: fixxpert, subtrade: subtrade
    login_as_user customer
    click_link 'Fixxpert'
    click_link 'Find a fixxpert'
    click_link "show_fixxpert_#{fixxpert.id}"
    page.should have_no_css '#new_review'
  end



  it 'should not create a review with empty text' do
    customer = create :customer
    fixxpert = create :fixxpert
    expert_session = create :expert_session, fixxpert: fixxpert, customer: customer, status: 'confirmed'

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

    within '#new_review' do
      click_button 'submit'
    end
    page.should have_content "Can't create the review"

    within '#customer_review' do
      page.should_not have_content 'New review'
    end
  end

  it 'should create a review with valid text' do
    customer = create :customer
    fixxpert = create :fixxpert
    subtrade = create :subtrade
    expert_session = create :expert_session, fixxpert: fixxpert, customer: customer, status: 'confirmed'
    login_as_user customer
    click_link 'Fixxpert'
    click_link 'Find a fixxpert'
    click_link "show_fixxpert_#{fixxpert.id}"

    within '#new_review' do
      fill_in 'review_subtrade', with: subtrade.name
      fill_in 'review_text', with: 'New review'
      click_button 'submit'
    end

    within '#customer_review' do
      page.should have_content 'New review'
    end

    customer.reload

    expect{customer.credits.should eq 2}
  end

   it 'should create a review and reward 2 credits to customer' do
    customer = create :customer
    fixxpert = create :fixxpert
    subtrade = create :subtrade
    expert_session = create :expert_session, fixxpert: fixxpert, customer: customer, status: 'confirmed'
    login_as_user customer
    click_link 'Fixxpert'
    click_link 'Find a fixxpert'
    click_link "show_fixxpert_#{fixxpert.id}"

    within '#new_review' do
      fill_in 'review_subtrade', with: subtrade.name
      fill_in 'review_text', with: 'New review'
      click_button 'submit'
    end

    customer.reload

    expect{customer.credits.should eq 2}
  end

  it 'should create a review and calculate the level of fixxpert' do
    customer = create :customer
    fixxpert = create :fixxpert
    fixxpert.billed_length = 720
    fixxpert.save
    subtrade = create :subtrade
    expert_session = create :expert_session, fixxpert: fixxpert, customer: customer, status: 'confirmed'
    login_as_user customer
    click_link 'Fixxpert'
    click_link 'Find a fixxpert'
    click_link "show_fixxpert_#{fixxpert.id}"

    within '#new_review' do
      fill_in 'review_subtrade', with: subtrade.name
      fill_in 'review_text', with: 'New review'
      click_button 'submit'
    end

    fixxpert.reload

    expect { fixxpert.level.should eq 2 }
  end

end
