Skip to content

Commit e3a31cf

Browse files
committed
test field validate event
1 parent 0f83624 commit e3a31cf

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

src/formGenerator.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ div
207207
208208
// Child field executed validation
209209
onFieldValidated(res, errors, field) {
210-
this.errors = this.errors.filter(e => e.field == field.schema);
210+
this.errors = this.errors.filter(e => e.field != field.schema);
211+
211212
// Remove old errors for this field
212213
if (!res && errors && errors.length > 0) {
213214
// Add errors with this field

test/unit/specs/VueFormGenerator.spec.js

+86
Original file line numberDiff line numberDiff line change
@@ -764,4 +764,90 @@ describe("VueFormGenerator.vue", () => {
764764

765765
});
766766

767+
describe("check onFieldValidated method if child validate", () => {
768+
let schema = {
769+
fields: [
770+
{
771+
type: "input",
772+
inputType: "text",
773+
label: "Name",
774+
model: "name",
775+
min: 3,
776+
validator: VueFormGenerator.validators.string
777+
},
778+
{
779+
type: "input",
780+
inputType: "text",
781+
label: "City",
782+
model: "city",
783+
validator() { return "Validation error!"; }
784+
}
785+
]
786+
};
787+
788+
let model = { name: "Bob" };
789+
let form;
790+
let field;
791+
let onValidated = sinon.spy();
792+
793+
before( (done) => {
794+
let elm = document.createElement("div");
795+
vm = new Vue({
796+
// eslint-disable-next-line quotes
797+
template: `<vue-form-generator :schema="schema" :model="model" :options="options" :multiple="false" ref="form" @validated="onValidated"></vue-form-generator>`,
798+
data: {
799+
schema,
800+
model,
801+
options: {}
802+
},
803+
methods: {
804+
onValidated
805+
}
806+
}).$mount(elm);
807+
808+
el = vm.$el;
809+
vm.$nextTick( () => {
810+
form = vm.$refs.form;
811+
field = form.$children[0];
812+
done();
813+
});
814+
});
815+
816+
it("should no errors after mounted()", (done) => {
817+
vm.$nextTick( () => {
818+
expect(form.errors).to.be.length(0);
819+
done();
820+
});
821+
});
822+
823+
it("should be validation error if model value is not valid", () => {
824+
onValidated.reset();
825+
vm.model.name = "A";
826+
field.validate();
827+
828+
expect(form.errors).to.be.length(1);
829+
expect(onValidated.callCount).to.be.equal(1);
830+
// console.log(onValidated.getCall(0).args[1][0].field);
831+
// console.log(schema.fields[0]);
832+
expect(onValidated.calledWith(false, [{ field: schema.fields[0], error: "The length of text is too small! Current: 1, Minimum: 3"}] )).to.be.true;
833+
});
834+
835+
it("should be 2 validation error", () => {
836+
form.$children[1].validate();
837+
expect(form.errors).to.be.length(2);
838+
expect(form.errors[0].error).to.be.equal("The length of text is too small! Current: 1, Minimum: 3");
839+
expect(form.errors[1].error).to.be.equal("Validation error!");
840+
});
841+
842+
it("should only other field validation error", () => {
843+
vm.model.name = "Alan";
844+
onValidated.reset();
845+
field.validate();
846+
847+
expect(form.errors).to.be.length(1);
848+
expect(onValidated.callCount).to.be.equal(1);
849+
expect(onValidated.calledWith(false, [{ field: schema.fields[1], error: "Validation error!"}] )).to.be.true;
850+
});
851+
});
852+
767853
});

test/unit/specs/fields/abstractField.spec.js

+63
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { expect } from "chai";
33

44
import Vue from "vue";
5+
import VueFormGenerator from "src/index";
56
import AbstractField from "src/fields/abstractField";
67
AbstractField.template = "<div></div>";
78
Vue.component("AbstractField", AbstractField);
@@ -356,6 +357,68 @@ describe("abstractField.vue", function() {
356357

357358
});
358359

360+
describe("check schema onValidated event", () => {
361+
let schema = {
362+
type: "text",
363+
label: "Name",
364+
model: "name",
365+
min: 3,
366+
validator: VueFormGenerator.validators.string
367+
};
368+
let model = { name: "John Doe" };
369+
let onValidated = sinon.spy();
370+
371+
beforeEach( () => {
372+
let elm = document.createElement("div");
373+
374+
vm = new Vue({
375+
// eslint-disable-next-line quotes
376+
template: `<abstract-field :schema="schema" :model="model" ref="field" @validated="onValidated"></abstract-field>`,
377+
data: {
378+
schema,
379+
model
380+
},
381+
methods: {
382+
onValidated
383+
}
384+
}).$mount(elm);
385+
el = vm.$el;
386+
387+
field = vm.$refs.field;
388+
});
389+
390+
it("should return empty array", () => {
391+
onValidated.reset();
392+
let res = field.validate();
393+
expect(res).to.be.an.array;
394+
expect(res.length).to.be.equal(0);
395+
396+
expect(onValidated.callCount).to.be.equal(1);
397+
expect(onValidated.calledWith(true, [])).to.be.true;
398+
});
399+
400+
it("should not call 'onValidated'", () => {
401+
onValidated.reset();
402+
let res = field.validate(true);
403+
expect(res).to.be.an.array;
404+
expect(res.length).to.be.equal(0);
405+
406+
expect(onValidated.callCount).to.be.equal(0);
407+
});
408+
409+
it("should return empty array", () => {
410+
model.name = "Al";
411+
onValidated.reset();
412+
let res = field.validate();
413+
expect(res).to.be.an.array;
414+
expect(res.length).to.be.equal(1);
415+
expect(res[0]).to.be.equal("The length of text is too small! Current: 2, Minimum: 3");
416+
417+
expect(onValidated.callCount).to.be.equal(1);
418+
expect(onValidated.calledWith(false, field.errors, field)).to.be.true;
419+
});
420+
});
421+
359422
describe("check clearValidationErrors", () => {
360423
let schema = {
361424
type: "text",

0 commit comments

Comments
 (0)