@@ -75,15 +75,25 @@ describe('uiStateRef', function() {
75
75
} ) ;
76
76
77
77
describe ( 'links' , function ( ) {
78
+ var timeoutFlush ;
78
79
79
- beforeEach ( inject ( function ( $rootScope , $compile ) {
80
+ beforeEach ( inject ( function ( $rootScope , $compile , $timeout ) {
80
81
el = angular . element ( '<a ui-sref="contacts.item.detail({ id: contact.id })">Details</a>' ) ;
81
82
scope = $rootScope ;
82
83
scope . contact = { id : 5 } ;
83
84
scope . $apply ( ) ;
84
85
85
86
$compile ( el ) ( scope ) ;
86
87
scope . $digest ( ) ;
88
+
89
+ timeoutFlush = function ( ) {
90
+ try {
91
+ $timeout . flush ( ) ;
92
+ } catch ( e ) {
93
+ // Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
94
+ // Behave as Angular >=1.1.5 and do nothing in such case.
95
+ }
96
+ }
87
97
} ) ) ;
88
98
89
99
it ( 'should generate the correct href' , function ( ) {
@@ -107,18 +117,18 @@ describe('uiStateRef', function() {
107
117
expect ( el . attr ( 'href' ) ) . toBe ( '#/contacts/3' ) ;
108
118
} ) ) ;
109
119
110
- it ( 'should transition states when left-clicked' , inject ( function ( $state , $stateParams , $document , $q , $timeout ) {
120
+ it ( 'should transition states when left-clicked' , inject ( function ( $state , $stateParams , $document , $q ) {
111
121
expect ( $state . $current . name ) . toEqual ( '' ) ;
112
122
113
123
triggerClick ( el ) ;
114
- $timeout . flush ( ) ;
124
+ timeoutFlush ( ) ;
115
125
$q . flush ( ) ;
116
126
117
127
expect ( $state . current . name ) . toEqual ( 'contacts.item.detail' ) ;
118
128
expect ( $stateParams ) . toEqual ( { id : "5" } ) ;
119
129
} ) ) ;
120
130
121
- it ( 'should transition when given a click that contains no data (fake-click)' , inject ( function ( $state , $stateParams , $document , $q , $timeout ) {
131
+ it ( 'should transition when given a click that contains no data (fake-click)' , inject ( function ( $state , $stateParams , $document , $q ) {
122
132
expect ( $state . current . name ) . toEqual ( '' ) ;
123
133
124
134
triggerClick ( el , {
@@ -128,7 +138,7 @@ describe('uiStateRef', function() {
128
138
altKey : undefined ,
129
139
button : undefined
130
140
} ) ;
131
- $timeout . flush ( ) ;
141
+ timeoutFlush ( ) ;
132
142
$q . flush ( ) ;
133
143
134
144
expect ( $state . current . name ) . toEqual ( 'contacts.item.detail' ) ;
@@ -139,7 +149,9 @@ describe('uiStateRef', function() {
139
149
expect ( $state . $current . name ) . toEqual ( '' ) ;
140
150
triggerClick ( el , { ctrlKey : true } ) ;
141
151
152
+ timeoutFlush ( ) ;
142
153
$q . flush ( ) ;
154
+
143
155
expect ( $state . current . name ) . toEqual ( '' ) ;
144
156
expect ( $stateParams ) . toEqual ( { id : "5" } ) ;
145
157
} ) ) ;
@@ -148,6 +160,7 @@ describe('uiStateRef', function() {
148
160
expect ( $state . $current . name ) . toEqual ( '' ) ;
149
161
150
162
triggerClick ( el , { metaKey : true } ) ;
163
+ timeoutFlush ( ) ;
151
164
$q . flush ( ) ;
152
165
153
166
expect ( $state . current . name ) . toEqual ( '' ) ;
@@ -158,6 +171,7 @@ describe('uiStateRef', function() {
158
171
expect ( $state . $current . name ) . toEqual ( '' ) ;
159
172
160
173
triggerClick ( el , { shiftKey : true } ) ;
174
+ timeoutFlush ( ) ;
161
175
$q . flush ( ) ;
162
176
163
177
expect ( $state . current . name ) . toEqual ( '' ) ;
@@ -168,17 +182,33 @@ describe('uiStateRef', function() {
168
182
expect ( $state . $current . name ) . toEqual ( '' ) ;
169
183
170
184
triggerClick ( el , { button : 1 } ) ;
185
+ timeoutFlush ( ) ;
171
186
$q . flush ( ) ;
172
187
173
188
expect ( $state . current . name ) . toEqual ( '' ) ;
174
189
expect ( $stateParams ) . toEqual ( { id : "5" } ) ;
175
190
} ) ) ;
176
191
177
- it ( 'should not transition states when element has target specified' , inject ( function ( $state , $stateParams , $document , $q , $timeout ) {
192
+ it ( 'should not transition states when element has target specified' , inject ( function ( $state , $stateParams , $document , $q ) {
178
193
el . attr ( 'target' , '_blank' ) ;
179
194
expect ( $state . $current . name ) . toEqual ( '' ) ;
180
195
181
196
triggerClick ( el ) ;
197
+ timeoutFlush ( ) ;
198
+ $q . flush ( ) ;
199
+
200
+ expect ( $state . current . name ) . toEqual ( '' ) ;
201
+ expect ( $stateParams ) . toEqual ( { id : "5" } ) ;
202
+ } ) ) ;
203
+
204
+ it ( 'should not transition states if preventDefault() is called in click handler' , inject ( function ( $state , $stateParams , $document , $q ) {
205
+ expect ( $state . $current . name ) . toEqual ( '' ) ;
206
+ el . bind ( 'click' , function ( e ) {
207
+ e . preventDefault ( ) ;
208
+ } ) ;
209
+
210
+ triggerClick ( el ) ;
211
+ timeoutFlush ( ) ;
182
212
$q . flush ( ) ;
183
213
184
214
expect ( $state . current . name ) . toEqual ( '' ) ;
0 commit comments