validator/types/number.test.js

46 lines
1.2 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()
throwFunc(num.validate(1))
throwFunc(num.validate(1.23))
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()
throwFunc(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()
throwFunc(num.validate(2))
expect(throwFunc(num.validate(3))).toThrow()
})
})