Skip to content

Commit 39c0fed

Browse files
committed
test(demo): test waiter and file-access
1 parent e971858 commit 39c0fed

File tree

5 files changed

+72
-22
lines changed

5 files changed

+72
-22
lines changed

__mocks__/chalk.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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;

__mocks__/jsonfile.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
'use strict';
22
const 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+
});
2111
jsonfile.setup = setup;
22-
jsonfile.readFileSync = readFileSync;
23-
jsonfile.writeFileSync = writeFileSync;
12+
jsonfile.writeFile = writeFile;
2413

2514
module.exports = jsonfile;

src/file-access.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ const jsonfile = require('jsonfile');
33

44
function 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

src/file-access.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
});

src/waiter.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
});

0 commit comments

Comments
 (0)