Skip to content

Commit

Permalink
Finished unit tests but feature test still failing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam authored and Sam committed Jul 9, 2017
1 parent fb8671b commit edf42fd
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 42 deletions.
116 changes: 102 additions & 14 deletions spec/units/FrameSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@ describe('Frame', function () {
expect(frame.checkFullFrame).toHaveBeenCalled();
});
it('calls updateFrameScore and passes roll() if rollcount is less than 2 and remainder greater than 0', function () {
spyOn(frame, 'getRollCount');
frame.getRollCount.and.returnValue(1);
spyOn(frame, 'remainder');
frame.remainder.and.returnValue(10);
spyOn(frame, 'getRollCount').and.returnValue(1);
spyOn(frame, 'remainder').and.returnValue(10);
spyOn(Math, 'random').and.returnValue(0.51);
spyOn(frame, 'updateFrameScore');
frame.processRoll();
expect(frame.updateFrameScore).toHaveBeenCalledWith(5);
});
it('calls updateFrameScore and passes 0 if rollcount is less than 2 but remainder is 0', function () {
spyOn(frame, 'getRollCount');
frame.getRollCount.and.returnValue(1);
spyOn(frame, 'remainder');
frame.remainder.and.returnValue(0);
spyOn(frame, 'getRollCount').and.returnValue(1);
spyOn(frame, 'remainder').and.returnValue(0);
spyOn(frame, 'updateFrameScore');
frame.processRoll();
expect(frame.updateFrameScore).toHaveBeenCalledWith(0);
Expand All @@ -52,16 +48,108 @@ describe('Frame', function () {

describe('remainder', function () {
it('defaults to return 10 if no roll has been taken', function () {
spyOn(frame, 'getRollCount');
frame.getRollCount.and.returnValue(0);
spyOn(frame, 'getRollCount').and.returnValue(0);
expect(frame.remainder()).toEqual(10);
});
it('returns points left after a roll has been taken', function () {
spyOn(frame, 'getRollCount');
frame.getRollCount.and.returnValue(1);
spyOn(frame, 'getFrameScore');
frame.getFrameScore.and.returnValue([4]);
spyOn(frame, 'getRollCount').and.returnValue(1);
spyOn(frame, 'getFrameScore').and.returnValue([4]);
expect(frame.remainder()).toEqual(6);
});
});

describe('updateFrameScore', function () {
it('throws error if trying to update frame that already has two scores', function () {
spyOn(frame, 'getFrameScore').and.returnValue([2, 4]);
expect(function () { frame.updateFrameScore(4); }).toThrowError();
});
it('throws error if passed a score that is not integer', function () {
spyOn(frame, 'getFrameScore').and.returnValue([2]);
expect(function () { frame.updateFrameScore(11); }).toThrowError(TypeError);
});
it('throws error if not passed a parameter', function () {
spyOn(frame, 'getFrameScore').and.returnValue([2]);
expect(function () { frame.updateFrameScore(); }).toThrowError(TypeError);
});
it('calls updateRollCount at the end', function () {
spyOn(frame, 'updateRollCount');
frame.updateFrameScore(2);
expect(frame.updateRollCount).toHaveBeenCalled();
});
it('pushes new score to frameScore', function () {
frame.updateFrameScore(2);
expect(frame.getFrameScore()).toEqual([2]);
});
});

describe('roll', function () {
it('returns random number calculated from remainder', function () {
spyOn(frame, 'remainder').and.returnValue(10);
spyOn(Math, 'random').and.returnValue(0.51);
expect(frame.roll()).toEqual(5);
});
});

describe('updateRollCount', function () {
it('increments rollCount once', function () {
frame.rollCount = 1;
frame.updateRollCount();
expect(frame.rollCount).toEqual(2);
});
});

describe('checkFullFrame', function () {
it('calls updateIsFinished if rollCount is > 1', function () {
spyOn(frame, 'getRollCount').and.returnValue(2);
spyOn(frame, 'updateIsFinished');
frame.checkFullFrame();
expect(frame.updateIsFinished).toHaveBeenCalledWith(true);
});
it('does not call updateIsFinished if rollCount is < 2', function () {
spyOn(frame, 'getRollCount').and.returnValue(1);
spyOn(frame, 'updateIsFinished');
frame.checkFullFrame();
expect(frame.updateIsFinished).not.toHaveBeenCalled();
});
});

describe('getIsFinished', function () {
it('returns false on initialisation', function () {
expect(frame.getIsFinished()).toBe(false);
});
});

describe('updateIsFinished', function () {
it('assigns argument to isFinished', function () {
frame.isFinished = false;
frame.updateIsFinished(true);
expect(frame.isFinished).toBe(true);
});
});

describe('getScoreCard', function () {
it('returns scoreCard property', function () {
frame.scoreCard = 'card';
expect(frame.getScoreCard()).toEqual('card');
});
});

describe('updateScoreCard', function () {
it('pushes latest frameScore to scoreCard', function () {
var card = { updateCard: function (score) {} };
spyOn(frame, 'getScoreCard').and.returnValue(card);
spyOn(frame, 'getFrameScore').and.returnValue([5]);
spyOn(card, 'updateCard');
frame.updateScoreCard();
expect(frame.getScoreCard).toHaveBeenCalled();
expect(card.updateCard).toHaveBeenCalledWith([5]);
});
});

describe('getFrameScore', function () {
it('returns frameScore property', function () {
frame.frameScore = [5, 3];
expect(frame.getFrameScore()).toEqual([5, 3]);
});
});
});
8 changes: 8 additions & 0 deletions spec/units/ScoreCardSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('ScoreCard', function () {
});
});

