Skip to content

Commit

Permalink
add test for async validator
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Mar 28, 2017
1 parent 806080e commit e9463c2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
72 changes: 72 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,4 +893,76 @@ describe("VueFormGenerator.vue", () => {
});
});


describe("check async validator", () => {
let schema = {
fields: [
{
type: "input",
inputType: "text",
label: "Name",
model: "name",
validator(value) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (value.length >= 3)
resolve();
else
resolve([ "Invalid name" ]);
}, 50);
});
}
}
]
};

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", cb => {
onValidated.reset();
vm.model.name = "A";
field.validate();

setTimeout(() => {
expect(form.errors).to.be.length(1);
expect(onValidated.calledWith(false, [{ field: schema.fields[0], error: "Invalid name"}] )).to.be.true;

cb();
}, 100);
});
});

});
8 changes: 4 additions & 4 deletions test/unit/webpack.test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ module.exports = {
devtool: "eval-source-map",

module: {
preLoaders: [
/*preLoaders: [
{
test: /\.js$/,
loader: "isparta",
include: sourceDir,
exclude: /node_modules/
}
],
],*/

loaders: [
{
Expand Down Expand Up @@ -70,9 +70,9 @@ module.exports = {
},

// Comment out this, if you would like to debug under `npm run ci`
loaders: {
/*loaders: {
js: "isparta"
}
}*/
}

};

0 comments on commit e9463c2

Please sign in to comment.