Created
May 1, 2018 08:25
-
-
Save konstantin24121/ff0350f2d95529a00db9b1b335d5b338 to your computer and use it in GitHub Desktop.
At least validator with test
This file contains hidden or 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
/** | |
* Array must containt at least nessesary values | |
* @param {Number} [count=1] nessesary values in array | |
* @param {String} [message='You must select at least one of the options'] error message | |
* @return {?String} | |
*/ | |
const atLeast = ( | |
count = 1, | |
message = 'You must select at least one of the options') => | |
(value) => { | |
return value.length >= count ? undefined : message; | |
}; | |
export default atLeast; |
This file contains hidden or 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
import createValidator from '../atLeast'; | |
describe('At least validator', () => { | |
test('must return undefined when validation is passed', () => { | |
const atLeast = createValidator(); | |
expect(atLeast(['one', 'ring', 'to', 'rule', 'them', 'all'])).toBeUndefined(); | |
}); | |
test('must return error message when validation is faled', () => { | |
const atLeast = createValidator(); | |
expect(atLeast([])).not.toBeUndefined(); | |
}); | |
test('must return special error message when validation is faled', () => { | |
const message = 'Fatal error'; | |
const atLeastWithMessage = createValidator(1, message); | |
expect(atLeastWithMessage([])).toBe(message); | |
}); | |
test('with another at least value when validation is passed', () => { | |
const atLeastFive = createValidator(5); | |
expect(atLeastFive(['one', 'ring', 'to', 'rule', 'them', 'all'])).toBeUndefined(); | |
}); | |
test('with another at least value when validation is failed', () => { | |
const atLeastFive = createValidator(10); | |
expect(atLeastFive(['one', 'ring', 'to', 'rule', 'them', 'all'])).not.toBeUndefined(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment