Skip to content

Commit

Permalink
Significant refactor and added new unit tests; Need to complete unit …
Browse files Browse the repository at this point in the history
…testing and fix feature test;
  • Loading branch information
Sam authored and Sam committed Jul 9, 2017
1 parent f721eda commit fb8671b
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 167 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ cd bowling-challenge
### To run tests

```bash
jasmine
npm i
open SpecRunner.html
```
If open command does not work on your machine, just copy the full path of the SpecRunner file into your browser's address bar after.

## User stories

-[] I want to start a game of bowling
-[x] I want to start a game of bowling

-[] I want to roll
-[x] I want to roll

-[] I want to have two rolls per frame
-[x] I want to have two rolls per frame

-[] I want to end a frame when I score 10 in first roll

-[] I want to keep track of my score for each roll including skipped ones
-[x] I want to keep track of my score for each roll including skipped ones

-[] I want to keep track of my score for each frame including bonuses

Expand Down
6 changes: 4 additions & 2 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@

<!-- include source files here... -->
<script src="src/ScoreCard.js"></script>
<script src="src/Game.js"></script>
<script src="src/Roll.js"></script>
<script src="src/Frame.js"></script>
<script src="src/Game.js"></script>



<!-- include spec files here... -->
<script src="spec/features/playFrameSpec.js"></script>
<script src="spec/units/RollSpec.js"></script>
<script src="spec/units/FrameSpec.js"></script>
<script src="spec/units/GameSpec.js"></script>
<!-- <script src="spec/ScoreCardSpec.js"></script> -->
<script src="spec/units/ScoreCardSpec.js"></script>

