Skip to content

Commit 1d4d898

Browse files
committed
✅ test: cover all source files
1 parent 3c132e3 commit 1d4d898

File tree

3 files changed

+201
-9
lines changed

3 files changed

+201
-9
lines changed

src/utils/validators.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ module.exports = {
3535

3636
if (!(Number(value) === value && value % 1 === 0))
3737
return ["Invalid number!"];
38-
39-
return [];
4038
},
4139

4240
double(value, field) {
4341
let res = checkEmpty(value, field.required); if (res != null) return res;
4442

4543
if (!(Number(value) === value && value % 1 !== 0))
4644
return ["Invalid number!"];
47-
48-
return [];
4945
},
5046

5147
string(value, field) {
@@ -153,12 +149,16 @@ module.exports = {
153149
alpha(value, field) {
154150
let res = checkEmpty(value, field.required); if (res != null) return res;
155151

156-
// TODO
152+
let re = /^[a-zA-Z]*$/;
153+
if (!re.test(value))
154+
return ["Invalid text! Cannot contains numbers or special characters"];
157155
},
158156

159157
alphaNumeric(value, field) {
160158
let res = checkEmpty(value, field.required); if (res != null) return res;
161159

162-
// TODO
160+
let re = /^[a-zA-Z0-9]*$/;
161+
if (!re.test(value))
162+
return ["Invalid text! Cannot contains special characters"];
163163
}
164164
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function createField(schema = {}, model = null, disabled = false, options) {
1313
[ el, vm, field ] = createVueField("fieldDateTime", schema, model, disabled, options);
1414
}
1515

16-
describe.only("fieldDateTime.vue", () => {
16+
describe("fieldDateTime.vue", () => {
1717

1818
describe("check template", () => {
1919
let schema = {

test/unit/specs/utils/validators.spec.js

+194-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import v from "src/utils/validators";
44

55
function check(validator, value, field, errorCount) {
66
let res = validator(value, field);
7-
expect(res).to.be.instanceof(Array);
8-
expect(res).to.be.length(errorCount);
7+
if (errorCount > 0 || res != undefined) {
8+
expect(res).to.be.instanceof(Array);
9+
expect(res).to.be.length(errorCount);
10+
}
911
return res;
1012
}
1113

@@ -107,6 +109,12 @@ describe("Validators", () => {
107109
it("should give error if value is greater than max", () => {
108110
check(v.string, "abcdefghijklmnop", field, 1);
109111
});
112+
113+
it("should give error if value is not string", () => {
114+
check(v.string, 123, field, 1);
115+
check(v.string, true, field, 1);
116+
check(v.string, [], field, 1);
117+
});
110118

111119
it("should not give error", () => {
112120
check(v.string, "Foo", field, 0);
@@ -148,6 +156,190 @@ describe("Validators", () => {
148156
check(v.date, "2100-04-05", field, 1);
149157
});
150158

159+
it("should give error if value is not a date", () => {
160+
check(v.date, "Foo", field, 1);
161+
check(v.date, true, field, 1);
162+
});
163+
164+
it("should not give error if value is null and field is not required", () => {
165+
field.required = false;
166+
check(v.date, null, field, 0);
167+
});
168+
169+
});
170+
171+
describe("test Validators.regexp", () => {
172+
173+
let field = {
174+
required: true,
175+
pattern: /^[a-z0-9-]+$/g
176+
}
177+
178+
it("should give error if value is null, but field is required", () => {
179+
check(v.regexp, null, field, 1);
180+
});
181+
182+
it("should give error if value is not matched the pattern", () => {
183+
check(v.regexp, "ABCD", field, 1);
184+
check(v.regexp, "12 34", field, 1);
185+
check(v.regexp, "555+666", field, 1);
186+
});
187+
188+
it("should not give error", () => {
189+
check(v.regexp, "foo-bar", field, 0);
190+
check(v.regexp, "john-doe-123", field, 0);
191+
});
192+
193+
it("should not give error if value is null and field is not required", () => {
194+
field.required = false;
195+
check(v.regexp, null, field, 0);
196+
});
197+
});
198+
199+
describe("test Validators.email", () => {
200+
201+
let field = { required: true };
202+
203+
it("should give error if value is null, but field is required", () => {
204+
check(v.email, null, field, 1);
205+
});
206+
207+
it("should give error if value is not matched the pattern", () => {
208+
check(v.email, "abcdefg", field, 1);
209+
check(v.email, "1234", field, 1);
210+
check(v.email, "abc@gmail", field, 1);
211+
check(v.email, "@gmail.com", field, 1);
212+
});
213+
214+
it("should not give error", () => {
215+
check(v.email, "[email protected]", field, 0);
216+
check(v.email, "[email protected]", field, 0);
217+
});
218+
219+
it("should not give error if value is null and field is not required", () => {
220+
field.required = false;
221+
check(v.email, null, field, 0);
222+
});
223+
});
224+
225+
226+
describe("test Validators.url", () => {
227+
228+
let field = { required: true };
229+
230+
it("should give error if value is null, but field is required", () => {
231+
check(v.url, null, field, 1);
232+
});
233+
234+
it("should give error if value is not matched the pattern", () => {
235+
check(v.url, "abcdefg", field, 1);
236+
check(v.url, "1234.c", field, 1);
237+
check(v.url, "gmail.company1234", field, 1);
238+
check(v.url, "@gmail.com", field, 1);
239+
});
240+
241+
it("should not give error", () => {
242+
check(v.url, "http://www.google.com", field, 0);
243+
check(v.url, "http://nasa.gov", field, 0);
244+
check(v.url, "http://github.com", field, 0);
245+
check(v.url, "http://github.com/icebob/vue-form-generator", field, 0);
246+
});
247+
248+
it("should not give error if value is null and field is not required", () => {
249+
field.required = false;
250+
check(v.url, null, field, 0);
251+
});
252+
});
253+
254+
describe("test Validators.creditCard", () => {
255+
256+
let field = { required: true };
257+
258+
it("should give error if value is null, but field is required", () => {
259+
check(v.creditCard, null, field, 1);
260+
});
261+
262+
it("should give error if value is not matched the pattern", () => {
263+
check(v.creditCard, "12345679", field, 1);
264+
check(v.creditCard, "4556778266680579000", field, 1);
265+
});
266+
267+
it("should not give error", () => {
268+
check(v.creditCard, "4556778266680579", field, 0); // Visa
269+
check(v.creditCard, "5491345312191350", field, 0); // Mastercard
270+
check(v.creditCard, "6011319767119926", field, 0); // Discover
271+
check(v.creditCard, "343811242956601", field, 0); // American Express
272+
});
273+
274+
it("should not give error if value is null and field is not required", () => {
275+
field.required = false;
276+
check(v.creditCard, null, field, 0);
277+
});
278+
});
279+
280+
describe("test Validators.alpha", () => {
281+
282+
let field = {
283+
required: true
284+
}
285+
286+
it("should give error if value is null, but field is required", () => {
287+
check(v.alpha, null, field, 1);
288+
});
289+
290+
it("should give error if value is not alpha", () => {
291+
check(v.alpha, "Abc5", field, 1);
292+
check(v.alpha, "$Abc", field, 1);
293+
check(v.alpha, "john.doe", field, 1);
294+
check(v.alpha, "john_doe", field, 1);
295+
check(v.alpha, 512, field, 1);
296+
});
297+
298+
it("should not give error", () => {
299+
check(v.alpha, "F", field, 0);
300+
check(v.alpha, "Foo", field, 0);
301+
check(v.alpha, "Foobar", field, 0);
302+
check(v.alpha, "JohnDoe", field, 0);
303+
check(v.alpha, "FoobarWithoutNumber", field, 0);
304+
});
305+
306+
it("should not give error if value is null and field is not required", () => {
307+
field.required = false;
308+
check(v.alpha, null, field, 0);
309+
});
151310
});
152311

312+
describe("test Validators.alphaNumeric", () => {
313+
314+
let field = {
315+
required: true
316+
}
317+
318+
it("should give error if value is null, but field is required", () => {
319+
check(v.alphaNumeric, null, field, 1);
320+
});
321+
322+
it("should give error if value is not alphaNumeric", () => {
323+
check(v.alphaNumeric, "Abc-5", field, 1);
324+
check(v.alphaNumeric, "$Abc", field, 1);
325+
check(v.alphaNumeric, "john.doe", field, 1);
326+
check(v.alphaNumeric, "john_doe", field, 1);
327+
});
328+
329+
it("should not give error", () => {
330+
check(v.alphaNumeric, "F", field, 0);
331+
check(v.alphaNumeric, "Foo", field, 0);
332+
check(v.alphaNumeric, "Foobar", field, 0);
333+
check(v.alphaNumeric, "Foobar555", field, 0);
334+
check(v.alphaNumeric, "JohnDoe", field, 0);
335+
check(v.alphaNumeric, "FoobarWithoutNumber", field, 0);
336+
check(v.alphaNumeric, "100", field, 0);
337+
check(v.alphaNumeric, 512, field, 0);
338+
});
339+
340+
it("should not give error if value is null and field is not required", () => {
341+
field.required = false;
342+
check(v.alphaNumeric, null, field, 0);
343+
});
344+
});
153345
});

0 commit comments

Comments
 (0)