Skip to content

Commit d0be50b

Browse files
committed
✅ test: abstractField.vue covered 100%
1 parent 81531fe commit d0be50b

File tree

3 files changed

+225
-22
lines changed

3 files changed

+225
-22
lines changed

src/fields/abstractField.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {each, isFunction, isArray, isUndefined} from "lodash";
1+
import {each, isFunction, isString, isArray, isUndefined} from "lodash";
22

33
export default {
44
props: [
@@ -40,6 +40,7 @@ export default {
4040

4141
watch: {
4242
value: function(newVal, oldVal) {
43+
//console.log("Changed", newVal, oldVal);
4344
if (isFunction(this.schema.onChanged)) {
4445
this.schema.onChanged(this.model, newVal, oldVal, this.schema);
4546
}
@@ -65,9 +66,12 @@ export default {
6566
}
6667

6768
each(validators, (validator) => {
68-
let err = validator(this.value, this.schema);
69-
if (err && err.length > 0) {
70-
Array.prototype.push.apply(this.schema.errors, err);
69+
let err = validator(this.value, this.schema, this.model);
70+
if (err) {
71+
if (isArray(err))
72+
Array.prototype.push.apply(this.schema.errors, err);
73+
else if (isString(err))
74+
this.schema.errors.push(err);
7175
}
7276
});
7377

test/unit/specs/fields/abstractField.spec.js

+217-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
//import chai from "chai";
21
import { expect } from "chai";
32

4-
//import sinon from "sinon";
5-
//import sinonChai from "sinon-chai";
6-
//chai.use(sinonChai);
7-
83
import Vue from "vue";
4+
import VueFormGenerator from "src/index";
95
import AbstractField from "src/fields/abstractField";
106

117
Vue.component("AbstractField", AbstractField);
128

139
let el, vm, field;
1410

15-
function createField(schema = {}, model = null, disabled = false) {
11+
function createField(schema = {}, model = null, disabled = false, options) {
1612
el = document.createElement("div");
1713
el.innerHTML = `<abstract-field :schema="schema" :model="model" :disabled="disabled" v-ref:field></abstract-field>`;
1814
vm = new Vue({
1915
el: el,
2016
data: {
2117
schema,
2218
model,
23-
disabled
19+
disabled,
20+
options
2421
}
2522
});
2623

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

5754
});
5855

59-
/*describe("check value as get/set function", () => {
56+
describe("check value as get/set function", () => {
6057
let schema = {
6158
type: "text",
6259
label: "Name",
6360
model: "name",
64-
get(model) {
65-
return "John Smith"
66-
},
67-
set: sinon.spy()
61+
get: sinon.stub().returns("John Smith"),
62+
set: sinon.stub()
6863
};
6964
let model = {};
7065

7166
beforeEach( () => {
7267
createField(schema, model);
7368
});
7469

75-
it("should give the model static value", () => {
70+
it("should be called the schema.get function", () => {
7671
expect(field).to.be.exist;
77-
expect(field.value).to.be.equal("John Doe");
72+
field.schema.get.reset();
73+
expect(field.value).to.be.equal("John Smith");
74+
expect(field.schema.get.calledOnce).to.be.true;
7875
});
7976

8077
it("should set new value to model if value changed", () => {
78+
field.schema.set.reset();
79+
field.value = "John Roe";
80+
expect(field.schema.set.calledOnce).to.be.true;
81+
expect(field.schema.set.calledWith(model, "John Roe")).to.be.true;
82+
});
83+
84+
});
85+
86+
describe("check formatValueToField & formatValueToModel function", () => {
87+
let schema = {
88+
type: "text",
89+
label: "Name",
90+
model: "name"
91+
};
92+
let model = { name: "John Doe" };
93+
94+
beforeEach( () => {
95+
createField(schema, model);
96+
field.formatValueToField = function(value) {
97+
return "**" + value + "**";
98+
};
99+
100+
field.formatValueToModel = function(value) {
101+
return "!!" + value + "!!";
102+
};
103+
});
104+
105+
it("should return the formatted value", () => {
106+
expect(field.value).to.be.equal("**John Doe**");
107+
});
108+
109+
it("should set the formatted value to model", () => {
81110
field.value = "Foo Bar";
82-
expect(model.name).to.be.equal("Foo Bar");
111+
expect(model.name).to.be.equal("!!Foo Bar!!");
112+
});
113+
114+
});
115+
116+
describe("check schema onChanged event", () => {
117+
let schema = {
118+
type: "text",
119+
label: "Name",
120+
model: "name",
121+
onChanged: sinon.spy()
122+
};
123+
let model = { name: "John Doe" };
124+
125+
beforeEach( () => {
126+
createField(schema, model);
127+
});
128+
129+
it("should called once the schema.onChanged", (done) => {
130+
schema.onChanged.reset();
131+
model.name = "Jane Doe";
132+
vm.$nextTick(() => {
133+
expect(schema.onChanged.calledOnce).to.be.true;
134+
expect(schema.onChanged.calledWith(model, "Jane Doe", "John Doe", schema)).to.be.true;
135+
done();
136+
})
137+
});
138+
139+
});
140+
141+
describe("check validateAfterChanged option", () => {
142+
let schema = {
143+
type: "text",
144+
label: "Name",
145+
model: "name"
146+
};
147+
148+
let model = { name: "John Doe" };
149+
let options = {
150+
validateAfterChanged: false
151+
}
152+
153+
beforeEach( () => {
154+
createField(schema, model, false, options);
155+
field.validate = sinon.spy();
156+
});
157+
158+
it("should not call validate function after value changed", (done) => {
159+
model.name = "Jane Doe";
160+
vm.$nextTick( () => {
161+
expect(field.validate.callCount).to.be.equal(0);
162+
done();
163+
});
164+
});
165+
166+
it("should not call validate function after value changed", (done) => {
167+
options.validateAfterChanged = true;
168+
model.name = "Jane Roe";
169+
vm.$nextTick( () => {
170+
expect(field.validate.callCount).to.be.equal(1);
171+
done();
172+
});
173+
});
174+
175+
});
176+
177+
describe("check validate function with one validator", () => {
178+
let schema = {
179+
type: "text",
180+
label: "Name",
181+
model: "name",
182+
validator: sinon.spy()
183+
};
184+
185+
let model = { name: "John Doe" };
186+
187+
beforeEach( () => {
188+
createField(schema, model);
189+
});
190+
191+
it("should call schema validator", () => {
192+
schema.validator.reset();
193+
field.validate();
194+
expect(schema.validator.calledOnce).to.be.true;
195+
expect(schema.validator.calledWith(field.value, schema, model)).to.be.true;
196+
});
197+
198+
});
199+
200+
describe("check validate function with validator array", () => {
201+
let spy1 = sinon.spy();
202+
let spy2 = sinon.spy();
203+
let schema = {
204+
type: "text",
205+
label: "Name",
206+
model: "name",
207+
validator: [spy1, spy2]
208+
};
209+
210+
let model = { name: "John Doe" };
211+
212+
beforeEach( () => {
213+
createField(schema, model);
214+
});
215+
216+
it("should call schema validator", () => {
217+
spy1.reset();
218+
spy2.reset();
219+
field.validate();
220+
221+
expect(spy1.calledOnce).to.be.true;
222+
expect(spy1.calledWith(field.value, schema, model)).to.be.true;
223+
224+
expect(spy2.calledOnce).to.be.true;
225+
expect(spy2.calledWith(field.value, schema, model)).to.be.true;
226+
});
227+
228+
});
229+
230+
describe("check schema onValidated event", () => {
231+
let schema = {
232+
type: "text",
233+
label: "Name",
234+
model: "name",
235+
validator: sinon.stub().returns("Validation error!"),
236+
onValidated: sinon.spy()
237+
};
238+
let model = { name: "John Doe" };
239+
240+
beforeEach( () => {
241+
createField(schema, model);
242+
});
243+
244+
it("should called once the schema.onValidated", () => {
245+
schema.onValidated.reset();
246+
let res = field.validate();
247+
expect(res).to.be.an.array;
248+
expect(res.length).to.be.equal(1);
249+
expect(res[0]).to.be.equal("Validation error!");
250+
251+
expect(schema.onValidated.calledOnce).to.be.true;
252+
expect(schema.onValidated.calledWith(model, field.schema.errors, schema)).to.be.true;
253+
});
254+
255+
});
256+
257+
describe("check clearValidationErrors", () => {
258+
let schema = {
259+
type: "text",
260+
label: "Name",
261+
model: "name",
262+
validator: sinon.stub().returns("Validation error!")
263+
};
264+
let model = { name: "John Doe" };
265+
266+
before( () => {
267+
createField(schema, model);
268+
});
269+
270+
it("should be undefined", () => {
271+
expect(schema.errors).to.be.undefined;
272+
});
273+
274+
it("should be an empty array", () => {
275+
field.clearValidationErrors();
276+
expect(schema.errors).to.be.defined;
277+
expect(schema.errors).to.be.length(0);
278+
});
279+
280+
it("should contain one error string", () => {
281+
field.validate();
282+
expect(schema.errors).to.be.length(1);
283+
expect(schema.errors[0]).to.be.equal("Validation error!");
83284
});
84285

85-
});*/
286+
});
86287

87288
});

test/unit/specs/index.spec.js

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ describe("module", () => {
66

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

9-
expect(true).to.be.true;
10-
119
expect(VueFormGenerator).to.be.exist;
1210
expect(VueFormGenerator).to.have.property("component");
1311
expect(VueFormGenerator).to.have.property("schema");

0 commit comments

Comments
 (0)