From 369b69fb8bcb4515c70376a092e8d150dd396957 Mon Sep 17 00:00:00 2001 From: Icebob Date: Tue, 21 Feb 2017 14:54:06 +0100 Subject: [PATCH] add `on-validated` event to formGenerator --- src/formGenerator.vue | 4 +- test/unit/specs/VueFormGenerator.spec.js | 70 ++++++++++++++++++++++++ test/unit/webpack.test.config.js | 5 +- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/formGenerator.vue b/src/formGenerator.vue index 6ac54ba2..3b84e549 100644 --- a/src/formGenerator.vue +++ b/src/formGenerator.vue @@ -222,7 +222,9 @@ div } }); - return this.errors.length == 0; + let isValid = this.errors.length == 0; + this.$emit('on-validated', isValid, this.errors); + return isValid; }, // Clear validation errors diff --git a/test/unit/specs/VueFormGenerator.spec.js b/test/unit/specs/VueFormGenerator.spec.js index bc9527cf..184589e9 100644 --- a/test/unit/specs/VueFormGenerator.spec.js +++ b/test/unit/specs/VueFormGenerator.spec.js @@ -629,6 +629,76 @@ describe("VueFormGenerator.vue", () => { }); + describe.only("check onValidated event", () => { + let schema = { + fields: [ + { + type: "input", + inputType: "text", + label: "Name", + model: "name", + min: 3, + validator: VueFormGenerator.validators.string + } + ] + }; + + let model = { name: "Bob" }; + let form; + 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; + 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", () => { + vm.model.name = "A"; + onValidated.reset(); + form.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 no validation error if model valie is valid", () => { + vm.model.name = "Alan"; + onValidated.reset(); + form.validate(); + + expect(form.errors).to.be.length(0); + expect(onValidated.callCount).to.be.equal(1); + expect(onValidated.calledWith(true, [] )).to.be.true; + }); + }); + describe("check schema.onChanged when the model changed", () => { let schema = { fields: [ diff --git a/test/unit/webpack.test.config.js b/test/unit/webpack.test.config.js index 6c905f99..2a6299c2 100644 --- a/test/unit/webpack.test.config.js +++ b/test/unit/webpack.test.config.js @@ -68,9 +68,10 @@ module.exports = { autoprefixer: { browsers: ["last 2 versions"] }, - loaders: { + // Comment this, if you would like to debug under `npm run ci` + /*loaders: { js: "isparta" - } + }*/ } };