-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.test.js
More file actions
153 lines (117 loc) · 4.89 KB
/
main.test.js
File metadata and controls
153 lines (117 loc) · 4.89 KB
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
import {test, expect, describe } from '@jest/globals'
import {startGame} from "./main.js"
// Welcome to guess the number game! Your task is to create a guess the number game. It should:
//MVP 1 COMPLETED
// Generate a random number between 1 and 100.
// It takes in a guess and returns a string with the following outcomes:
// If the guess is correct, return "You got it!"
// If the guess is higher than the number, return "Too high!"
// if the guess is lower than the number, return "Too low!"
//MVP2 COMPLETED
//Edge cases
// if the input invalid, return "Please enter a number between 1 and 100"
// if the input is not a number, return "Please enter a number between 1 and 100"
//MVP3 COMPLETED
// if the guess is more than 20 higher than the number, return "Way too high"
// if the guess is more than 20 lower than the number, return "Way too low"
//MVP4
// limit the number of guesses to 10
// if the user runs out of guesses, return "You lose! The number was [number]"
// if the user guesses the number within 10 guesses, return "You win! The number was [number]"
//MVP 5
// Keep track of guesses and return how many guesses are remaining
//MVP 6
// Have a replay feature
describe("Number Guess Game Tests", () => {
test('should return "You got it!" if the guess is correct', () => {
// Arrange
const mockNumber = 50;
const guess = mockNumber;
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("You got it!");
// Restore the original Math.random
jest.restoreAllMocks();
});
test('should return "Too high!" if the guess is higher than the number', () => {
// Arrange
const mockNumber = 50;
const guess = 60;
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("Too high!");
// Restore the original Math.random
jest.restoreAllMocks();
});
test('should return "Too low!" if the guess is lower than the number', () => {
// Arrange
const mockNumber = 50;
const guess = 40;
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("Too low!");
// Restore the original Math.random
jest.restoreAllMocks();
});
test('should return "Please enter a number between 1 and 100" if the input is out of range', () => {
// Arrange
const mockNumber = 50;
const guess = 101;
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("Please enter a number between 1 and 100");
// Restore the original Math.random
jest.restoreAllMocks();
});
test('should return "Please enter a number between 1 and 100" if the input is not a number', () => {
// Arrange
const mockNumber = 50;
const guess = "string";
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("Please enter a number between 1 and 100");
// Restore the original Math.random
jest.restoreAllMocks();
});
test('should return "Way too low if guess is more than 20 less than number', () => {
// Arrange
const mockNumber = 50;
const guess = 20;
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("Way too low");
// Restore the original Math.random
jest.restoreAllMocks();
});
test('should return "Way too high" if guess is more than 20 greater than number', () => {
// Arrange
const mockNumber = 50;
const guess = 80;
// Mock the random number generation to return the mock number
jest.spyOn(Math, 'random').mockReturnValue(0.49); // 0.49 * 100 = 49 + 1 = 50
// Act
const result = startGame(guess);
// Assert
expect(result).toBe("Way too high");
// Restore the original Math.random
jest.restoreAllMocks();
});
});