# Feature: Table show
#   As a visitor
#   I want to view a table
#   So I can sit and play a game
feature 'Table show', :devise do
  context "As a visitor" do
    before(:each) do
      @table = FactoryGirl.create :table
      visit tables_path
      click_link @table.title
    end

    scenario 'visitor can sit at the table', js: true do
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
    end

    scenario 'visitor can join the current game', js: true do
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.text.scan('Guest').count).to eq 1
    end

    scenario 'visitor can leave the current game', js: true do
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.text.scan('Guest').count).to eq 1
      click_button 'Stand', match: :first
      expect(page.find('.game-seat.position-1')).to have_content "Sit"
      expect(page.text.scan('Guest').count).to eq 0
    end

  end

  context "As a user" do
    before(:each) do
      @table = FactoryGirl.create :table
      @user = FactoryGirl.create :user
      signin(@user.email, @user.password)
      visit table_path @table
    end

    scenario 'user can sit at the table', js: true do
      #TODO: Fix race condition
      expect(page).to have_content "Sit"
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
    end

    scenario 'user can join the current game', js: true do
      expect(page).to have_content "Sit"
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
      expect(page.text.scan(@user.email).count).to eq 1
    end

    scenario 'user can leave the current game', js: true do
      expect(page).to have_content "Sit"
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
      expect(page.text.scan(@user.email).count).to eq 1
      click_button 'Stand', match: :first
      expect(page.find('.game-seat.position-1')).to have_content "Sit"
      expect(page.text.scan(@user.email).count).to eq 0
    end

  end
end
