|
| 1 | +import * as v from '.' |
| 2 | + |
| 3 | +test('email', () => { |
| 4 | + expect(v.email('invalid')).toBeTruthy() |
| 5 | + expect(v.email('invalid@invalid')).toBeTruthy() |
| 6 | + expect(v.email('[email protected]')).toBeFalsy() |
| 7 | +}) |
| 8 | + |
| 9 | +test('url', () => { |
| 10 | + expect(v.url('invalid')).toBeTruthy() |
| 11 | + expect(v.url('valid.com')).toBeFalsy() |
| 12 | + expect(v.url('valid.com/test')).toBeFalsy() |
| 13 | + expect(v.url('http://valid.com')).toBeFalsy() |
| 14 | +}) |
| 15 | + |
| 16 | +test('required', () => { |
| 17 | + expect(v.required('')).toBeTruthy() |
| 18 | + expect(v.required(null)).toBeTruthy() |
| 19 | + expect(v.required(undefined)).toBeTruthy() |
| 20 | + expect(v.required('valid')).toBeFalsy() |
| 21 | +}) |
| 22 | + |
| 23 | +test('minLength', () => { |
| 24 | + expect(v.minLength(5)('1234')).toBeTruthy() |
| 25 | + expect(v.minLength(5)('12345')).toBeFalsy() |
| 26 | +}) |
| 27 | + |
| 28 | +test('maxLength', () => { |
| 29 | + expect(v.maxLength(5)('123456')).toBeTruthy() |
| 30 | + expect(v.maxLength(5)('12345')).toBeFalsy() |
| 31 | +}) |
| 32 | + |
| 33 | +test('integer', () => { |
| 34 | + expect(v.integer('invalid')).toBeTruthy() |
| 35 | + expect(v.integer('2.3')).toBeTruthy() |
| 36 | + expect(v.integer('.5')).toBeTruthy() |
| 37 | + expect(v.integer('1')).toBeFalsy() |
| 38 | +}) |
| 39 | + |
| 40 | +test('oneOf', () => { |
| 41 | + expect(v.oneOf(['valid', 'test'])('invalid')).toBeTruthy() |
| 42 | + expect(v.oneOf(['valid', 'test'])('valid')).toBeFalsy() |
| 43 | + expect(v.oneOf(['valid', 'test'])('test')).toBeFalsy() |
| 44 | +}) |
| 45 | + |
| 46 | +test('match', () => { |
| 47 | + expect(v.match('invalid')('123', { password: '321' })).toBeTruthy() |
| 48 | + expect(v.match('password')('123', { password: '321' })).toBeTruthy() |
| 49 | + expect(v.match('password')('321', { password: '321' })).toBeFalsy() |
| 50 | +}) |
| 51 | + |
| 52 | +test('createValidator', () => { |
| 53 | + const validator = v.createValidator({ |
| 54 | + email: [v.required, v.email], |
| 55 | + password: [v.required, v.minLength(6)], |
| 56 | + passwordRepeat: [v.match('password'), v.required] |
| 57 | + }) |
| 58 | + |
| 59 | + expect(typeof validator).toBe('function') |
| 60 | + |
| 61 | + expect(validator({ |
| 62 | + email: '', |
| 63 | + password: '', |
| 64 | + passwordRepeat: null |
| 65 | + })).toEqual({ |
| 66 | + email: v.required(''), |
| 67 | + password: v.required(''), |
| 68 | + passwordRepeat: v.match('a')('c', { a: 'b' }) |
| 69 | + }, 'Expected to follow the validation order') |
| 70 | + |
| 71 | + expect(Object.keys(validator({ |
| 72 | + email: 'invalid', |
| 73 | + password: '12345', |
| 74 | + passwordRepeat: '' |
| 75 | + }))).toEqual(['email', 'password', 'passwordRepeat']) |
| 76 | + |
| 77 | + expect(Object.keys(validator({ |
| 78 | + |
| 79 | + password: '12345', |
| 80 | + passwordRepeat: '' |
| 81 | + }))).toEqual(['password', 'passwordRepeat']) |
| 82 | + |
| 83 | + expect(Object.keys(validator({ |
| 84 | + |
| 85 | + password: '123456', |
| 86 | + passwordRepeat: '654321' |
| 87 | + }))).toEqual(['passwordRepeat']) |
| 88 | + |
| 89 | + expect(validator({ |
| 90 | + |
| 91 | + password: '123456', |
| 92 | + passwordRepeat: '123456' |
| 93 | + })).toEqual({}) |
| 94 | + |
| 95 | + expect(validator()).toEqual({ |
| 96 | + email: v.required(''), |
| 97 | + password: v.required(''), |
| 98 | + passwordRepeat: v.required('') |
| 99 | + }) |
| 100 | +}) |
0 commit comments