forked from Jakobovski/angular-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-validator-spec.js
More file actions
136 lines (93 loc) · 3.86 KB
/
Copy pathangular-validator-spec.js
File metadata and controls
136 lines (93 loc) · 3.86 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
describe('angularValidator', function() {
beforeEach(angular.mock.module('angularValidator'));
var scope, compile;
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
htmlForm = angular.element(
'<form name="myForm" angular-validator>' +
'<input ng-model="model.firstName" validate-on="dirty" name="firstName" type="text" required/>' +
'<input ng-model="model.lastName" validate-on="dirty"name="lastName" ng-pattern="/lastName/" invalid-message="\'WHOA\'" type="text" required/>' +
'<input ng-model="model.city" name="city" type="text" validate-on="dirty" validator="model.city === \'chicago\'" required/>' +
'<input ng-model="model.state" name="state" type="text" required/>' +
'</form>'
);
element = $compile(htmlForm)(scope);
scope.$digest();
}));
describe('$dirty tests', function() {
it('should be dirty', function() {
scope.myForm.lastName.$setViewValue('lastName');
scope.$digest();
expect(scope.myForm.lastName.$dirty).toBe(true);
expect(element.hasClass('ng-dirty')).toBe(true);
});
it('should not be $dirty', function() {
expect(scope.myForm.$pristine).toBe(true);
expect(element.hasClass('ng-pristine')).toBe(true);
});
});
describe('$valid tests', function() {
it('should be $valid', function() {
scope.myForm.lastName.$setViewValue('lastName');
scope.$digest();
expect(scope.myForm.lastName.$invalid).toBe(false);
});
it('should not be $valid', function() {
scope.myForm.lastName.$setViewValue('sss');
scope.$digest();
expect(scope.myForm.lastName.$invalid).toBe(true);
});
});
describe('custom validator tests', function() {
it('should be $valid', function() {
scope.myForm.city.$setViewValue('chicago');
scope.$digest();
expect(scope.myForm.city.$invalid).toBe(false);
});
it('should not be $valid', function() {
scope.myForm.city.$setViewValue('sss');
scope.$digest();
expect(scope.myForm.city.$invalid).toBe(true);
});
});
describe('.has-error class tests', function() {
it('should not not have class .has-error', function() {
expect(element.hasClass('has-error')).toBe(false);
expect(scope.myForm.$valid).toBe(false);
});
it('should not not have class .has-error', function() {
scope.myForm.firstName.$setViewValue('blah');
scope.myForm.lastName.$setViewValue('lastName');
scope.myForm.city.$setViewValue('chicago');
scope.myForm.state.$setViewValue('chicago');
scope.$digest();
expect(element.hasClass('has-error')).toBe(false);
expect(scope.myForm.$valid).toBe(true);
expect(scope.myForm.firstName.$valid).toBe(true);
});
it('should have the class .has-error', function() {
scope.myForm.lastName.$setViewValue('blah');
scope.$digest();
expect(scope.myForm.lastName.$valid).toBe(false);
expect(element.hasClass('has-error')).toBe(true);
expect(scope.myForm.$valid).toBe(false);
expect(element.hasClass('ng-invalid')).toBe(true);
});
});
describe('required and error messages test', function() {
it('should not display error or required message', function() {
expect(element.hasClass('has-error')).toBe(false);
expect(scope.myForm.$valid).toBe(false);
});
it('should show required message', function() {
scope.myForm.firstName.$setViewValue('');
scope.$digest();
expect(element[0][0].nextSibling.innerHTML.indexOf("Required") > 0).toBe(true);
});
it('should have the custom invalid message', function() {
scope.myForm.lastName.$setViewValue('blah');
scope.$digest();
expect(element[0][1].nextSibling.innerHTML === "WHOA").toBe(true);
});
});
});