api-cli/tests/lint.test.js

49 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-07-07 20:54:56 +02:00
const assert = require('assert').strict
const logging = require('../src/logging')
const expect = require('chai').expect
const linter = require('../src/linter')
logging.init(-1)
describe('linter validators', () => {
it('creates an error on required property', () => {
let msg = []
linter.validators.required()('path', '', msg)
linter.validators.required()('path', null, msg)
linter.validators.required()('path', undefined, msg)
linter.validators.required()('path', ' ', msg)
expect(msg).to.not.be.empty
expect(msg.length).to.equal(4)
})
it('creates an error on to small strings for length validator', () => {
let msg = []
linter.validators.length({ min: 4 })('path', '', msg)
linter.validators.length({ min: 4 })('path', null, msg)
linter.validators.length({ min: 4 })('path', undefined, msg)
linter.validators.length({ min: 4 })('path', '123', msg)
expect(msg).to.not.be.empty
expect(msg.length).to.equal(4)
})
it('creates an error on too large strings for length validator', () => {
let msg = []
linter.validators.length({ max: 4 })('path', '', msg)
linter.validators.length({ max: 4 })('path', null, msg)
linter.validators.length({ max: 4 })('path', undefined, msg)
linter.validators.length({ min: 4 })('path', '12345', msg)
linter.validators.length({ min: 4 })('path', ' 1 ', msg)
expect(msg).to.not.be.empty
expect(msg.length).to.equal(1)
})
it('creates an error when string does not match regex', () => {
let msg = []
linter.validators.regex(/[0-9]+/)('path', 'abcdef', msg)
linter.validators.regex(/[0-9]+/)('path', ' ', msg)
linter.validators.regex(/.+/)('path', ' ', msg)
expect(msg).to.not.be.empty
expect(msg.length).to.equal(3)
})
})