File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments