diff --git a/app/account/login/login.controller.js b/app/account/login/login.controller.js index ae56fed41..e88aa49f2 100644 --- a/app/account/login/login.controller.js +++ b/app/account/login/login.controller.js @@ -10,7 +10,6 @@ $log = $log.getInstance("LoginController"); vm.$stateParams = $stateParams; vm.passwordReset = false; - vm.currentPasswordDefaultPlaceholder = "Password"; vm.loginErrors = { USERNAME_NONEXISTANT: false, WRONG_PASSWORD: false, diff --git a/app/directives/account/toggle-password/toggle-password.directive.js b/app/directives/account/toggle-password/toggle-password.directive.js index 56d122252..65c774ace 100644 --- a/app/directives/account/toggle-password/toggle-password.directive.js +++ b/app/directives/account/toggle-password/toggle-password.directive.js @@ -10,8 +10,8 @@ templateUrl: 'directives/account/toggle-password/toggle-password.html', link: function(scope, element, attrs, formController) { var vm = scope.vm; - vm.currentPasswordField = formController.currentPassword; - vm.currentPasswordPlaceholder = vm.currentPasswordDefaultPlaceholder; + scope.currentPasswordDefaultPlaceholder = attrs.placeholder || 'Password'; + scope.currentPasswordPlaceholder = scope.currentPasswordDefaultPlaceholder; vm.currentPassword = ''; var currentPasswordInput = element.children()[0]; @@ -27,8 +27,7 @@ }); vm.onCPFocus = function(event) { - vm.currentPasswordFocus = true; - vm.currentPasswordPlaceholder = ''; + scope.currentPasswordPlaceholder = ''; element.addClass('focus'); } @@ -38,15 +37,11 @@ // If you are blurring from the password input and clicking the checkbox if (relatedTarget.attr('type') === 'checkbox' && relatedTarget.attr('id') === 'currentPasswordCheckbox') { - vm.currentPasswordFocus = true; - vm.currentPasswordPlaceholder = ''; + scope.currentPasswordPlaceholder = ''; currentPasswordInput.focus(); } else { - // If you are blurring from the password input and clicking anywhere but the checkbox - vm.currentPasswordFocus = false; - if (vm.currentPassword === '' || vm.currentPassword === undefined) { - vm.currentPasswordPlaceholder = vm.currentPasswordDefaultPlaceholder; + scope.currentPasswordPlaceholder = scope.currentPasswordDefaultPlaceholder; formController.currentPassword.$setPristine(); } } diff --git a/app/directives/account/toggle-password/toggle-password.jade b/app/directives/account/toggle-password/toggle-password.jade index 5d38f3997..044f4db03 100644 --- a/app/directives/account/toggle-password/toggle-password.jade +++ b/app/directives/account/toggle-password/toggle-password.jade @@ -7,8 +7,8 @@ input#current-password-input( name="currentPassword", type="password", - placeholder="{{vm.currentPasswordPlaceholder}}", + placeholder="{{currentPasswordPlaceholder}}", required) -label(ng-show="vm.currentPasswordFocus || vm.currentPasswordField.$dirty") #[input(type="checkbox", id="currentPasswordCheckbox", ng-model="focusOnCurrentPasswordInput", ng-change="vm.toggleTypeAttribute()")] Show +label #[input(type="checkbox", id="currentPasswordCheckbox", ng-model="focusOnCurrentPasswordInput", ng-change="vm.toggleTypeAttribute()")] Show diff --git a/app/directives/account/toggle-password/toggle-password.spec.js b/app/directives/account/toggle-password/toggle-password.spec.js index dc5dbbf71..ed19113a4 100644 --- a/app/directives/account/toggle-password/toggle-password.spec.js +++ b/app/directives/account/toggle-password/toggle-password.spec.js @@ -2,14 +2,150 @@ describe('Toggle Password Directive', function() { var scope; var element; - // var challenge = mockData.getMockChallengeWithUserDetails(); - // var spotlightChallenge = mockData.getMockSpotlightChallenges()[0]; beforeEach(function() { bard.appModule('topcoder'); bard.inject(this, '$compile', '$rootScope'); scope = $rootScope.$new(); + scope.vm = {}; }); bard.verifyNoOutstandingHttpRequests(); + + describe('Toggle Password Directive', function() { + var togglePassword, controller, formController, passwordFormFieldSpy; + + beforeEach(function() { + var form = angular.element('
)'); + element = form.find('toggle-password'); + var formElement = $compile(form)(scope); + scope.$digest(); + + // controller = element.controller('togglePassword'); + formController = form.controller('form'); + passwordFormFieldSpy = sinon.spy(formController.currentPassword, '$setPristine'); + }); + + afterEach(function() { + // do nohting + }); + + it('should have password default placeholder', function() { + expect(scope.currentPasswordDefaultPlaceholder).to.exist.to.equal('Password'); + expect(scope.currentPasswordPlaceholder).to.exist.to.equal('Password'); + }); + + it('should not have focus class', function() { + expect(element.hasClass('focus')).to.be.false; + }); + + it('should trigger click handler ', function() { + var mockFocus = sinon.spy(element.find('input')[0], 'focus'); + element.trigger('click'); + expect(mockFocus).to.be.calledOnce; + }); + + it('should trigger focus handler ', function() { + var pwsIntputElement = angular.element(element.find('input')[0]); + pwsIntputElement.triggerHandler('focus'); + expect(element.hasClass('focus')).to.be.true; + }); + + it('should trigger blur handler with form field pristine ', function() { + var pwsIntputElement = angular.element(element.find('input')[0]); + // focus it first + pwsIntputElement.triggerHandler('focus'); + // verifies if focus class is added + expect(element.hasClass('focus')).to.be.true; + // now blurs from it + pwsIntputElement.triggerHandler('blur'); + // focus class should not be there + expect(element.hasClass('focus')).to.be.false; + // password field's setPristine method should be called once because currentPassword is empty + expect(passwordFormFieldSpy).to.be.calledOnce; + }); + + it('should trigger blur handler without form field pristine ', function() { + scope.vm.currentPassword = 'some-password'; + scope.$digest(); + var pwsIntputElement = angular.element(element.find('input')[0]); + // focus it first + pwsIntputElement.triggerHandler('focus'); + // verifies if focus class is added + expect(element.hasClass('focus')).to.be.true; + // now blurs from it + pwsIntputElement.triggerHandler('blur'); + // focus class should not be there + expect(element.hasClass('focus')).to.be.false; + // password field's setPristine method should not be called because currentPassword is non-empty + expect(passwordFormFieldSpy).not.to.be.called; + }); + + it('should keep focus on password field on blurring to checkbox ', function() { + + var pwsIntputElement = angular.element(element.find('input')[0]); + // focus it first + pwsIntputElement.triggerHandler('focus'); + // verifies if focus class is added + expect(element.hasClass('focus')).to.be.true; + // now blurs from it + + var e = jQuery.Event("blur"); + e.relatedTarget = { + getAttribute: function(name) { + if (name === 'type') return 'checkbox'; + if (name === 'id') return 'currentPasswordCheckbox'; + } + }; + //mock focus event + var mockFocus = sinon.spy(element.find('input')[0], 'focus'); + // trigger event + pwsIntputElement.trigger(e); + + // focus should be called once + expect(mockFocus).to.be.calledOnce; + // password field placeholde should be empty + expect(scope.currentPasswordPlaceholder).to.exist.to.equal(''); + }); + + it('should change type of input field to be text ', function() { + var pwsIntputElement = angular.element(element.find('input')[0]); + var checkbox = angular.element(element.find('input')[1]); + // before clicking on checkbox, it should have password type + expect(pwsIntputElement.attr('type')).to.equal('password'); + checkbox.trigger('click'); + // after clicking on checkbox, it should have text type + expect(pwsIntputElement.attr('type')).to.equal('text'); + }); + + it('should change type of input field to be password ', function() { + var pwsIntputElement = angular.element(element.find('input')[0]); + var checkbox = angular.element(element.find('input')[1]); + // before clicking on checkbox, it should have password type + expect(pwsIntputElement.attr('type')).to.equal('password'); + checkbox.trigger('click'); + // after clicking on checkbox, it should have text type + expect(pwsIntputElement.attr('type')).to.equal('text'); + // click again to uncheck the checkbox + checkbox.trigger('click'); + // after unchecking the checkbox, it should have password type + expect(pwsIntputElement.attr('type')).to.equal('password'); + }); + + it('should trigger keyup handler with enter/return key ', function() { + var mockBlur = sinon.spy(element.find('input')[0], 'blur'); + var e = jQuery.Event("keyup"); + e.keyCode = 13; + element.trigger(e); + expect(mockBlur).to.be.calledOnce; + }); + + it('should NOT trigger keyup handler with non enter/return key ', function() { + var mockBlur = sinon.spy(element.find('input')[0], 'blur'); + var e = jQuery.Event("keyup"); + e.keyCode = 14; + element.trigger(e); + expect(mockBlur).not.to.be.called; + }); + }); });