semisleep / simple-vue-validator

A simple yet flexible validator library for vue.js
MIT License
293 stars 28 forks source link

You should be using some sort of test framework. #40

Closed bdsoha closed 1 year ago

bdsoha commented 6 years ago

The project is missing a test suite and therefore lacks user trust. I have put together a simple idea of how the tests should be structured.

import {expect} from 'chai'
import Rule from '../src/rule'

function makeRule(value) {
    return new Rule().value(value)
}

function hasErrors(rule) {
    return rule._messages.length > 0
}

describe('Rules', () => {
    describe('integer', () => {
        it('string equivalents are considered integers', () => {
            expect(hasErrors(makeRule('1').integer())).to.be.false
            expect(hasErrors(makeRule('0').integer())).to.be.false
            expect(hasErrors(makeRule('-1').integer())).to.be.false
        })

        it('will fail if a float value is provided', () => {
            expect(hasErrors(makeRule('1.1').integer())).to.be.true
            expect(hasErrors(makeRule('0.').integer())).to.be.true
        })

        it('empty values are considered invalid', () => {
            expect(hasErrors(makeRule(null).integer())).to.be.true
        })
    })
})

Let me know if this is something you would want to expand upon and can even help write and run the required tests.

bdsoha commented 6 years ago

You can even add an optional assertion to make the tests more readable:

utils.addProperty(chai.Assertion.prototype, 'errors', function () {
    this.assert(
        this._obj._messages.length > 0,
        'expected #{this} to have errors',
        'expected #{this} to not have errors'
    )
})

it('string equivalents are considered integers', () => {
    expect(makeRule('1').integer()).to.not.have.errors
    expect(makeRule('0').integer()).to.not.have.errors
    expect(makeRule('-1').integer()).to.not.have.errors
})
semisleep commented 6 years ago

Hi, thank you for your suggestion!

Frankly speaking, I don't have more time to write the test cases at this moment. Will you be interested to contribute to this project?

Thanks

bdsoha commented 6 years ago

@semisleep I already made a sample PR #41, take a look and let me know if it is something you are interested in.

bdsoha commented 1 year ago

Seems like the package is inactive.