Skip to content

Commit 849f605

Browse files
committed
Update: add 3-payment.js file
1 parent bc8f9ff commit 849f605

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

0x06-unittests_in_js/3-payment.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const Utils = require('./utils');
2+
3+
const sendPaymentRequestToApi = (totalAmount, totalShipping) => {
4+
const total = Utils.calculateNumber('SUM', totalAmount, totalShipping);
5+
console.log(`The total is: ${total}`);
6+
};
7+
8+
module.exports = sendPaymentRequestToApi;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const sinon = require('sinon');
2+
const Utils = require('./utils');
3+
const { expect } = require('chai');
4+
const sendPaymentRequestToApi = require('./3-payment');
5+
6+
describe('sendPaymentRequestToApi', () => {
7+
it('sendPaymentRequestToApi uses the calculateNumber method of Utils', () => {
8+
const sender = sinon.spy(Utils);
9+
10+
sendPaymentRequestToApi(100, 20);
11+
expect(sender.calculateNumber.calledWith('SUM', 100, 20)).to.be.true;
12+
expect(sender.calculateNumber.callCount).to.be.equal(1);
13+
sender.calculateNumber.restore();
14+
});
15+
});

0x06-unittests_in_js/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const Utils = {
2+
calculateNumber(type, a, b) {
3+
if (type === 'SUM') {
4+
return Math.round(a) + Math.round(b);
5+
}
6+
if (type === 'SUBTRACT') {
7+
return Math.round(a) - Math.round(b);
8+
}
9+
if (type === 'DIVIDE') {
10+
return Math.round(b) === 0 ? 'Error' : Math.round(a) / Math.round(b);
11+
}
12+
return 0;
13+
},
14+
};
15+
16+
module.exports = Utils;

0 commit comments

Comments
 (0)