-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: use Jest assertion return api (#349)
- Loading branch information
Showing
11 changed files
with
245 additions
and
8 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,54 @@ | ||
import throwError from '../throwError'; | ||
|
||
export function validateResult(result, rule) { | ||
if ( | ||
typeof result !== 'boolean' && | ||
(!result || typeof result.pass !== 'boolean') | ||
) { | ||
throwError( | ||
`/${rule.name} wrong return value for the rule please check that the return is valid` | ||
); | ||
} | ||
} | ||
|
||
// for easier testing and mocking | ||
export function getDefaultResult(value, rule) { | ||
return { | ||
message: formatResultMessage(rule, `invalid ${typeof value} value`), | ||
}; | ||
} | ||
|
||
export function formatResultMessage(rule, msg) { | ||
return `[${LIBRARY_NAME}]/${rule.name} ${msg}`; | ||
} | ||
|
||
/** | ||
* Transform the result of a rule into a standard format | ||
* @param {string} interfaceName to be used in the messages | ||
* @param {*} result of the rule | ||
* @param {Object} options | ||
* @param {function} options.rule | ||
* @param {*} options.value | ||
* @returns {Object} result | ||
* @returns {string} result.message | ||
* @returns {boolean} result.pass indicates if the test passes or not | ||
*/ | ||
export function transformResult(result, { rule, value }) { | ||
const defaultResult = getDefaultResult(value, rule); | ||
validateResult(result, rule); | ||
|
||
if (typeof result === 'boolean') { | ||
return { ...defaultResult, pass: result }; | ||
} else { | ||
const formattedResult = { | ||
pass: result.pass, | ||
}; | ||
if (result.message) { | ||
formattedResult.message = formatResultMessage( | ||
rule, | ||
typeof result.message === 'function' ? result.message() : result.message | ||
); | ||
} | ||
return { ...defaultResult, ...formattedResult }; | ||
} | ||
} |
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 @@ | ||
import { | ||
goodBooleanRule, | ||
goodObjectRule, | ||
goodObjectMessageRule, | ||
badObjectRule, | ||
nullRule, | ||
goodObjectMessageFunctionRule, | ||
} from '../../testUtils/rules'; | ||
import { | ||
transformResult, | ||
getDefaultResult, | ||
formatResultMessage, | ||
validateResult, | ||
} from '.'; | ||
|
||
describe('Tests `validateResult` helper', () => { | ||
it('Should pass with boolean return values', () => { | ||
const testRule = () => true; | ||
validateResult(testRule()); | ||
}); | ||
|
||
it('Should pass with a verbose return value', () => { | ||
const testRule = () => ({ pass: false, message: 'pass' }); | ||
validateResult(testRule()); | ||
}); | ||
|
||
it('Should throw error when reciving null', () => { | ||
const testRule = () => null; | ||
expect(() => validateResult(testRule())).toThrow(Error); | ||
}); | ||
|
||
it("Should return false if it is a verbose result and doesn't have pass property", () => { | ||
const missingPass = () => ({ message: 'missing pass' }); | ||
expect(() => validateResult(missingPass())).toThrow(Error); | ||
}); | ||
}); | ||
|
||
describe.each([true, false])('Test transform result', bool => { | ||
// to make sure we don't end up leaving some hardcoded value behind | ||
it(`Should transform a boolean`, () => { | ||
const { message: expectedMessage } = getDefaultResult( | ||
bool, | ||
goodBooleanRule | ||
); | ||
const result = goodBooleanRule(bool); | ||
expect( | ||
transformResult(result, { rule: goodBooleanRule, value: bool }) | ||
).toEqual({ pass: bool, message: expectedMessage }); | ||
}); | ||
|
||
it('Should transform a complete object', () => { | ||
const result = goodObjectRule(bool); | ||
expect( | ||
transformResult(result, { rule: goodObjectRule, value: bool }) | ||
).toEqual({ | ||
pass: bool, | ||
message: formatResultMessage(goodObjectRule, result.message), | ||
}); | ||
}); | ||
|
||
it('Should add default message', () => { | ||
const result = goodObjectMessageRule(bool); | ||
const { message: expectedMessage } = getDefaultResult( | ||
bool, | ||
goodObjectMessageRule | ||
); | ||
|
||
expect( | ||
transformResult(result, { | ||
rule: goodObjectMessageRule, | ||
value: bool, | ||
}) | ||
).toEqual({ | ||
pass: bool, | ||
message: expectedMessage, | ||
}); | ||
}); | ||
|
||
it('Should get the message from the function', () => { | ||
const result = goodObjectMessageFunctionRule(bool); | ||
expect( | ||
transformResult(result, { | ||
rule: goodObjectMessageFunctionRule, | ||
value: bool, | ||
}) | ||
).toEqual({ | ||
pass: bool, | ||
message: formatResultMessage( | ||
goodObjectMessageFunctionRule, | ||
result.message() | ||
), | ||
}); | ||
}); | ||
|
||
it('Should throw with malformed result', () => { | ||
const result = badObjectRule(bool); | ||
expect(() => | ||
transformResult(result, { rule: badObjectRule, value: bool }) | ||
).toThrow(Error); | ||
}); | ||
|
||
it('Should throw with null result', () => { | ||
const result = nullRule(bool); | ||
expect(() => | ||
transformResult(result, { rule: nullRule, value: bool }) | ||
).toThrow(Error); | ||
}); | ||
}); | ||
|
||
describe("Test transform result's message", () => { | ||
[goodObjectMessageRule, goodObjectMessageRule, goodObjectRule].forEach( | ||
rule => { | ||
it('Should contain the library name', () => { | ||
const result = rule(true); | ||
expect(transformResult(result, { rule, value: true }).message).toMatch( | ||
LIBRARY_NAME | ||
); | ||
}); | ||
|
||
it('Should contain the name of the rule', () => { | ||
const result = rule(true); | ||
expect(transformResult(result, { rule, value: true }).message).toMatch( | ||
rule.name | ||
); | ||
}); | ||
} | ||
); | ||
}); |
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,31 @@ | ||
export function goodBooleanRule(bool) { | ||
return bool; | ||
} | ||
|
||
export function goodObjectRule(bool) { | ||
return { | ||
pass: bool, | ||
message: 'What did you do Carl!?', | ||
}; | ||
} | ||
|
||
export function goodObjectMessageRule(bool) { | ||
return { | ||
pass: bool, | ||
}; | ||
} | ||
|
||
export function goodObjectMessageFunctionRule(bool) { | ||
return { | ||
pass: bool, | ||
message: () => `I passed with ${bool}`, | ||
}; | ||
} | ||
|
||
export function badObjectRule() { | ||
return { message: 'Something went wrong' }; | ||
} | ||
|
||
export function nullRule() { | ||
return null; | ||
} |