-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter 03: copy code from chapter 02 to the chapter 03 folder in ord…
…er to continue with the book
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
chapter_03-using-stubs-to-break-dependencies/LogAn/ArgumentError.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
chapter_03-using-stubs-to-break-dependencies/LogAn/logAnalyzer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
45 changes: 45 additions & 0 deletions
45
chapter_03-using-stubs-to-break-dependencies/LogAn/logAnalyzer.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); |