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