/*global describe, it, beforeEach, expect, spyOn, jasmine*/
/* eslint camelcase: 0, no-invalid-this: 0 */

const Challenge = require('../../../server/game/challenge.js');
const Player = require('../../../server/game/player.js');
const DrawCard = require('../../../server/game/drawcard.js');

describe('Challenge', function() {
    beforeEach(function() {
        this.gameSpy = jasmine.createSpyObj('game', ['on', 'raiseEvent']);
        this.attackingPlayer = new Player('1', 'Player 1', true, this.gameSpy);
        spyOn(this.attackingPlayer, 'winChallenge');
        spyOn(this.attackingPlayer, 'loseChallenge');
        this.defendingPlayer = new Player('2', 'Player 2', true, this.gameSpy);
        spyOn(this.defendingPlayer, 'winChallenge');
        spyOn(this.defendingPlayer, 'loseChallenge');

        this.attackerCard = new DrawCard(this.attackingPlayer, {});
        this.defenderCard = new DrawCard(this.defendingPlayer, {});

        this.challenge = new Challenge(this.gameSpy, this.attackingPlayer, this.defendingPlayer, 'military');
    });

    describe('determineWinner()', function() {
        describe('when the attacker has higher strength', function() {
            beforeEach(function() {
                spyOn(this.attackerCard, 'getStrength').and.returnValue(5);
                spyOn(this.defenderCard, 'getStrength').and.returnValue(4);
                this.challenge.addAttackers([this.attackerCard]);
                this.challenge.addDefenders([this.defenderCard]);
                this.challenge.determineWinner();
            });

            it('should have the attacking player be the winner', function() {
                expect(this.challenge.winner).toBe(this.attackingPlayer);
            });

            it('should mark the win for the attacking player', function() {
                expect(this.attackingPlayer.winChallenge).toHaveBeenCalledWith('military');
            });

            it('should have the defending player be the loser', function() {
                expect(this.challenge.loser).toBe(this.defendingPlayer);
            });

            it('should mark the loss for the defending player', function() {
                expect(this.defendingPlayer.loseChallenge).toHaveBeenCalledWith('military');
            });
        });


        describe('when the attacker and defender have equal strength', function() {
            beforeEach(function() {
                spyOn(this.attackerCard, 'getStrength').and.returnValue(5);
                spyOn(this.defenderCard, 'getStrength').and.returnValue(5);
                this.challenge.addAttackers([this.attackerCard]);
                this.challenge.addDefenders([this.defenderCard]);
                this.challenge.determineWinner();
            });

            it('should have the attacking player be the winner', function() {
                expect(this.challenge.winner).toBe(this.attackingPlayer);
            });

            it('should mark the win for the attacking player', function() {
                expect(this.attackingPlayer.winChallenge).toHaveBeenCalledWith('military');
            });

            it('should have the defending player be the loser', function() {
                expect(this.challenge.loser).toBe(this.defendingPlayer);
            });

            it('should mark the loss for the defending player', function() {
                expect(this.defendingPlayer.loseChallenge).toHaveBeenCalledWith('military');
            });
        });

        describe('when the defender has higher strength', function() {
            beforeEach(function() {
                spyOn(this.attackerCard, 'getStrength').and.returnValue(4);
                spyOn(this.defenderCard, 'getStrength').and.returnValue(5);
                this.challenge.addAttackers([this.attackerCard]);
                this.challenge.addDefenders([this.defenderCard]);
                this.challenge.determineWinner();
            });

            it('should have the defending player be the winner', function() {
                expect(this.challenge.winner).toBe(this.defendingPlayer);
            });

            it('should mark the win for the defending player', function() {
                expect(this.defendingPlayer.winChallenge).toHaveBeenCalledWith('military');
            });

            it('should have the attacking player be the loser', function() {
                expect(this.challenge.loser).toBe(this.attackingPlayer);
            });

            it('should mark the loss for the attacking player', function() {
                expect(this.attackingPlayer.loseChallenge).toHaveBeenCalledWith('military');
            });
        });
    });
});

@@ -1885,35 +1885,41 @@
 dWith('military'
+, true
 );%0A
-
             %7D);%0A
@@ -2245,32 +2245,39 @@
 dWith('military'
+, false
 );%0A            %7D
@@ -3090,32 +3090,38 @@
 dWith('military'
+, true
 );%0A            %7D
@@ -3450,32 +3450,39 @@
 dWith('military'
+, false
 );%0A            %7D
@@ -4281,32 +4281,39 @@
 dWith('military'
+, false
 );%0A            %7D
@@ -4552,32 +4552,32 @@
 ', function() %7B%0A
-
                 
@@ -4646,24 +4646,30 @@
 h('military'
+, true
 );%0A         

fix tests