Skip to content

Commit 6ecfd55

Browse files
committed
chapter 02: add a memory calculator in order to test it with state-based testing
1 parent 9b319ea commit 6ecfd55

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

chapter_02-a-first-unit-test/MemCalculator/memCalculator.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function memCalculator() {
2323

2424
return temp;
2525
}
26+
27+
return {
28+
add,
29+
result,
30+
};
2631
}
2732

2833
module.exports = memCalculator;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const memCalculator = require('./memCalculator');
2+
3+
let memCalculatorInstance;
4+
beforeEach(() => {
5+
memCalculatorInstance = memCalculator();
6+
});
7+
8+
describe('result', () => {
9+
it('by default returns zero', () => {
10+
const lastResult = memCalculatorInstance.result();
11+
expect(lastResult).toBe(0);
12+
});
13+
14+
it('changes when call add', () => {
15+
const expectedResult = 12;
16+
17+
memCalculatorInstance.add(5);
18+
memCalculatorInstance.add(7);
19+
20+
const lastResult = memCalculatorInstance.result();
21+
22+
expect(lastResult).toBe(expectedResult);
23+
});
24+
});

0 commit comments

Comments
 (0)