require 'rails_helper'

describe 'Sign in User', type: :feature do
  scenario 'when company is not created' do
    visit sign_in_path

    expect(current_path).to eq(setup_company_index_path)
  end

  scenario 'when company has been recorded on the database' do
    company = create(:company)

    visit sign_in_path

    expect(current_path).to eq(setup_account_index_path)
  end

  scenario 'when user has been recorded on the database', js: true do
    company = create(:company)
    user    = create(:user, company: company)

    visit root_path
    click_link 'Get Started'

    expect(current_path).to eq(sign_in_path)
    expect(page).to have_content 'Sign In'
  end

  scenario 'when user sign in successfully', js: true do
    company = create(:company)
    user    = create(:user, company: company)
    profile = create(:profile, user: user)

    visit sign_in_path

    expect(current_path).to eq(sign_in_path)
    find('#email').set user.email
    find('#password').set user.password
    click_on 'Sign in'

    page.has_content? 'Dashboard'

    expect(page).to have_content 'Successfully signed in.'
  end

  scenario 'when user failed to login', js: true do
    company = create(:company)
    user    = create(:user, company: company)

    visit sign_in_path

    expect(current_path).to eq(sign_in_path)
    find('#email').set 'xyz'
    find('#password').set 'bar'
    click_on 'Sign in'

    expect(page).to have_content 'Email or password did not matched.'
  end
end
