require 'spec_helper'

describe 'create expert session' do
  it 'should create expert session with valid values', js: true do
    fixxpert = create :fixxpert, availability_defaults: "{\"0\":[\"25200\", \"26100\"],\"1\":[],\"2\":[],\"3\":[],\"4\":[],\"5\":[],\"6\":[]}"
    customer = create :customer
    login_as_user 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

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


    click_button 'submit'

    page.should have_content 'Expert session created'

    expert = ExpertSession.last
    expert.date.should eq date
    expert.length.should eq 30
    expert.start_time.should eq 25200
    expert.fixxpert.should eq fixxpert
    expert.customer.should eq customer
  end


  it 'should show next month when click', js: true do
    fixxpert = create :fixxpert, availability_defaults: "{\"0\":[\"25200\", \"26100\"],\"1\":[],\"2\":[],\"3\":[],\"4\":[],\"5\":[],\"6\":[]}"
    customer = create :customer
    login_as_user 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
    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: 'expert_session_length'
    sleep 1
    find(:xpath, "//div[@data-block='25200']").click

    click_button 'submit'

    page.should have_content 'Expert session created'
  end


  it 'should fail with invalid length', js: true do
    fixxpert = create :fixxpert, availability_defaults: "{\"0\":[\"25200\", \"26100\"],\"1\":[],\"2\":[],\"3\":[],\"4\":[],\"5\":[],\"6\":[]}"
    customer = create :customer
    login_as_user 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 '60', from: 'expert_session_length'
    sleep 1
    find(:xpath, "//div[@data-block='25200']").click
    click_button 'submit'

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