|
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | describe('jcs-autoValidate validationManager', function () { |
5 | | - var sandbox, $rootScope, $compile, $q, validator, validationManager, modelCtrl, defer, elementUtils, |
| 5 | + var sandbox, $rootScope, $compile, $q, validator, validationManager, modelCtrl, defer, elementUtils, $anchorScroll, |
6 | 6 | setModelCtrl = function () { |
7 | 7 | modelCtrl = { |
8 | 8 | $parsers: [], |
|
35 | 35 | beforeEach(module('jcs-autoValidate')); |
36 | 36 |
|
37 | 37 | describe('validationManager', function () { |
38 | | - beforeEach(inject(function ($injector) { |
| 38 | + beforeEach(function () { |
39 | 39 | sandbox = sinon.sandbox.create(); |
| 40 | + }); |
| 41 | + |
| 42 | + beforeEach(module('jcs-autoValidate', function ($provide) { |
| 43 | + $provide.value('$anchorScroll', sandbox.stub()); |
| 44 | + })); |
| 45 | + |
| 46 | + beforeEach(inject(function ($injector) { |
40 | 47 | $rootScope = $injector.get('$rootScope'); |
41 | 48 | $compile = $injector.get('$compile'); |
42 | 49 | $q = $injector.get('$q'); |
43 | 50 | defer = $q.defer(); |
44 | 51 | validator = $injector.get('validator'); |
45 | 52 | validationManager = $injector.get('validationManager'); |
46 | 53 | elementUtils = $injector.get('jcs-elementUtils'); |
| 54 | + $anchorScroll = $injector.get('$anchorScroll'); |
47 | 55 |
|
48 | 56 | sandbox.stub(validator, 'makeValid'); |
49 | 57 | sandbox.stub(validator, 'makeInvalid'); |
|
439 | 447 |
|
440 | 448 | expect(validator.makeInvalid.calledOnce).to.equal(true); |
441 | 449 | }); |
| 450 | + |
| 451 | + it('should call $anchorScroll to first invalid element when the option is disabled', function () { |
| 452 | + var frm = compileElement('<form name="frm1" ng-submit=""></form>', true), |
| 453 | + inpt = compileElement('<input type="text" ng-model="name" required="required" ng-minlength="2" />', true); |
| 454 | + |
| 455 | + |
| 456 | + sandbox.stub(elementUtils, 'isElementVisible').returns(true); |
| 457 | + sandbox.stub(validator, 'firstInvalidElementScrollingOnSubmitEnabled').returns(false); |
| 458 | + frm.append(inpt); |
| 459 | + $rootScope.$apply(); |
| 460 | + |
| 461 | + frm.on('submit', function (event) { |
| 462 | + event.preventDefault(); |
| 463 | + }); |
| 464 | + |
| 465 | + defer.resolve('errorMsg'); |
| 466 | + frm.trigger('submit'); |
| 467 | + |
| 468 | + $rootScope.$apply(); |
| 469 | + |
| 470 | + expect($anchorScroll.notCalled).to.equal(true); |
| 471 | + }); |
| 472 | + |
| 473 | + it('should call $anchorScroll with first invalid element id when the option is enabled', function () { |
| 474 | + var frm = compileElement('<form name="frm1" ng-submit=""></form>', true), |
| 475 | + firstInpt = compileElement('<input type="text" id="first" ng-model="name" required="required" ng-minlength="2" />', true), |
| 476 | + secondInpt = compileElement('<input type="text" id="second" ng-model="description" required="required" ng-minlength="2" />', true); |
| 477 | + |
| 478 | + |
| 479 | + sandbox.stub(elementUtils, 'isElementVisible').returns(true); |
| 480 | + sandbox.stub(validator, 'firstInvalidElementScrollingOnSubmitEnabled').returns(true); |
| 481 | + frm.append(firstInpt); |
| 482 | + frm.append(secondInpt); |
| 483 | + $rootScope.$apply(); |
| 484 | + |
| 485 | + frm.on('submit', function (event) { |
| 486 | + event.preventDefault(); |
| 487 | + }); |
| 488 | + |
| 489 | + defer.resolve('errorMsg'); |
| 490 | + frm.trigger('submit'); |
| 491 | + |
| 492 | + $rootScope.$apply(); |
| 493 | + |
| 494 | + expect($anchorScroll.calledOnce).to.equal(true); |
| 495 | + expect($anchorScroll.calledWith('first')).to.equal(true); |
| 496 | + }); |
| 497 | + |
| 498 | + it('should not call $anchorScroll with an undefined id', function () { |
| 499 | + var frm = compileElement('<form name="frm1" ng-submit=""></form>', true), |
| 500 | + firstInpt = compileElement('<input type="text" ng-model="name" required="required" ng-minlength="2" />', true); |
| 501 | + |
| 502 | + |
| 503 | + sandbox.stub(elementUtils, 'isElementVisible').returns(true); |
| 504 | + sandbox.stub(validator, 'firstInvalidElementScrollingOnSubmitEnabled').returns(true); |
| 505 | + frm.append(firstInpt); |
| 506 | + $rootScope.$apply(); |
| 507 | + |
| 508 | + frm.on('submit', function (event) { |
| 509 | + event.preventDefault(); |
| 510 | + }); |
| 511 | + |
| 512 | + defer.resolve('errorMsg'); |
| 513 | + frm.trigger('submit'); |
| 514 | + |
| 515 | + $rootScope.$apply(); |
| 516 | + |
| 517 | + expect($anchorScroll.notCalled).to.equal(true); |
| 518 | + }); |
442 | 519 | }); |
443 | 520 |
|
444 | 521 | describe('resetForm', function () { |
|
0 commit comments