Skip to content

Commit 230f98f

Browse files
committed
End testing for value validator
1 parent ab3c233 commit 230f98f

File tree

2 files changed

+189
-10
lines changed

2 files changed

+189
-10
lines changed

lib/validators/value.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function isValid(data, model) {
2525
// If object, return util.compare. Else, return false.
2626
return (typeof data === 'object') ? util.equal(data, model.value.eq) : false;
2727
}
28+
2829
// Different types: Try to convert one to other.
2930
var parsed = data;
3031
var type = typeof (model.value.eq);
@@ -56,8 +57,13 @@ function isValid(data, model) {
5657
}
5758

5859
// Data does not contain something
59-
if (model.value.contains && data.indexOf(model.value.contains) === -1) {
60-
return false;
60+
if (model.value.contains) {
61+
var testData = data;
62+
if(typeof data === 'number') {
63+
testData = data.toString();
64+
}
65+
66+
return (testData.indexOf(model.value.contains) !== -1);
6167
}
6268

6369
return true;

spec/validators/value.spec.js

Lines changed: 181 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const valid = require('../../lib/validators/value');
2+
const util = require('../../lib/util');
3+
4+
// Update tests
25

36
const str = 'Hello world';
4-
// const obj = {hello: 'world'};
5-
const num = 42;
7+
const obj = {hello: 'world'};
68
const bool = true;
9+
const num = 42;
710

811
const types = {
912
string: str,
10-
// object: obj,
13+
boolean: bool,
14+
// object: util.clone(obj),
15+
object: obj,
1116
number: num
1217
};
1318
const typesKeys = Object.keys(types);
@@ -40,8 +45,9 @@ describe(' - Date validator', () => {
4045
};
4146

4247
beforeEach(function () {
48+
const val = (typesKeys[validType] === 'object') ? util.clone(obj) : types[typesKeys[validType]];
4349
model.value = {
44-
eq: types[typesKeys[validType]]
50+
eq: val
4551
};
4652
});
4753

@@ -55,15 +61,182 @@ describe(' - Date validator', () => {
5561
});
5662

5763
describe('max filter', () => {
58-
// ToDo: TEST MAX FILTER
64+
65+
it('shall understand a number', () => {
66+
const model = {
67+
value: {
68+
max: num
69+
}
70+
};
71+
const result = valid(num, model);
72+
expect(result).toEqual(true);
73+
});
74+
75+
it('shall do nothing with anything else', () => {
76+
const model = {
77+
value: {
78+
max: 'num'
79+
}
80+
};
81+
const result = valid(num, model);
82+
expect(result).toEqual(true);
83+
});
84+
85+
it('shall allow numbers lower than expected', () => {
86+
const model = {
87+
value: {
88+
max: num
89+
}
90+
};
91+
const result = valid((num - 1), model);
92+
expect(result).toEqual(true);
93+
});
94+
95+
it('shall allow numbers equal than expected', () => {
96+
const model = {
97+
value: {
98+
max: num
99+
}
100+
};
101+
const result = valid(num, model);
102+
expect(result).toEqual(true);
103+
});
104+
105+
it('shall NOT allow numbers higher than expected', () => {
106+
const model = {
107+
value: {
108+
max: num
109+
}
110+
};
111+
const result = valid((num + 1), model);
112+
expect(result).toEqual(false);
113+
});
59114
});
60115

61116
describe('min filter', () => {
62-
// ToDo: TEST MIN FILTER
117+
118+
it('shall understand a number', () => {
119+
const model = {
120+
value: {
121+
min: num
122+
}
123+
};
124+
const result = valid(num, model);
125+
expect(result).toEqual(true);
126+
});
127+
128+
it('shall do nothing with anything else', () => {
129+
const model = {
130+
value: {
131+
min: 'num'
132+
}
133+
};
134+
const result = valid(num, model);
135+
expect(result).toEqual(true);
136+
});
137+
138+
it('shall NOT allow numbers lower than expected', () => {
139+
const model = {
140+
value: {
141+
min: num
142+
}
143+
};
144+
const result = valid((num - 1), model);
145+
expect(result).toEqual(false);
146+
});
147+
148+
it('shall allow numbers equal than expected', () => {
149+
const model = {
150+
value: {
151+
min: num
152+
}
153+
};
154+
const result = valid(num, model);
155+
expect(result).toEqual(true);
156+
});
157+
158+
it('shall allow numbers higher than expected', () => {
159+
const model = {
160+
value: {
161+
min: num
162+
}
163+
};
164+
const result = valid((num + 1), model);
165+
expect(result).toEqual(true);
166+
});
63167
});
64168

65169
describe('contains filter', () => {
66-
// ToDo: TEST CONTAINS FILTER
67-
});
170+
171+
it('shall validate a equal string', () => {
172+
const model = {
173+
value: {
174+
contains: str
175+
}
176+
};
177+
const result = valid(str, model);
178+
expect(result).toEqual(true);
179+
});
180+
181+
it('shall understand a number', () => {
182+
const model = {
183+
value: {
184+
contains: num
185+
}
186+
};
187+
const result = valid(num*100, model);
188+
expect(result).toEqual(true);
189+
});
190+
191+
it('shall validate a sufix string', () => {
192+
const model = {
193+
value: {
194+
contains: str
195+
}
196+
};
197+
const result = valid('hello' + str, model);
198+
expect(result).toEqual(true);
199+
});
68200

201+
it('shall validate a prefix string', () => {
202+
const model = {
203+
value: {
204+
contains: str
205+
}
206+
};
207+
const result = valid(str + 'world', model);
208+
expect(result).toEqual(true);
209+
});
210+
211+
it('shall validate a interfix string', () => {
212+
const model = {
213+
value: {
214+
contains: str
215+
}
216+
};
217+
const result = valid('hello' + str + 'world', model);
218+
expect(result).toEqual(true);
219+
});
220+
221+
it('shall NOT validate an false string', () => {
222+
const model = {
223+
value: {
224+
contains: str
225+
}
226+
};
227+
const result = valid(str.substr(1), model);
228+
expect(result).toEqual(false);
229+
});
230+
231+
it('shall NOT validate an empty string', () => {
232+
const model = {
233+
value: {
234+
contains: str
235+
}
236+
};
237+
const result = valid('', model);
238+
expect(result).toEqual(false);
239+
});
240+
241+
});
69242
});

0 commit comments

Comments
 (0)