|
2 | 2 | describe('Toggle Password Directive', function() {
|
3 | 3 | var scope;
|
4 | 4 | var element;
|
5 |
| - // var challenge = mockData.getMockChallengeWithUserDetails(); |
6 |
| - // var spotlightChallenge = mockData.getMockSpotlightChallenges()[0]; |
7 | 5 |
|
8 | 6 | beforeEach(function() {
|
9 | 7 | bard.appModule('topcoder');
|
10 | 8 | bard.inject(this, '$compile', '$rootScope');
|
11 | 9 | scope = $rootScope.$new();
|
| 10 | + scope.vm = {}; |
12 | 11 | });
|
13 | 12 |
|
14 | 13 | bard.verifyNoOutstandingHttpRequests();
|
| 14 | + |
| 15 | + describe('Toggle Password Directive', function() { |
| 16 | + var togglePassword, controller, formController, passwordFormFieldSpy; |
| 17 | + |
| 18 | + beforeEach(function() { |
| 19 | + var form = angular.element('<form><toggle-password /></form>)'); |
| 20 | + element = form.find('toggle-password'); |
| 21 | + var formElement = $compile(form)(scope); |
| 22 | + scope.$digest(); |
| 23 | + |
| 24 | + // controller = element.controller('togglePassword'); |
| 25 | + formController = form.controller('form'); |
| 26 | + passwordFormFieldSpy = sinon.spy(formController.currentPassword, '$setPristine'); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(function() { |
| 30 | + // do nohting |
| 31 | + }); |
| 32 | + |
| 33 | + it('should have password default placeholder', function() { |
| 34 | + expect(scope.currentPasswordDefaultPlaceholder).to.exist.to.equal('Password'); |
| 35 | + expect(scope.currentPasswordPlaceholder).to.exist.to.equal('Password'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not have focus class', function() { |
| 39 | + expect(element.hasClass('focus')).to.be.false; |
| 40 | + }); |
| 41 | + |
| 42 | + it('should trigger click handler ', function() { |
| 43 | + var mockFocus = sinon.spy(element.find('input')[0], 'focus'); |
| 44 | + element.trigger('click'); |
| 45 | + expect(mockFocus).to.be.calledOnce; |
| 46 | + }); |
| 47 | + |
| 48 | + it('should trigger focus handler ', function() { |
| 49 | + var pwsIntputElement = angular.element(element.find('input')[0]); |
| 50 | + pwsIntputElement.triggerHandler('focus'); |
| 51 | + expect(element.hasClass('focus')).to.be.true; |
| 52 | + }); |
| 53 | + |
| 54 | + it('should trigger blur handler with form field pristine ', function() { |
| 55 | + var pwsIntputElement = angular.element(element.find('input')[0]); |
| 56 | + // focus it first |
| 57 | + pwsIntputElement.triggerHandler('focus'); |
| 58 | + // verifies if focus class is added |
| 59 | + expect(element.hasClass('focus')).to.be.true; |
| 60 | + // now blurs from it |
| 61 | + pwsIntputElement.triggerHandler('blur'); |
| 62 | + // focus class should not be there |
| 63 | + expect(element.hasClass('focus')).to.be.false; |
| 64 | + // password field's setPristine method should be called once because currentPassword is empty |
| 65 | + expect(passwordFormFieldSpy).to.be.calledOnce; |
| 66 | + }); |
| 67 | + |
| 68 | + it('should trigger blur handler without form field pristine ', function() { |
| 69 | + scope.vm.currentPassword = 'some-password'; |
| 70 | + scope.$digest(); |
| 71 | + var pwsIntputElement = angular.element(element.find('input')[0]); |
| 72 | + // focus it first |
| 73 | + pwsIntputElement.triggerHandler('focus'); |
| 74 | + // verifies if focus class is added |
| 75 | + expect(element.hasClass('focus')).to.be.true; |
| 76 | + // now blurs from it |
| 77 | + pwsIntputElement.triggerHandler('blur'); |
| 78 | + // focus class should not be there |
| 79 | + expect(element.hasClass('focus')).to.be.false; |
| 80 | + // password field's setPristine method should not be called because currentPassword is non-empty |
| 81 | + expect(passwordFormFieldSpy).not.to.be.called; |
| 82 | + }); |
| 83 | + |
| 84 | + it('should keep focus on password field on blurring to checkbox ', function() { |
| 85 | + |
| 86 | + var pwsIntputElement = angular.element(element.find('input')[0]); |
| 87 | + // focus it first |
| 88 | + pwsIntputElement.triggerHandler('focus'); |
| 89 | + // verifies if focus class is added |
| 90 | + expect(element.hasClass('focus')).to.be.true; |
| 91 | + // now blurs from it |
| 92 | + |
| 93 | + var e = jQuery.Event("blur"); |
| 94 | + e.relatedTarget = { |
| 95 | + getAttribute: function(name) { |
| 96 | + if (name === 'type') return 'checkbox'; |
| 97 | + if (name === 'id') return 'currentPasswordCheckbox'; |
| 98 | + } |
| 99 | + }; |
| 100 | + //mock focus event |
| 101 | + var mockFocus = sinon.spy(element.find('input')[0], 'focus'); |
| 102 | + // trigger event |
| 103 | + pwsIntputElement.trigger(e); |
| 104 | + |
| 105 | + // focus should be called once |
| 106 | + expect(mockFocus).to.be.calledOnce; |
| 107 | + // password field placeholde should be empty |
| 108 | + expect(scope.currentPasswordPlaceholder).to.exist.to.equal(''); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should change type of input field to be text ', function() { |
| 112 | + var pwsIntputElement = angular.element(element.find('input')[0]); |
| 113 | + var checkbox = angular.element(element.find('input')[1]); |
| 114 | + // before clicking on checkbox, it should have password type |
| 115 | + expect(pwsIntputElement.attr('type')).to.equal('password'); |
| 116 | + checkbox.trigger('click'); |
| 117 | + // after clicking on checkbox, it should have text type |
| 118 | + expect(pwsIntputElement.attr('type')).to.equal('text'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should change type of input field to be password ', function() { |
| 122 | + var pwsIntputElement = angular.element(element.find('input')[0]); |
| 123 | + var checkbox = angular.element(element.find('input')[1]); |
| 124 | + // before clicking on checkbox, it should have password type |
| 125 | + expect(pwsIntputElement.attr('type')).to.equal('password'); |
| 126 | + checkbox.trigger('click'); |
| 127 | + // after clicking on checkbox, it should have text type |
| 128 | + expect(pwsIntputElement.attr('type')).to.equal('text'); |
| 129 | + // click again to uncheck the checkbox |
| 130 | + checkbox.trigger('click'); |
| 131 | + // after unchecking the checkbox, it should have password type |
| 132 | + expect(pwsIntputElement.attr('type')).to.equal('password'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should trigger keyup handler with enter/return key ', function() { |
| 136 | + var mockBlur = sinon.spy(element.find('input')[0], 'blur'); |
| 137 | + var e = jQuery.Event("keyup"); |
| 138 | + e.keyCode = 13; |
| 139 | + element.trigger(e); |
| 140 | + expect(mockBlur).to.be.calledOnce; |
| 141 | + }); |
| 142 | + |
| 143 | + it('should NOT trigger keyup handler with non enter/return key ', function() { |
| 144 | + var mockBlur = sinon.spy(element.find('input')[0], 'blur'); |
| 145 | + var e = jQuery.Event("keyup"); |
| 146 | + e.keyCode = 14; |
| 147 | + element.trigger(e); |
| 148 | + expect(mockBlur).not.to.be.called; |
| 149 | + }); |
| 150 | + }); |
15 | 151 | });
|
0 commit comments