Skip to content

Commit

Permalink
✅ test: abstractField.vue covered 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 9, 2016
1 parent 81531fe commit d0be50b
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 22 deletions.
12 changes: 8 additions & 4 deletions src/fields/abstractField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {each, isFunction, isArray, isUndefined} from "lodash";
import {each, isFunction, isString, isArray, isUndefined} from "lodash";

export default {
props: [
Expand Down Expand Up @@ -40,6 +40,7 @@ export default {

watch: {
value: function(newVal, oldVal) {
//console.log("Changed", newVal, oldVal);
if (isFunction(this.schema.onChanged)) {
this.schema.onChanged(this.model, newVal, oldVal, this.schema);
}
Expand All @@ -65,9 +66,12 @@ export default {
}

each(validators, (validator) => {
let err = validator(this.value, this.schema);
if (err && err.length > 0) {
Array.prototype.push.apply(this.schema.errors, err);
let err = validator(this.value, this.schema, this.model);
if (err) {
if (isArray(err))
Array.prototype.push.apply(this.schema.errors, err);
else if (isString(err))
this.schema.errors.push(err);
}
});

Expand Down
233 changes: 217 additions & 16 deletions test/unit/specs/fields/abstractField.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
//import chai from "chai";
import { expect } from "chai";

//import sinon from "sinon";
//import sinonChai from "sinon-chai";
//chai.use(sinonChai);

import Vue from "vue";
import VueFormGenerator from "src/index";
import AbstractField from "src/fields/abstractField";

Vue.component("AbstractField", AbstractField);

let el, vm, field;

function createField(schema = {}, model = null, disabled = false) {
function createField(schema = {}, model = null, disabled = false, options) {
el = document.createElement("div");
el.innerHTML = `<abstract-field :schema="schema" :model="model" :disabled="disabled" v-ref:field></abstract-field>`;
vm = new Vue({
el: el,
data: {
schema,
model,
disabled
disabled,
options
}
});

Expand Down Expand Up @@ -56,32 +53,236 @@ describe("abstractField", () => {

});

/*describe("check value as get/set function", () => {
describe("check value as get/set function", () => {
let schema = {
type: "text",
label: "Name",
model: "name",
get(model) {
return "John Smith"
},
set: sinon.spy()
get: sinon.stub().returns("John Smith"),
set: sinon.stub()
};
let model = {};

beforeEach( () => {
createField(schema, model);
});

it("should give the model static value", () => {
it("should be called the schema.get function", () => {
expect(field).to.be.exist;
expect(field.value).to.be.equal("John Doe");
field.schema.get.reset();
expect(field.value).to.be.equal("John Smith");
expect(field.schema.get.calledOnce).to.be.true;
});

it("should set new value to model if value changed", () => {
field.schema.set.reset();
field.value = "John Roe";
expect(field.schema.set.calledOnce).to.be.true;
expect(field.schema.set.calledWith(model, "John Roe")).to.be.true;
});

});

describe("check formatValueToField & formatValueToModel function", () => {
let schema = {
type: "text",
label: "Name",
model: "name"
};
let model = { name: "John Doe" };

beforeEach( () => {
createField(schema, model);
field.formatValueToField = function(value) {
return "**" + value + "**";
};

field.formatValueToModel = function(value) {
return "!!" + value + "!!";
};
});

it("should return the formatted value", () => {
expect(field.value).to.be.equal("**John Doe**");
});

it("should set the formatted value to model", () => {
field.value = "Foo Bar";
expect(model.name).to.be.equal("Foo Bar");
expect(model.name).to.be.equal("!!Foo Bar!!");
});

});

describe("check schema onChanged event", () => {
let schema = {
type: "text",
label: "Name",
model: "name",
onChanged: sinon.spy()
};
let model = { name: "John Doe" };

beforeEach( () => {
createField(schema, model);
});

it("should called once the schema.onChanged", (done) => {
schema.onChanged.reset();
model.name = "Jane Doe";
vm.$nextTick(() => {
expect(schema.onChanged.calledOnce).to.be.true;
expect(schema.onChanged.calledWith(model, "Jane Doe", "John Doe", schema)).to.be.true;
done();
})
});

});

describe("check validateAfterChanged option", () => {
let schema = {
type: "text",
label: "Name",
model: "name"
};

let model = { name: "John Doe" };
let options = {
validateAfterChanged: false
}

beforeEach( () => {
createField(schema, model, false, options);
field.validate = sinon.spy();
});

it("should not call validate function after value changed", (done) => {
model.name = "Jane Doe";
vm.$nextTick( () => {
expect(field.validate.callCount).to.be.equal(0);
done();
});
});

it("should not call validate function after value changed", (done) => {
options.validateAfterChanged = true;
model.name = "Jane Roe";
vm.$nextTick( () => {
expect(field.validate.callCount).to.be.equal(1);
done();
});
});

});

describe("check validate function with one validator", () => {
let schema = {
type: "text",
label: "Name",
model: "name",
validator: sinon.spy()
};

let model = { name: "John Doe" };

beforeEach( () => {
createField(schema, model);
});

it("should call schema validator", () => {
schema.validator.reset();
field.validate();
expect(schema.validator.calledOnce).to.be.true;
expect(schema.validator.calledWith(field.value, schema, model)).to.be.true;
});

});

describe("check validate function with validator array", () => {
let spy1 = sinon.spy();
let spy2 = sinon.spy();
let schema = {
type: "text",
label: "Name",
model: "name",
validator: [spy1, spy2]
};

let model = { name: "John Doe" };

beforeEach( () => {
createField(schema, model);
});

it("should call schema validator", () => {
spy1.reset();
spy2.reset();
field.validate();

expect(spy1.calledOnce).to.be.true;
expect(spy1.calledWith(field.value, schema, model)).to.be.true;

expect(spy2.calledOnce).to.be.true;
expect(spy2.calledWith(field.value, schema, model)).to.be.true;
});

});

describe("check schema onValidated event", () => {
let schema = {
type: "text",
label: "Name",
model: "name",
validator: sinon.stub().returns("Validation error!"),
onValidated: sinon.spy()
};
let model = { name: "John Doe" };

beforeEach( () => {
createField(schema, model);
});

it("should called once the schema.onValidated", () => {
schema.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("Validation error!");

expect(schema.onValidated.calledOnce).to.be.true;
expect(schema.onValidated.calledWith(model, field.schema.errors, schema)).to.be.true;
});

});

describe("check clearValidationErrors", () => {
let schema = {
type: "text",
label: "Name",
model: "name",
validator: sinon.stub().returns("Validation error!")
};
let model = { name: "John Doe" };

before( () => {
createField(schema, model);
});

it("should be undefined", () => {
expect(schema.errors).to.be.undefined;
});

it("should be an empty array", () => {
field.clearValidationErrors();
expect(schema.errors).to.be.defined;
expect(schema.errors).to.be.length(0);
});

it("should contain one error string", () => {
field.validate();
expect(schema.errors).to.be.length(1);
expect(schema.errors[0]).to.be.equal("Validation error!");
});

});*/
});

});
2 changes: 0 additions & 2 deletions test/unit/specs/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ describe("module", () => {

it("module properties", () => {

expect(true).to.be.true;

expect(VueFormGenerator).to.be.exist;
expect(VueFormGenerator).to.have.property("component");
expect(VueFormGenerator).to.have.property("schema");
Expand Down

0 comments on commit d0be50b

Please sign in to comment.