forked from angular-ui/ui-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateSpec.js
189 lines (152 loc) · 7.33 KB
/
validateSpec.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
describe('uiValidate', function () {
'use strict';
var scope, compileAndDigest;
var trueValidator = function () {
return true;
};
var falseValidator = function () {
return false;
};
var passedValueValidator = function (valueToValidate) {
return valueToValidate;
};
beforeEach(module('ui.validate'));
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
compileAndDigest = function (inputHtml, scope) {
var inputElm = angular.element(inputHtml);
var formElm = angular.element('<form name="form"></form>');
formElm.append(inputElm);
$compile(formElm)(scope);
scope.$digest();
return inputElm;
};
}));
describe('initial validation', function () {
it('should mark input as valid if initial model is valid', inject(function () {
scope.validate = trueValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
expect(scope.form.input.$valid).toBeTruthy();
expect(scope.form.input.$error).toEqual({validator: false});
}));
it('should mark input as invalid if initial model is invalid', inject(function () {
scope.validate = falseValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
expect(scope.form.input.$valid).toBeFalsy();
expect(scope.form.input.$error).toEqual({ validator: true });
}));
it('should keep the invalid value in model if it is marked as invalid', inject(function () {
var testValue = 'test123';
scope.value = testValue;
scope.validate = falseValidator;
var inputElm = compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
expect(scope.form.input.$valid).toBeFalsy();
expect(scope.value).toEqual(testValue);
expect(inputElm.val()).toEqual(testValue);
}));
});
describe('validation on model change', function () {
it('should change valid state in response to model changes', inject(function () {
scope.value = false;
scope.validate = passedValueValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
expect(scope.form.input.$valid).toBeFalsy();
scope.$apply('value = true');
expect(scope.form.input.$valid).toBeTruthy();
}));
});
describe('validation on element change', function () {
var sniffer;
beforeEach(inject(function ($sniffer) {
sniffer = $sniffer;
}));
it('should change valid state in response to element events', function () {
scope.value = false;
scope.validate = passedValueValidator;
var inputElm = compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
expect(scope.form.input.$valid).toBeFalsy();
inputElm.val('true');
inputElm.trigger((sniffer.hasEvent('input') ? 'input' : 'change'));
expect(scope.form.input.$valid).toBeTruthy();
});
});
describe('multiple validators with custom keys', function () {
it('should support multiple validators with custom keys', function () {
scope.validate1 = trueValidator;
scope.validate2 = falseValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="{key1 : \'validate1($value)\', key2 : \'validate2($value)\'}">', scope);
expect(scope.form.input.$valid).toBeFalsy();
expect(scope.form.input.$error.key1).toBeFalsy();
expect(scope.form.input.$error.key2).toBeTruthy();
});
it('should show subsequent validation errors as false if the first of multiple validators fails, but the other validations do not fail', function () {
scope.validate1 = falseValidator;
scope.validate2 = trueValidator;
scope.validate3 = trueValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="{key1 : \'validate1($value)\', key2 : \'validate2($value)\', key3 : \'validate2($value)\'}">', scope);
expect(scope.form.input.$valid).toBeFalsy();
expect(scope.form.input.$error.key1).toBeTruthy();
expect(scope.form.input.$error.key2).toBeFalsy();
expect(scope.form.input.$error.key3).toBeFalsy();
});
});
describe('uiValidateWatch', function(){
function validateWatch(watchMe) {
return watchMe;
}
beforeEach(function(){
scope.validateWatch = validateWatch;
});
it('should watch the string and refire the single validator', function () {
scope.watchMe = false;
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validateWatch(watchMe)\'" ui-validate-watch="\'watchMe\'">', scope);
expect(scope.form.input.$valid).toBe(false);
expect(scope.form.input.$error.validator).toBe(true);
scope.$apply('watchMe=true');
expect(scope.form.input.$valid).toBe(true);
expect(scope.form.input.$error.validator).toBe(false);
});
it('should watch the string and refire all validators', function () {
scope.watchMe = false;
compileAndDigest('<input name="input" ng-model="value" ui-validate="{foo:\'validateWatch(watchMe)\',bar:\'validateWatch(watchMe)\'}" ui-validate-watch="\'watchMe\'">', scope);
expect(scope.form.input.$valid).toBe(false);
expect(scope.form.input.$error.foo).toBe(true);
expect(scope.form.input.$error.bar).toBe(true);
scope.$apply('watchMe=true');
expect(scope.form.input.$valid).toBe(true);
expect(scope.form.input.$error.foo).toBe(false);
expect(scope.form.input.$error.bar).toBe(false);
});
it('should watch the all object attributes and each respective validator', function () {
scope.watchFoo = false;
scope.watchBar = false;
compileAndDigest('<input name="input" ng-model="value" ui-validate="{foo:\'validateWatch(watchFoo)\',bar:\'validateWatch(watchBar)\'}" ui-validate-watch="{foo:\'watchFoo\',bar:\'watchBar\'}">', scope);
expect(scope.form.input.$valid).toBe(false);
expect(scope.form.input.$error.foo).toBe(true);
expect(scope.form.input.$error.bar).toBe(true);
scope.$apply('watchFoo=true');
expect(scope.form.input.$valid).toBe(false);
expect(scope.form.input.$error.foo).toBe(false);
expect(scope.form.input.$error.bar).toBe(true);
scope.$apply('watchBar=true');
scope.$apply('watchFoo=false');
expect(scope.form.input.$valid).toBe(false);
expect(scope.form.input.$error.foo).toBe(true);
expect(scope.form.input.$error.bar).toBe(false);
scope.$apply('watchFoo=true');
expect(scope.form.input.$valid).toBe(true);
expect(scope.form.input.$error.foo).toBe(false);
expect(scope.form.input.$error.bar).toBe(false);
});
});
describe('error cases', function () {
it('should fail if ngModel not present', inject(function () {
expect(function () {
compileAndDigest('<input name="input" ui-validate="\'validate($value)\'">', scope);
}).toThrow(new Error('No controller: ngModel'));
}));
it('should have no effect if validate expression is empty', inject(function () {
compileAndDigest('<input ng-model="value" ui-validate="">', scope);
}));
});
});