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