validator/types/string.test.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-06-03 15:49:04 +00:00
/* eslint-disable no-undef */
const TypeString = require('./string.js')
describe('test validate type schema string', () => {
function throwFunc (val) {
return () => {
if (val) throw new Error(val)
}
}
it('test string type without required', async () => {
const str = new TypeString()
str.validate(undefined)
})
it('test string type with required', async () => {
const str = new TypeString().required()
str.validate('123')
expect(throwFunc(str.validate(undefined))).toThrow()
})
it('test string type with empty', async () => {
const str = new TypeString().empty().required()
str.validate('')
})
it('test string type without empty', async () => {
const str = new TypeString().required()
str.validate('asd')
expect(throwFunc(str.validate(''))).toThrow()
})
it('test string type with min length', async () => {
const str = new TypeString().required().min(2)
expect(() => new TypeString().min()).toThrow()
expect(() => new TypeString().min(false)).toThrow()
expect(() => new TypeString().max(2).min(3)).toThrow()
str.validate('asd')
expect(throwFunc(str.validate('a'))).toThrow()
})
it('test string type with max length', async () => {
const str = new TypeString().required().max(2)
expect(() => new TypeString().max()).toThrow()
expect(() => new TypeString().max(false)).toThrow()
expect(() => new TypeString().min(2).max(1)).toThrow()
str.validate('a')
expect(throwFunc(str.validate('asd'))).toThrow()
})
})