|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "Opponent", type: :request do |
| 4 | + describe 'POST create' do |
| 5 | + let(:game) { Game.create!(name: 'Starship Battle', slug: 'starship-battle', state: 'awaiting_opponent') } |
| 6 | + |
| 7 | + before :each do |
| 8 | + game.players.create! |
| 9 | + end |
| 10 | + |
| 11 | + context 'when game is not full' do |
| 12 | + before :each do |
| 13 | + allow(ActionCable.server).to receive(:broadcast) |
| 14 | + post "/games/starship-battle/opponent" |
| 15 | + end |
| 16 | + |
| 17 | + it { expect(response).to redirect_to("/games/starship-battle") } |
| 18 | + it { expect(Player.count).to eq(2) } |
| 19 | + it { expect(Player.last.game).to eq(game) } |
| 20 | + it { expect(Player.last.life).to eq(10) } |
| 21 | + |
| 22 | + it 'sets game state to running' do |
| 23 | + expect(game.reload.state).to eq('running') |
| 24 | + end |
| 25 | + |
| 26 | + it 'broadcasts a player joined payload' do |
| 27 | + expect(ActionCable.server).to have_received(:broadcast).with( |
| 28 | + anything, |
| 29 | + { |
| 30 | + code: 'player_joined', |
| 31 | + player_id: Player.last.id, |
| 32 | + nickname: Player.last.nickname |
| 33 | + } |
| 34 | + ) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when game is full' do |
| 39 | + before :each do |
| 40 | + game.update(state: 'running') |
| 41 | + game.players.create! |
| 42 | + post "/games/starship-battle/opponent" |
| 43 | + end |
| 44 | + |
| 45 | + it { expect(response).to redirect_to("/games/starship-battle") } |
| 46 | + it { expect(Player.count).to eq(2) } |
| 47 | + it { expect(Game.last.state).to eq('running') } |
| 48 | + |
| 49 | + it 'tells game is full' do |
| 50 | + follow_redirect! |
| 51 | + expect(response.body).to include('already full') |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when game is already finished' do |
| 56 | + before :each do |
| 57 | + game.update(state: 'finished') |
| 58 | + post "/games/starship-battle/opponent" |
| 59 | + end |
| 60 | + |
| 61 | + it { expect(response).to redirect_to("/games/starship-battle") } |
| 62 | + it { expect(Player.count).to eq(1) } |
| 63 | + it { expect(Game.last.state).to eq('finished') } |
| 64 | + |
| 65 | + it 'tells game is finished' do |
| 66 | + follow_redirect! |
| 67 | + expect(response.body).to include('Game Finished') |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context "game doesn't exist" do |
| 72 | + before :each do |
| 73 | + post "/games/foo/opponent" |
| 74 | + end |
| 75 | + |
| 76 | + it { expect(response).to redirect_to(root_path) } |
| 77 | + it { expect(Player.count).to eq(1) } |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments