require 'rails_helper'
include Warden::Test::Helpers
Warden.test_mode!

feature 'Join a game', type: :feature, js: true do
#  before(:each) do
#    @game = setup_game
#    @game.reload
#    @player = @game.controlling_player
#    signin(@player.user.email,'please123')
#    #login_as(@player.user, scope: :user)
#    visit game_path @game
#  end

  after(:each) do
    Warden.test_reset!
  end

  # Scenario: The user joins a game
  #   Given: A joinable game and I am a signed in user
  #   When I click the join game button
  #   Then I see myself in the game
  scenario 'user can join a game' do
    @game = FactoryGirl.create :game
    @user = FactoryGirl.create :user
    login_as(@user, scope: :user)
    visit game_path @game
    click_button "Sit", match: :first
    expect(page).to have_content @user.display_name
  end

  # Scenario: The user is unable to join a game if they are already joined
  #   Given: A game I am currently in that is otherwise joinable
  #   When I visit the game path
  #   Then I do not see a Join button
  scenario 'user can not join a game twice' do
    @game = FactoryGirl.create :game
    @user = FactoryGirl.create :user
    login_as(@user, scope: :user)
    visit game_path @game
    click_button "Sit", match: :first
    expect(page).to have_content @user.display_name
    click_button "Sit", match: :first
    expect(page).to have_content @user.display_name
    expect(page.text.scan(@user.display_name).count).to eq 1
  end
end
