require 'spec_helper'

describe 'create mentoring session' do
  let(:marketplace) {get_marketplace}

  it 'should create mentoring session with valid values', js: true do
    mentor = create :mentor, availability_defaults: "{\"0\":[\"25200\", \"26100\"],\"1\":[],\"2\":[],\"3\":[],\"4\":[],\"5\":[],\"6\":[]}"
    customer = create :customer
    prev_balance = marketplace.in_escrow
    customer.mentors<< mentor
    customer.save
    login_as_user customer

    click_link 'Mentor'
    click_link 'Find a mentor'
    click_link "show_mentor_#{mentor.id}"
    
    click_link 'request_mentoring_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: 'mentoring_session_length'
    find(:xpath, "//div[@data-block='25200']").click

    disabled = page.evaluate_script("$('#new_mentoring_session #submit').attr('disabled');")
    disabled.should == nil


    click_button 'submit'

    sleep(15)

    page.should have_content 'Mentoring session created'

    inc = ((((50 + 0.30) * 1.029).round(2)) - 50) + 5.25
    marketplace.reload
    marketplace.in_escrow.should eq (prev_balance + (inc * 100))


    mentoring = MentoringSession.last
    mentoring.date.should eq date
    mentoring.length.should eq 30
    mentoring.start_time.should eq 25200
    mentoring.mentor.should eq mentor
    mentoring.customer.should eq customer
  end


  it 'should show next month when click', js: true do
    mentor = create :mentor, availability_defaults: "{\"0\":[\"25200\", \"26100\"],\"1\":[],\"2\":[],\"3\":[],\"4\":[],\"5\":[],\"6\":[]}"
    customer = create :customer
    customer.mentors<< mentor
    customer.save
    login_as_user customer

    click_link 'Mentor'
    click_link 'Find a mentor'
    click_link "show_mentor_#{mentor.id}"

    click_link 'request_mentoring_session_button'


    # Look for next sunday because there are a validation
    # for dates and it shold be in the future
    desired_date = Date.today.next_month
    date = next_sunday(desired_date)
    sleep(2)

    click_link 'calendar_next_month'

    # If next_sunday is in next month, click on the calendar
    if date.month > desired_date.month
      click_link 'calendar_next_month'
    end
    sleep(2)

    find(:xpath, "//a[@data-date='#{date.to_s}']").click
    select '30', from: 'mentoring_session_length'
    sleep 1
    find(:xpath, "//div[@data-block='25200']").click

    click_button 'submit'

    page.should have_content 'Mentoring session created'
  end


  it 'should fail with invalid length', js: true do
    mentor = create :mentor, availability_defaults: "{\"0\":[\"25200\", \"26100\"],\"1\":[],\"2\":[],\"3\":[],\"4\":[],\"5\":[],\"6\":[]}"
    customer = create :customer
    customer.mentors<< mentor
    customer.save
    login_as_user customer

    click_link 'Mentor'
    click_link 'Find a mentor'
    click_link "show_mentor_#{mentor.id}"

    click_link 'request_mentoring_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 '60', from: 'mentoring_session_length'
    sleep 1
    find(:xpath, "//div[@data-block='25200']").click
    click_button 'submit'

    page.should have_content "Length isn't available"
  end
end
