Skip to content

Commit

Permalink
add on-validated event to formGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Feb 21, 2017
1 parent 5ffcf20 commit 369b69f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<vue-form-generator :schema="schema" :model="model" :options="options" :multiple="false" ref="form" @on-validated="onValidated"></vue-form-generator>`,
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: [
Expand Down
5 changes: 3 additions & 2 deletions test/unit/webpack.test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}*/
}

};

0 comments on commit 369b69f

Please sign in to comment.