-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: createTestHandler constructor can be extended (#134)
- expose createTestHandler - add constructor parameter to createTestHandler - write example-readme-custom-testhandler tests - write documentation about createTestHandler Closes #133 Ref #134
- Loading branch information
1 parent
7f98550
commit 088184b
Showing
6 changed files
with
181 additions
and
13 deletions.
There are no files selected for viewing
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
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,128 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import expect from 'expect.js'; | ||
import { test, describe } from 'async-describe'; | ||
|
||
module.exports = ({ io, handler, testHandler, createTestHandler }) => ( | ||
describe('[README.md] Custom testHandler example', async () => { | ||
const log = io(console.log); | ||
|
||
const createCustomTestHandler = (h, mockedIOs = [], expectedRetValue, assertRet = false, constructor = createCustomTestHandler) => { | ||
return { | ||
...createTestHandler(h, mockedIOs, expectedRetValue, assertRet, constructor), | ||
matchLog: (arg, ret) => constructor( | ||
h, | ||
[...mockedIOs, [io(console.log)(arg), ret]], | ||
expectedRetValue, | ||
assertRet, | ||
constructor, | ||
), | ||
}; | ||
}; | ||
const customTestHandler = h => createCustomTestHandler(h); | ||
|
||
const myHandler = handler(function*(value) { | ||
yield log(value); | ||
yield log(value); | ||
return 42; | ||
}); | ||
|
||
await describe('whith testHandler', async () => { | ||
await test('logs "hello world" twice and return 42', () => { | ||
testHandler(myHandler('hello world')) | ||
.matchIo(log('hello world')) | ||
.matchIo(log('hello world')) | ||
.shouldReturn(42) | ||
.run() | ||
}); | ||
}); | ||
|
||
await describe('with customTestHandler (success)', async () => { | ||
await test('logs "hello world" twice and return 42', async () => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world') | ||
.matchLog('hello world') | ||
.shouldReturn(42) | ||
.run(); | ||
|
||
customTestHandler(myHandler('hello world')) | ||
.shouldReturn(42) | ||
.matchLog('hello world') | ||
.matchIo(log('hello world')) | ||
.run(); | ||
|
||
customTestHandler(myHandler('hello world')) | ||
.shouldReturn(42) | ||
.matchIo(log('hello world')) | ||
.matchLog('hello world') | ||
.run(); | ||
}); | ||
}); | ||
|
||
await describe('with customTestHandler (failure)', async () => { | ||
await test('throws an error when arguments are invalid', async () => { | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world 1') | ||
.matchLog('hello world') | ||
.shouldReturn(42) | ||
.run(); | ||
}).to.throwError('Invalid IO#0 function arguments') | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world') | ||
.matchLog('hello world 2') | ||
.shouldReturn(42) | ||
.run(); | ||
}).to.throwError('Invalid IO#1 function arguments') | ||
}); | ||
|
||
await test('throws an error when io functions are wrong', async () => { | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchIo(io(() => {})('hello world')) | ||
.matchLog('hello world') | ||
.shouldReturn(42) | ||
.run(); | ||
}).to.throwError('Invalid IO#0 function'); | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world') | ||
.matchIo(io(() => {})('hello world')) | ||
.shouldReturn(42) | ||
.run(); | ||
}).to.throwError('Invalid IO#1 function'); | ||
}); | ||
|
||
await test('throws an error when returned value is wrong', async () => { | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world') | ||
.matchLog('hello world') | ||
.shouldReturn(0) | ||
.run(); | ||
}).to.throwError('Invalid returned value'); | ||
}); | ||
|
||
await test('throws an error when too much io ran', async () => { | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world') | ||
.shouldReturn(42) | ||
.run(); | ||
}).to.throwError('Too much io ran'); | ||
}); | ||
|
||
await test('throws an error when not enough io ran', async () => { | ||
expect(() => { | ||
customTestHandler(myHandler('hello world')) | ||
.matchLog('hello world') | ||
.matchLog('hello world') | ||
.matchLog('hello world') | ||
.shouldReturn(42) | ||
.run(); | ||
}).to.throwError('Not enough io ran'); | ||
}); | ||
}); | ||
}) | ||
) |
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export { default as io } from './io'; | ||
export { default as handler } from './handler'; | ||
export { default as testHandler } from './testHandler'; | ||
export { default as testHandler, createTestHandler } from './testHandler'; | ||
export { default as catchError } from './catchError'; | ||
export { default as simulateThrow } from './simulateThrow'; |
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