-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
61 lines (53 loc) · 1.5 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var QUnit = require('steal-qunit');
var makeValidator = require('can-validate-validatejs');
var constraints = {
age: {
numericality: {
message: 'should be a number'
}
},
name: {
presence: {
message: 'cannot be blank'
}
}
};
var invalidPerson = {
name: '',
age: 'hello'
};
var validPerson = {
name: 'Juan',
age: 35
};
QUnit.module('can-validate-validatejs');
QUnit.test('makeValidator sets errors',function(assert){
var validateAge = makeValidator(constraints.age);
var errors = validateAge(invalidPerson.age);
var expectedErrors = [constraints.age.numericality.message];
assert.deepEqual(errors, expectedErrors, 'returns expected errors object');
});
QUnit.test('makeValidator validates',function(assert){
var validateAge = makeValidator(constraints.age);
var errors = validateAge(validPerson.age);
assert.notOk(errors, 'value is valid, so no errors return');
});
QUnit.test('makeValidator.many sets errors',function(assert){
var validatePerson = makeValidator.many(constraints);
var errors = validatePerson(invalidPerson);
var expectedErrors = [
{
message: constraints.age.numericality.message,
related: ['age']
}, {
message: constraints.name.presence.message,
related: ['name']
}
];
assert.deepEqual(errors, expectedErrors, 'Many errors are set');
});
QUnit.test('makeValidator.many validates',function(assert){
var validatePerson = makeValidator.many(constraints);
var errors = validatePerson(validPerson);
assert.notOk(errors, 'values are valid, so no errors return');
});