File tree Expand file tree Collapse file tree 5 files changed +72
-22
lines changed Expand file tree Collapse file tree 5 files changed +72
-22
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const chalk = jest . genMockFromModule ( 'chalk' ) ;
3+
4+ chalk . red = jest . fn ( ) ;
5+ chalk . green = jest . fn ( ) ;
6+ chalk . blue = {
7+ bold : jest . fn ( )
8+ } ;
9+
10+ module . exports = chalk ;
Original file line number Diff line number Diff line change 11'use strict' ;
22const jsonfile = jest . genMockFromModule ( 'jsonfile' ) ;
33
4- let mockedReadFileResponse ;
5- let throwError = false ;
4+ let throwError ;
65
7- const setup = ( responseMock , errorResponse ) => {
8- if ( errorResponse ) {
9- throwError = true ;
10- }
11- mockedReadFileResponse = responseMock ;
12- } ;
13- const readFileSync = jest . fn ( ( ) => {
14- if ( throwError ) {
15- throw 'Something went wrong' ;
16- }
17- return mockedReadFileResponse ;
18- } ) ;
19- const writeFileSync = jest . fn ( ( path , file ) => { } ) ;
6+ const setup = withError => ( throwError = withError ) ;
207
8+ const writeFile = jest . fn ( ( fileName , order , cb ) => {
9+ throwError ? cb ( 'Something went wrong' ) : cb ( ) ;
10+ } ) ;
2111jsonfile . setup = setup ;
22- jsonfile . readFileSync = readFileSync ;
23- jsonfile . writeFileSync = writeFileSync ;
12+ jsonfile . writeFile = writeFile ;
2413
2514module . exports = jsonfile ;
Original file line number Diff line number Diff line change @@ -3,11 +3,9 @@ const jsonfile = require('jsonfile');
33
44function writeFile ( fileName , order ) {
55 jsonfile . writeFile ( fileName , order , function ( err ) {
6- if ( err ) {
7- console . log ( chalk . red ( err ) ) ;
8- } else {
9- console . log ( chalk . green ( `Success` ) ) ;
10- }
6+ err
7+ ? console . log ( chalk . red ( err ) )
8+ : console . log ( chalk . green ( 'Order successfully written to file' ) ) ;
119 } ) ;
1210}
1311
Original file line number Diff line number Diff line change 1+ const jsonfile = require ( 'jsonfile' ) ;
2+ const chalk = require ( 'chalk' ) ;
3+
4+ const sut = require ( './file-access' ) ;
5+
6+ describe ( 'file access' , ( ) => {
7+ beforeAll ( ( ) => ( console . log = jest . fn ( ) ) ) ;
8+
9+ test ( 'should log an error message if something went wrong' , ( ) => {
10+ const throwError = true ;
11+ jsonfile . setup ( throwError ) ;
12+
13+ sut . writeFile ( '../test' , { } ) ;
14+ expect ( chalk . red ) . toHaveBeenCalled ( ) ;
15+ } ) ;
16+
17+ test ( 'should log a success message if everything went well' , ( ) => {
18+ jsonfile . setup ( false ) ;
19+
20+ sut . writeFile ( '../test' , { } ) ;
21+ expect ( chalk . green ) . toHaveBeenCalledWith (
22+ 'Order successfully written to file'
23+ ) ;
24+ } ) ;
25+ } ) ;
Original file line number Diff line number Diff line change 1+ const chalk = require ( 'chalk' ) ;
2+ const sut = require ( './waiter' ) ;
3+
4+ describe ( 'Waiter' , ( ) => {
5+ beforeAll ( ( ) => ( console . log = jest . fn ( ) ) ) ;
6+
7+ test ( 'should print the food on the order' , ( ) => {
8+ const food = 'Pizza' ;
9+ const drink = 'Coke' ;
10+ sut . placeOrder ( food , drink ) ;
11+
12+ expect ( chalk . green ) . toHaveBeenCalledWith (
13+ 'You ordered the following food: '
14+ ) ;
15+ expect ( chalk . blue . bold ) . toHaveBeenCalledWith ( food ) ;
16+ } ) ;
17+
18+ test ( 'should print the drink to the order' , ( ) => {
19+ const food = 'Pizza' ;
20+ const drink = 'Coke' ;
21+ sut . placeOrder ( food , drink ) ;
22+
23+ expect ( chalk . green ) . toHaveBeenCalledWith (
24+ 'You ordered the following drink: '
25+ ) ;
26+ expect ( chalk . blue . bold ) . toHaveBeenCalledWith ( drink ) ;
27+ } ) ;
28+ } ) ;
You can’t perform that action at this time.
0 commit comments