-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.spec.js
More file actions
67 lines (54 loc) · 1.97 KB
/
validate.spec.js
File metadata and controls
67 lines (54 loc) · 1.97 KB
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
62
63
64
65
66
67
describe('dbrans.validate', function() {
var $compile;
var $rootScope;
var $q;
beforeEach(module('dbrans.validate'));
beforeEach(inject(function (_$compile_, _$rootScope_, _$q_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$q = _$q_;
}));
it('supports synchronous validation', function() {
scope = $rootScope.$new();
scope.isOdd = function(value) {
return value % 2 === 1;
};
var html = '<input type="number" min="1" ng-model="foo"' +
' dbrans-validate="{odd: isOdd}">';
var input = angular.element(html);
$compile(input)(scope);
var ngModelCtrl = input.controller('ngModel');
ngModelCtrl.$setViewValue(0);
expect(ngModelCtrl.$error.odd).toBe(true);
expect(ngModelCtrl.$error.min).toBe(true);
ngModelCtrl.$setViewValue(1);
expect(ngModelCtrl.$error.odd).toBeFalsy();
expect(ngModelCtrl.$error.min).toBeFalsy();
ngModelCtrl.$setViewValue(2);
expect(ngModelCtrl.$error.odd).toBe(true);
expect(ngModelCtrl.$error.min).toBeFalsy();
});
it('supports asynchronous validation', function() {
scope = $rootScope.$new();
scope.isOddAsync = function(value) {
return $q(function(resolve, reject) {
value % 2 === 1 ? resolve() : reject();
});
};
var html = '<input type="number" min="1" ng-model="foo"' +
' dbrans-validate-async="{odd: isOddAsync}">';
var input = angular.element(html);
$compile(input)(scope);
var ngModelCtrl = input.controller('ngModel');
ngModelCtrl.$setViewValue(0);
// Async validation doesn't happen if sync validation fails.
expect(ngModelCtrl.$error.odd).toBeFalsy();
expect(ngModelCtrl.$error.min).toBe(true);
ngModelCtrl.$setViewValue(1);
expect(ngModelCtrl.$error.odd).toBeFalsy();
expect(ngModelCtrl.$error.min).toBeFalsy();
ngModelCtrl.$setViewValue(2);
expect(ngModelCtrl.$error.odd).toBe(true);
expect(ngModelCtrl.$error.min).toBeFalsy();
});
});