|
1 | 1 | (function() { |
2 | | - var expectFirstNameFormGroupHasSuccessClass, expectFormGroupHasErrorClass, expectLastNameFormGroupHasSuccessClass, find, firstNameEl, lastNameEl; |
| 2 | + var expectFirstNameFormGroupHasErrorClass, expectFirstNameFormGroupHasSuccessClass, expectFormGroupHasErrorClass, expectLastNameFormGroupHasErrorClass, expectLastNameFormGroupHasSuccessClass, find, firstNameEl, lastNameEl; |
3 | 3 |
|
4 | 4 | describe('showErrors', function() { |
5 | 5 | var $compile, $scope, $timeout, compileEl, invalidName, validName; |
|
280 | 280 | var testModule; |
281 | 281 | testModule = angular.module('testModule', []); |
282 | 282 | testModule.config(function(showErrorsConfigProvider) { |
283 | | - return showErrorsConfigProvider.showSuccess(true); |
| 283 | + showErrorsConfigProvider.showSuccess(true); |
| 284 | + return showErrorsConfigProvider.trigger('keypress'); |
284 | 285 | }); |
285 | 286 | module('ui.bootstrap.showErrors', 'testModule'); |
286 | 287 | return inject(function(_$compile_, _$rootScope_, _$timeout_) { |
|
292 | 293 | compileEl = function() { |
293 | 294 | var el; |
294 | 295 | el = $compile('<form name="userForm">\ |
295 | | - <div id="first-name-group" class="form-group" show-errors="{showSuccess: false}">\ |
| 296 | + <div id="first-name-group" class="form-group" show-errors="{showSuccess: false, trigger: \'blur\'}">\ |
296 | 297 | <input type="text" name="firstName" ng-model="firstName" ng-minlength="3" class="form-control" />\ |
297 | 298 | </div>\ |
298 | 299 | <div id="last-name-group" class="form-group" show-errors>\ |
|
309 | 310 | var el; |
310 | 311 | el = compileEl(); |
311 | 312 | $scope.userForm.lastName.$setViewValue(validName); |
312 | | - angular.element(lastNameEl(el)).triggerHandler('blur'); |
| 313 | + angular.element(lastNameEl(el)).triggerHandler('keypress'); |
313 | 314 | $scope.$digest(); |
314 | 315 | return expectLastNameFormGroupHasSuccessClass(el).toBe(true); |
315 | 316 | }); |
316 | 317 | }); |
317 | 318 | }); |
318 | | - return describe('when showErrorsConfig.showSuccess is true', function() { |
| 319 | + describe('when showErrorsConfig.showSuccess is true', function() { |
319 | 320 | return describe('but options.showSuccess is false', function() { |
320 | 321 | return it('show-success class is not applied', function() { |
321 | 322 | var el; |
|
327 | 328 | }); |
328 | 329 | }); |
329 | 330 | }); |
| 331 | + return describe('when showErrorsConfig.trigger is "keypress"', function() { |
| 332 | + describe('and no options given', function() { |
| 333 | + return it('validates the value on the first keypress', function() { |
| 334 | + var el; |
| 335 | + el = compileEl(); |
| 336 | + $scope.userForm.lastName.$setViewValue(invalidName); |
| 337 | + angular.element(lastNameEl(el)).triggerHandler('keypress'); |
| 338 | + $scope.$digest(); |
| 339 | + return expectLastNameFormGroupHasErrorClass(el).toBe(true); |
| 340 | + }); |
| 341 | + }); |
| 342 | + return describe('but options.trigger is "blur"', function() { |
| 343 | + return it('does not validate the value on keypress', function() { |
| 344 | + var el; |
| 345 | + el = compileEl(); |
| 346 | + $scope.userForm.firstName.$setViewValue(invalidName); |
| 347 | + angular.element(firstNameEl(el)).triggerHandler('keypress'); |
| 348 | + $scope.$digest(); |
| 349 | + return expectFirstNameFormGroupHasErrorClass(el).toBe(false); |
| 350 | + }); |
| 351 | + }); |
| 352 | + }); |
330 | 353 | }); |
331 | 354 |
|
332 | 355 | find = function(el, selector) { |
|
359 | 382 | return expect(angular.element(formGroup).hasClass('has-success')); |
360 | 383 | }; |
361 | 384 |
|
| 385 | + expectFirstNameFormGroupHasErrorClass = function(el) { |
| 386 | + var formGroup; |
| 387 | + formGroup = el[0].querySelector('[id=first-name-group]'); |
| 388 | + return expect(angular.element(formGroup).hasClass('has-error')); |
| 389 | + }; |
| 390 | + |
| 391 | + expectLastNameFormGroupHasErrorClass = function(el) { |
| 392 | + var formGroup; |
| 393 | + formGroup = el[0].querySelector('[id=last-name-group]'); |
| 394 | + return expect(angular.element(formGroup).hasClass('has-error')); |
| 395 | + }; |
| 396 | + |
362 | 397 | }).call(this); |
0 commit comments