From e3a31cf44a169408824eb169716c82f1cfca71b0 Mon Sep 17 00:00:00 2001 From: Icebob Date: Tue, 21 Feb 2017 16:16:15 +0100 Subject: [PATCH] test field validate event --- src/formGenerator.vue | 3 +- test/unit/specs/VueFormGenerator.spec.js | 86 ++++++++++++++++++++ test/unit/specs/fields/abstractField.spec.js | 63 ++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) diff --git a/src/formGenerator.vue b/src/formGenerator.vue index d95dfc79..f042989b 100644 --- a/src/formGenerator.vue +++ b/src/formGenerator.vue @@ -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 diff --git a/test/unit/specs/VueFormGenerator.spec.js b/test/unit/specs/VueFormGenerator.spec.js index 39b2d350..a4fdc0ef 100644 --- a/test/unit/specs/VueFormGenerator.spec.js +++ b/test/unit/specs/VueFormGenerator.spec.js @@ -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: ``, + 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; + }); + }); + }); \ No newline at end of file diff --git a/test/unit/specs/fields/abstractField.spec.js b/test/unit/specs/fields/abstractField.spec.js index 7b1a661a..1426682c 100644 --- a/test/unit/specs/fields/abstractField.spec.js +++ b/test/unit/specs/fields/abstractField.spec.js @@ -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 = "
"; Vue.component("AbstractField", AbstractField); @@ -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: ``, + 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",