Skip to content

Commit 577ee95

Browse files
committed
add tests
1 parent 9379394 commit 577ee95

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

Diff for: test/calculator.test.js

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
const calculateUserInput = require('../calculator').calculateUserInput;
2+
3+
describe('calculateUserInput', () => {
4+
let standardOutput = null;
5+
6+
// Before each test replace standard output (console.log)
7+
// with a "mock" function so we can see that console.log
8+
// was called with the proper parameters.
9+
beforeEach(() => {
10+
standardOutput = console.log;
11+
console.log = jest.fn(str => str);
12+
});
13+
14+
// After each test restore console.log
15+
afterEach(() => {
16+
console.log = standardOutput;
17+
})
18+
test('3 + 4 = 7', () => {
19+
20+
calculateUserInput({}, {
21+
num1: '3',
22+
num2: '4',
23+
operation: '+',
24+
});
25+
26+
expect(console.log).toHaveBeenCalled();
27+
expect(console.log.mock.calls).toContainEqual(['3 + 4 = 7']);
28+
});
29+
30+
test('3 + 4 = 7 with "add"', () => {
31+
32+
calculateUserInput({}, {
33+
num1: '3',
34+
num2: '4',
35+
operation: 'add',
36+
});
37+
38+
expect(console.log).toHaveBeenCalled();
39+
expect(console.log.mock.calls).toContainEqual(['3 + 4 = 7']);
40+
});
41+
test('3 + -4 = -1', () => {
42+
43+
calculateUserInput({}, {
44+
num1: '3',
45+
num2: '-4',
46+
operation: '+',
47+
});
48+
49+
expect(console.log).toHaveBeenCalled();
50+
expect(console.log.mock.calls).toContainEqual(['3 + -4 = -1']);
51+
});
52+
53+
test('3 + -4 = -1 with "add"', () => {
54+
55+
calculateUserInput({}, {
56+
num1: '3',
57+
num2: '-4',
58+
operation: 'add',
59+
});
60+
61+
expect(console.log).toHaveBeenCalled();
62+
expect(console.log.mock.calls).toContainEqual(['3 + -4 = -1']);
63+
});
64+
65+
test('3 * 4 = 12', () => {
66+
67+
calculateUserInput({}, {
68+
num1: '3',
69+
num2: '4',
70+
operation: '*',
71+
});
72+
73+
expect(console.log).toHaveBeenCalled();
74+
expect(console.log.mock.calls).toContainEqual(['3 * 4 = 12']);
75+
});
76+
77+
test('3 * 4 = 12 with "multiply"', () => {
78+
79+
calculateUserInput({}, {
80+
num1: '3',
81+
num2: '4',
82+
operation: 'multiply',
83+
});
84+
85+
expect(console.log).toHaveBeenCalled();
86+
expect(console.log.mock.calls).toContainEqual(['3 * 4 = 12']);
87+
});
88+
89+
test('3 * -4 = -12', () => {
90+
91+
calculateUserInput({}, {
92+
num1: '3',
93+
num2: '-4',
94+
operation: '*',
95+
});
96+
97+
expect(console.log).toHaveBeenCalled();
98+
expect(console.log.mock.calls).toContainEqual(['3 * -4 = -12']);
99+
});
100+
101+
test('3 * -4 = -12, with "multiply', () => {
102+
103+
calculateUserInput({}, {
104+
num1: '3',
105+
num2: '-4',
106+
operation: '*',
107+
});
108+
109+
expect(console.log).toHaveBeenCalled();
110+
expect(console.log.mock.calls).toContainEqual(['3 * -4 = -12']);
111+
});
112+
113+
test('3 * 0 = 0', () => {
114+
115+
calculateUserInput({}, {
116+
num1: '3',
117+
num2: '0',
118+
operation: '*',
119+
});
120+
121+
expect(console.log).toHaveBeenCalled();
122+
expect(console.log.mock.calls).toContainEqual(['3 * 0 = 0']);
123+
});
124+
125+
test('15 / 3 = 5', () => {
126+
127+
calculateUserInput({}, {
128+
num1: '15',
129+
num2: '3',
130+
operation: '/',
131+
});
132+
133+
expect(console.log).toHaveBeenCalled();
134+
expect(console.log.mock.calls).toContainEqual(['15 / 3 = 5']);
135+
});
136+
137+
test('15 / 3 = 5 with "divide"', () => {
138+
139+
calculateUserInput({}, {
140+
num1: '15',
141+
num2: '3',
142+
operation: 'divide',
143+
});
144+
145+
expect(console.log).toHaveBeenCalled();
146+
expect(console.log.mock.calls).toContainEqual(['15 / 3 = 5']);
147+
});
148+
149+
test('-15 / 3 = 5', () => {
150+
151+
calculateUserInput({}, {
152+
num1: '-15',
153+
num2: '3',
154+
operation: '/',
155+
});
156+
157+
expect(console.log).toHaveBeenCalled();
158+
expect(console.log.mock.calls).toContainEqual(['-15 / 3 = -5']);
159+
});
160+
161+
test('Can catch divideByZero errors', () => {
162+
163+
calculateUserInput({}, {
164+
num1: '15',
165+
num2: '0',
166+
operation: '/',
167+
});
168+
169+
expect(console.log).toHaveBeenCalled();
170+
expect(console.log.mock.calls).toContainEqual(['You cannot divide by zero']);
171+
});
172+
});
173+

0 commit comments

Comments
 (0)