Skip to content

Commit

Permalink
chapter 03: copy code from chapter 02 to the chapter 03 folder in ord…
Browse files Browse the repository at this point in the history
…er to continue with the book
  • Loading branch information
devcorpio committed Jan 10, 2019
1 parent 4a2856b commit b23053f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ArgumentError extends Error {}

module.exports = ArgumentError;
41 changes: 41 additions & 0 deletions chapter_03-using-stubs-to-break-dependencies/LogAn/logAnalyzer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const ArgumentError = require('./ArgumentError');

function logAnalyzer() {
/**
* @type {boolean}
*/
let wasLastFileNameValid;

/**
* @return {boolean}
*/
function getWasLastFileNameValid() {
return wasLastFileNameValid;
}

/**
* @param {string} fileName
* @return {boolean}
*/
function isValidLogFileName(fileName) {
wasLastFileNameValid = false;

if (fileName === '') {
throw new ArgumentError('filename has to be provided');
}

if (!fileName.toUpperCase().endsWith('.SLF')) {
return false;
}

wasLastFileNameValid = true;
return true;
}

return {
getWasLastFileNameValid,
isValidLogFileName,
};
}

module.exports = logAnalyzer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const logAnalyzer = require('./logAnalyzer');

let logAnalyzerInstance;
beforeEach(() => {
logAnalyzerInstance = logAnalyzer();
});

describe.each([
['johndoe.js', false],
['johndoe.slf', true],
['johndoe.SLF', true],
])('isValidLogFileName("%s"))', (fileName, expected) => {
it(`bad extension returns ${expected}`, () => {
const result = logAnalyzerInstance.isValidLogFileName(fileName);
expect(result).toBe(expected);
});
});

describe('isValidLogFileName', () => {
it('empty filename throws error', () => {
function emptyLogFileName() {
logAnalyzerInstance.isValidLogFileName('');
}

expect(emptyLogFileName).toThrow('filename has to be provided');
});

/**
* an example of state-based testing
*/
it.each`
fileName | expected
${'johndoe.foo'} | ${false}
${'johndoe.slf'} | ${true}
`(
'when called there changes wasLastFileNameValid that returns $expected',
({ fileName, expected }) => {
console.log(fileName);
logAnalyzerInstance.isValidLogFileName(fileName);
const result = logAnalyzerInstance.getWasLastFileNameValid();

expect(result).toBe(expected);
}
);
});

0 comments on commit b23053f

Please sign in to comment.