validator/types/number.test.js
2020-06-03 15:49:04 +00:00

44 lines
1.1 KiB
JavaScript

/* 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()
num.validate(1)
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()
num.validate(2)
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()
num.validate(2)
expect(throwFunc(num.validate(3))).toThrow()
})
})