api-cli/tests/lint.test.js

53 lines
2.4 KiB
JavaScript

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', () => {
expect(linter.validators.required()('path', '')).to.not.be.empty
expect(linter.validators.required()('path', null)).to.not.be.empty
expect(linter.validators.required()('path', undefined)).to.not.be.empty
expect(linter.validators.required()('path', ' ')).to.not.be.empty
})
it('creates an error on to small strings for length validator', () => {
expect(linter.validators.length({ min: 4 })('path', '')).to.not.be.empty
expect(linter.validators.length({ min: 4 })('path', null)).to.not.be.empty
expect(linter.validators.length({ min: 4 })('path', undefined)).to.not.be.empty
expect(linter.validators.length({ min: 4 })('path', '123')).to.not.be.empty
})
it('creates an error on too large strings for length validator', () => {
expect(linter.validators.length({ max: 4 })('path', '')).to.be.undefined
expect(linter.validators.length({ max: 4 })('path', null)).to.be.undefined
expect(linter.validators.length({ max: 4 })('path', undefined)).to.be.undefined
expect(linter.validators.length({ max: 4 })('path', '12345')).to.not.be.empty
expect(linter.validators.length({ max: 4 })('path', ' 1 ')).to.be.undefined
})
it('creates an error when string does not match regex', () => {
expect(linter.validators.regex(/[0-9]+/)('path', 'abcdef')).to.not.be.empty
expect(linter.validators.regex(/[0-9]+/)('path', ' ')).to.not.be.empty
expect(linter.validators.regex(/.+/)('path', ' ')).to.not.be.empty
})
})
describe('linter for spec', () => {
it('fails on invalid name property', () => {
let result = linter.lint({
name: "Test",
description: "Test."
})
expect(result.success).to.equal(false)
expect(result.name).to.not.equal('OK')
})
it('fails on invalid description property', () => {
let result = linter.lint({
name: "test-service",
description: "test."
})
expect(result.success).to.equal(false)
expect(result.description).to.not.equal('OK')
})
})