forked from makersacademy/bowling-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoresheet.test.js
174 lines (161 loc) · 5.33 KB
/
scoresheet.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const Scoresheet = require("./scoresheet");
describe("Scoresheet", () => {
beforeEach(() => {
scoresheet = new Scoresheet();
mockEmptyFrame = {
scores: [],
rolls: () => 0,
isStrike: () => false,
isSpare: () => false,
};
mockFrame = {
rolls: () => 2,
firstRoll: () => 3,
secondRoll: () => 4,
isStrike: () => false,
isSpare: () => false,
};
mockStrike = {
rolls: () => 1,
firstRoll: () => 10,
secondRoll: () => 7,
bonusRoll: () => 2,
isStrike: () => true,
isSpare: () => false,
};
mockSpare = {
rolls: () => 2,
firstRoll: () => 3,
secondRoll: () => 7,
bonusRoll: () => 5,
isStrike: () => false,
isSpare: () => true,
};
perfectStrike = {
rolls: () => 1,
firstRoll: () => 10,
secondRoll: () => 10,
bonusRoll: () => 10,
isStrike: () => true,
isSpare: () => false,
};
});
describe("addFrame", () => {
it("can save a Frame", () => {
scoresheet.addFrame(mockFrame);
expect(scoresheet.frames.length).toEqual(1);
});
});
describe("newFrame", () => {
it("can create a new empty frame", () => {
scoresheet.newFrame(mockEmptyFrame);
expect(scoresheet.currentFrame.rolls()).toEqual(0);
});
});
describe("frameScore", () => {
it("can calculate the score of a frame", () => {
scoresheet.addFrame(mockFrame);
expect(scoresheet.frameScore(0)).toEqual(7);
});
});
describe("totalScore", () => {
it("can calculate the total score of a game", () => {
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
expect(scoresheet.totalScore()).toEqual(70);
});
});
describe("strikeBonus", () => {
it("should add the values of the next 2 rolls as a bonus for hitting a strike", () => {
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockFrame);
expect(scoresheet.frameScore(0)).toEqual(17);
});
it("should add the values of the next 2 rolls as a bonus even if from different frames", () => {
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockFrame);
expect(scoresheet.frameScore(0)).toEqual(23);
});
it("if on the 9th frame a strike is landed, it will add the first two rolls of the tenth frame even if tenth frame is a strike", () => {
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockStrike);
expect(scoresheet.frameScore(8)).toEqual(27);
});
});
describe("spareBonus", () => {
it("should add the values of the next roll as a bonus for hitting a spare", () => {
scoresheet.addFrame(mockSpare);
scoresheet.addFrame(mockFrame);
expect(scoresheet.frameScore(0)).toEqual(13);
});
});
it("will have a bonus roll on the final frame if a spare is landed", () => {
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockSpare);
expect(scoresheet.frameScore(9)).toEqual(15);
});
it("will have a bonus roll on the final frame if a strike is landed", () => {
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockStrike);
expect(scoresheet.frameScore(9)).toEqual(19);
});
it("calculates score of perfect game correctly", () => {
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
scoresheet.addFrame(perfectStrike);
expect(scoresheet.totalScore()).toEqual(300);
});
it("calculates score of a random game correctly", () => {
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockSpare);
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockSpare);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(mockStrike);
scoresheet.addFrame(mockFrame);
scoresheet.addFrame(perfectStrike);
expect(scoresheet.totalScore()).toEqual(148);
});
});