describe('isFullFrame', function () {
it('returns true if 2 items in last array of card', function () {
spyOn(card, 'getLastFrame').and.returnValue([1, 3]);
expect(card.isFullFrame()).toBe(true);
expect(card.getLastFrame).toHaveBeenCalled();
});
});

describe('getLastFrame', function () {
it('returns last item in this.card as an array', function () {
card.card = [[1, 1], [5, 0]];
Expand Down
29 changes: 16 additions & 13 deletions src/Frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ Frame.prototype = {

updateFrameScore: function (rollScore) {
if (this.getFrameScore().length > 1) {
throw Error('Game has tried to play a full frame');
throw Error('Game has tried to play another roll on a full frame');
} else if (rollScore < 0 || rollScore > 10 || rollScore === undefined) {
throw TypeError('not passed a valid number - score is ' + rollScore);
}
// else if (!(rollScore instanceof 'Integer'){
// throw TypeError('not passed a number');
// }
this.frameScore.push(rollScore);
this.updateRollCount();
},
Expand All @@ -49,7 +48,7 @@ Frame.prototype = {
},

checkFullFrame: function () {
if (this.getRollCount() === 2) {
if (this.getRollCount() > 1) {
this.updateIsFinished(true);
}
},
Expand All @@ -62,19 +61,23 @@ Frame.prototype = {
this.isFinished = boolean;
},

getScoreCard: function () {
return this.scoreCard;
},

updateScoreCard: function () {
this.getScoreCard().updateCard(this.getFrameScore());
},

getFrameScore: function () {
return this.frameScore;
},

getLastFrameScore: function () {
var lastScore = this.getFrameScore()[this.getFameScore().length - 1];
if (lastScore === 'undefined') {
throw Error('Tried to update scorecard with no score in frame');
}
return lastScore;
}

// getLastFrameScore: function () {
// var lastScore = this.getFrameScore()[this.getFameScore().length - 1];
// if (lastScore === 'undefined') {
// throw Error('Tried to update scorecard with no score in frame');
// }
// return lastScore;
// }
};
32 changes: 17 additions & 15 deletions src/ScoreCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ function ScoreCard () {
this.card = [[]];
}

ScoreCard.prototype.getCard = function () {
return this.card;
};
ScoreCard.prototype = {
getCard: function () {
return this.card;
},

ScoreCard.prototype.updateCard = function (rollScore) {
if (this.isFullFrame()) {
this.card.push([rollScore]);
} else {
this.getLastFrame().push(rollScore);
}
};
updateCard: function (rollScore) {
if (this.isFullFrame()) {
this.card.push([rollScore]);
} else {
this.getLastFrame().push(rollScore);
}
},

ScoreCard.prototype.isFullFrame = function () {
return this.getLastFrame().length > 1;
};
isFullFrame: function () {
return this.getLastFrame().length > 1;
},

ScoreCard.prototype.getLastFrame = function () {
return this.getCard()[this.getCard().length - 1];
getLastFrame: function () {
return this.getCard()[this.getCard().length - 1];
}
};

0 comments on commit edf42fd

Please sign in to comment.