Skip to content

Commit

Permalink
test field validate event
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Feb 21, 2017
1 parent 0f83624 commit e3a31cf
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ div
// Child field executed validation
onFieldValidated(res, errors, field) {
this.errors = this.errors.filter(e => e.field == field.schema);
this.errors = this.errors.filter(e => e.field != field.schema);
// Remove old errors for this field
if (!res && errors && errors.length > 0) {
// Add errors with this field
Expand Down
86 changes: 86 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,90 @@ describe("VueFormGenerator.vue", () => {

});

describe("check onFieldValidated method if child validate", () => {
let schema = {
fields: [
{
type: "input",
inputType: "text",
label: "Name",
model: "name",
min: 3,
validator: VueFormGenerator.validators.string
},
{
type: "input",
inputType: "text",
label: "City",
model: "city",
validator() { return "Validation error!"; }
}
]
};

let model = { name: "Bob" };
let form;
let field;
let onValidated = sinon.spy();

before( (done) => {
let elm = document.createElement("div");
vm = new Vue({
// eslint-disable-next-line quotes
template: `<vue-form-generator :schema="schema" :model="model" :options="options" :multiple="false" ref="form" @validated="onValidated"></vue-form-generator>`,
data: {
schema,
model,
options: {}
},
methods: {
onValidated
}
}).$mount(elm);

el = vm.$el;
vm.$nextTick( () => {
form = vm.$refs.form;
field = form.$children[0];
done();
});
});

it("should no errors after mounted()", (done) => {
vm.$nextTick( () => {
expect(form.errors).to.be.length(0);
done();
});
});

it("should be validation error if model value is not valid", () => {
onValidated.reset();
vm.model.name = "A";
field.validate();

expect(form.errors).to.be.length(1);
expect(onValidated.callCount).to.be.equal(1);
// console.log(onValidated.getCall(0).args[1][0].field);
// console.log(schema.fields[0]);
expect(onValidated.calledWith(false, [{ field: schema.fields[0], error: "The length of text is too small! Current: 1, Minimum: 3"}] )).to.be.true;
});

it("should be 2 validation error", () => {
form.$children[1].validate();
expect(form.errors).to.be.length(2);
expect(form.errors[0].error).to.be.equal("The length of text is too small! Current: 1, Minimum: 3");
expect(form.errors[1].error).to.be.equal("Validation error!");
});

it("should only other field validation error", () => {
vm.model.name = "Alan";
onValidated.reset();
field.validate();

expect(form.errors).to.be.length(1);
expect(onValidated.callCount).to.be.equal(1);
expect(onValidated.calledWith(false, [{ field: schema.fields[1], error: "Validation error!"}] )).to.be.true;
});
});

});
63 changes: 63 additions & 0 deletions test/unit/specs/fields/abstractField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { expect } from "chai";

import Vue from "vue";
import VueFormGenerator from "src/index";
import AbstractField from "src/fields/abstractField";
AbstractField.template = "<div></div>";
Vue.component("AbstractField", AbstractField);
Expand Down Expand Up @@ -356,6 +357,68 @@ describe("abstractField.vue", function() {

});

describe("check schema onValidated event", () => {
let schema = {
type: "text",
label: "Name",
model: "name",
min: 3,
validator: VueFormGenerator.validators.string
};
let model = { name: "John Doe" };
let onValidated = sinon.spy();

beforeEach( () => {
let elm = document.createElement("div");

vm = new Vue({
// eslint-disable-next-line quotes
template: `<abstract-field :schema="schema" :model="model" ref="field" @validated="onValidated"></abstract-field>`,
data: {
schema,
model
},
methods: {
onValidated
}
}).$mount(elm);
el = vm.$el;

field = vm.$refs.field;
});

it("should return empty array", () => {
onValidated.reset();
let res = field.validate();
expect(res).to.be.an.array;
expect(res.length).to.be.equal(0);

expect(onValidated.callCount).to.be.equal(1);
expect(onValidated.calledWith(true, [])).to.be.true;
});

it("should not call 'onValidated'", () => {
onValidated.reset();
let res = field.validate(true);
expect(res).to.be.an.array;
expect(res.length).to.be.equal(0);

expect(onValidated.callCount).to.be.equal(0);
});

it("should return empty array", () => {
model.name = "Al";
onValidated.reset();
let res = field.validate();
expect(res).to.be.an.array;
expect(res.length).to.be.equal(1);
expect(res[0]).to.be.equal("The length of text is too small! Current: 2, Minimum: 3");

expect(onValidated.callCount).to.be.equal(1);
expect(onValidated.calledWith(false, field.errors, field)).to.be.true;
});
});

describe("check clearValidationErrors", () => {
let schema = {
type: "text",
Expand Down

0 comments on commit e3a31cf

Please sign in to comment.