You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

28 lines
1.1 KiB

import test from 'ava';
import m from '..';
test('not', t => {
t.notThrows(() => m(1, m.number.not.infinite));
t.notThrows(() => m(1, m.number.not.infinite.greaterThan(5)));
t.notThrows(() => m('foo!', m.string.not.alphanumeric));
t.throws(() => m('' as any, m.string.not.empty), '[NOT] Expected string to be empty, got ``');
});
test('is', t => {
const greaterThan = (max: number, x: number) => {
return x > max || `Expected \`${x}\` to be greater than \`${max}\``;
};
t.notThrows(() => m(1, m.number.is(x => x < 10)));
t.throws(() => m(1, m.number.is(x => x > 10)), 'Expected `1` to pass custom validation function');
t.throws(() => m(5, m.number.is(x => greaterThan(10, x))), 'Expected `5` to be greater than `10`');
});
test('reusable validator', t => {
const checkUsername = m.create(m.string.minLength(3));
t.notThrows(() => checkUsername('foo'));
t.notThrows(() => checkUsername('foobar'));
t.throws(() => checkUsername('fo'), 'Expected string to have a minimum length of `3`, got `fo`');
t.throws(() => checkUsername(5 as any), 'Expected argument to be of type `string` but received type `number`');
});