</head>

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
"doc": "docs"
},
"dependencies": {
"eslint": "^3.19.0"
},
"devDependencies": {
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.1.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"jasmine": "^2.6.0"
"eslint-plugin-standard": "^3.0.1"
},
"repository": {
"type": "git",
Expand Down
14 changes: 14 additions & 0 deletions spec/features/playFrameSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

describe('Play a frame', function () {
it('updates the scorecard with correct frame scores', function () {
var game, scorecard;
game = new Game();
spyOn(Math, 'random').and.returnValues(0.51, 0.9);
game.play();
game.play();
scorecard = game.getScoreCard();
console.dir(scorecard.getCard());
expect(scorecard.getCard()).toEqual([[5, 5]]);
});
});
91 changes: 53 additions & 38 deletions spec/units/FrameSpec.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,67 @@
'use strict';

describe('Frame', function() {
describe('Frame', function () {
var frame;
beforeEach(function() {

beforeEach(function () {
frame = new Frame();
});
describe('roll', function() {
it('returns a number between 0 and 10', function() {
frame.roll();
expect(frame.getScore()[0]).toBeLessThan(11);

describe('processRoll', function () {
it('calls checkFullFrame', function () {
spyOn(frame, 'checkFullFrame');
frame.processRoll();
expect(frame.checkFullFrame).toHaveBeenCalled();
});
it('does not roll more than 2 times', function() {
for (var i = 0; i < 3; i++) {
frame.roll();
}
expect(frame.getScore().length).toBeLessThan(3);
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(Math, 'random').and.returnValue(0.51);
spyOn(frame, 'updateFrameScore');
frame.processRoll();
expect(frame.updateFrameScore).toHaveBeenCalledWith(5);
});
});
describe('isSpare', function() {
it('sets this.spare to true when first and second score = 10', function() {
spyOn(Math, "random").and.returnValues(0.51, 0.9);
frame.roll();
frame.roll();
expect(frame.getScore()).toEqual([5, 5]);
expect(frame.getSpare()).toBeTrue;
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, 'updateFrameScore');
frame.processRoll();
expect(frame.updateFrameScore).toHaveBeenCalledWith(0);
});
it('does not set spare to true when strike', function() {
spyOn(Math, "random").and.returnValue(0.97);
frame.roll();
frame.roll();
expect(frame.getScore()).toEqual([10, 0]);
expect(frame.getSpare()).toBeFalse;
it('does not call updateFrameScore if rollcount is >= 2', function () {
spyOn(frame, 'getRollCount');
frame.getRollCount.and.returnValue(2);
spyOn(frame, 'remainder');
frame.remainder.and.returnValue(5);
spyOn(frame, 'updateFrameScore');
frame.processRoll();
expect(frame.updateFrameScore).not.toHaveBeenCalled();
});
});
describe('isStrike', function() {
it('sets this.strike to true when first score = 10', function() {
spyOn(Math, "random").and.returnValues(0.95, 0.5);
frame.roll();
frame.roll();
expect(frame.getScore()).toEqual([10, 0]);
expect(frame.getStrike()).toBeTrue;

describe('getRollCount', function () {
it('returns rollCount', function () {
frame.rollCount = 4;
expect(frame.getRollCount()).toEqual(4);
});
});

describe('remainder', function () {
it('defaults to return 10 if no roll has been taken', function () {
spyOn(frame, 'getRollCount');
frame.getRollCount.and.returnValue(0);
expect(frame.remainder()).toEqual(10);
});
it('does not set strike to true when second roll is 10', function() {
spyOn(Math, "random").and.returnValues(0, 0.97);
frame.roll();
frame.roll();
expect(frame.getScore()).toEqual([0, 10]);
expect(frame.getStrike()).toBeFalse;
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]);
expect(frame.remainder()).toEqual(6);
});
});
});
82 changes: 54 additions & 28 deletions spec/units/GameSpec.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,71 @@
'use strict';

describe('Game', function () {
var game;
var game, frame;

beforeEach(function () {
game = new Game();
frame = game.currentFrame;
});

describe('getScoreCard', function () {
it('returns each frame that has been started', function () {
for (var i = 0; i < 6; i++) {
game.play();
}
expect(game.getScoreCard().length).toEqual(6);
});
xit('returns both rolls for each frame', function () {
for (var i = 0; i < 6; i++) {
game.play();
}
expect(game.getScoreCard()[1].length).toEqual(2);
describe('play', function () {
it('calls processroll on currentFrame', function () {
spyOn(frame, 'processRoll');
game.play();
expect(frame.processRoll).toHaveBeenCalled();
});
it('calls nextFrame', function () {
spyOn(game, 'nextFrame');
game.play();
expect(game.nextFrame).toHaveBeenCalled();
});
});

describe('play', function () {
it('rolls a ball and updates the frame score', function () {
spyOn(Math, 'random').and.returnValue(0.5);
game.play();
expect(game.getScoreCard()).toEqual([
[5]
]);
describe('getCurrentFrame', function () {
it('returns a frame object', function () {
expect(frame instanceof Frame).toBeTruthy();
});
});

describe('startNewFrame', function () {
it('updates currentFrame with new Frame instance', function () {
game.startNewFrame();
var newFrame = game.getCurrentFrame();
expect(frame).not.toBe(newFrame);
});
});

describe('getTotalScore', function () {
it('returns total score', function () {
spyOn(Math, 'random').and.returnValues(0.5, 0.9, 0.1, 0.5);
for (var i = 0; i < 5; i++) {
game.play();
console.log(game.getScoreCard()[i]);
}
expect(game.getTotalScore()).toEqual();
describe('nextFrame', function () {
beforeEach(function () {
spyOn(game, 'isFrameFinished');
spyOn(game, 'startNewFrame');
});
it('calls startNewFrame if isFrameFinished returns true', function () {
game.isFrameFinished.and.returnValue(true);
game.nextFrame();
expect(game.isFrameFinished).toHaveBeenCalled();
expect(game.startNewFrame).toHaveBeenCalled();
});
it('does not call startNewFrame if isFrameFinished returns false', function () {
game.isFrameFinished.and.returnValue(false);
game.nextFrame();
expect(game.isFrameFinished).toHaveBeenCalled();
expect(game.startNewFrame).not.toHaveBeenCalled();
});
});

describe('isFrameFinished', function () {
it('calls getIsFinished on currentFrame', function () {
spyOn(frame, 'getIsFinished');
game.isFrameFinished();
expect(frame.getIsFinished).toHaveBeenCalled();
});
});

describe('getScoreCard', function () {
it('returns a frame object', function () {
var card = game.getScoreCard();
expect(card instanceof ScoreCard).toBeTruthy();
});
});
});
27 changes: 10 additions & 17 deletions spec/units/RollSpec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
'use strict';

describe('Roll', function () {
var roll;
beforeEach(function () {
roll = new Roll();
});

describe('randomInt', function () {
it('returns random number between 0 and the remaining pins for a frame',
describe('randomInt', function () {
it('returns random number between 0 and the remaining pins for a frame',
function () {
expect(roll.randomInt(10)).toBeLessThan(11);
expect(roll.randomInt(7)).toBeLessThan(8);
expect(roll.randomInt(5)).toBeLessThan(6);
expect(roll.randomInt(2)).toBeLessThan(3);
expect(randomInt(10)).toBeLessThan(11);
expect(randomInt(7)).toBeLessThan(8);
expect(randomInt(5)).toBeLessThan(6);
expect(randomInt(2)).toBeLessThan(3);
});
});
});

describe('score', function () {
it('returns random number between 0 and the remaining pins for a frame',
describe('roll', function () {
it('returns random number between 0 and the remaining pins for a frame',
function () {
expect(roll.score(10)).toBeLessThan(11);
expect(roll(10)).toBeLessThan(11);
});
});
});
52 changes: 52 additions & 0 deletions spec/units/ScoreCardSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

describe('ScoreCard', function () {
var card;

beforeEach(function () {
card = new ScoreCard();
});

describe('getCard', function () {
it('returns an array', function () {
expect(card.getCard()).toEqual(jasmine.any(Array));
});
it('returns array of arrays stored in this.card', function () {
card.card = [[1, 1], [5, 0]];
expect(card.getCard()).toEqual([[1, 1], [5, 0]]);
});
});

describe('updateCard', function () {
beforeEach(function () {
spyOn(card, 'isFullFrame');
});
it('checks if frame is full', function () {
card.updateCard(1);
expect(card.isFullFrame).toHaveBeenCalled();
});
it('updates complete frame with new rollScore', function () {
spyOn(card, 'getLastFrame');
card.getLastFrame.and.returnValue([1]);
card.isFullFrame.and.returnValue(false);
card.updateCard(2);
expect(card.getLastFrame).toHaveBeenCalled();
expect(card.getLastFrame()).toEqual([1, 2]);
});
it('pushes new frame if last frame has 2 objects', function () {
spyOn(card, 'getLastFrame').and.callThrough();
card.isFullFrame.and.returnValue(true);
card.updateCard(2);
expect(card.getLastFrame).not.toHaveBeenCalled();
expect(card.getLastFrame()).toEqual([2]);
expect(card.getCard()).toEqual([[], [2]]);
});
});

describe('getLastFrame', function () {
it('returns last item in this.card as an array', function () {
card.card = [[1, 1], [5, 0]];
expect(card.getLastFrame()).toEqual([5, 0]);
});
});
});
Loading

0 comments on commit fb8671b

Please sign in to comment.