2020-06-03 15:49:04 +00:00
|
|
|
/* eslint-disable no-undef */
|
|
|
|
const TypeNumber = require('./number.js')
|
|
|
|
|
|
|
|
describe('test validate schema type number', () => {
|
|
|
|
function throwFunc (val) {
|
|
|
|
return () => {
|
|
|
|
if (val) throw new Error(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it('test type number with required', async () => {
|
|
|
|
const num = new TypeNumber().required()
|
|
|
|
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(num.validate(1))
|
|
|
|
|
|
|
|
throwFunc(num.validate(1.23))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
expect(throwFunc(num.validate(undefined))).toThrow()
|
|
|
|
expect(throwFunc(num.validate({}))).toThrow()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('test type number with min', async () => {
|
|
|
|
const num = new TypeNumber().required().min(1)
|
|
|
|
|
|
|
|
expect(() => new TypeNumber().min()).toThrow()
|
|
|
|
expect(() => new TypeNumber().min(false)).toThrow()
|
|
|
|
expect(() => new TypeNumber().max(1).min(2)).toThrow()
|
|
|
|
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(num.validate(2))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
expect(throwFunc(num.validate(-1))).toThrow()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('test type number with max', async () => {
|
|
|
|
const num = new TypeNumber().required().max(2)
|
|
|
|
|
|
|
|
expect(() => new TypeNumber().max()).toThrow()
|
|
|
|
expect(() => new TypeNumber().max(false)).toThrow()
|
|
|
|
expect(() => new TypeNumber().min(3).max(2)).toThrow()
|
|
|
|
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(num.validate(2))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
expect(throwFunc(num.validate(3))).toThrow()
|
|
|
|
})
|
|
|
|
})
|