@@ -15,6 +15,9 @@ describe('uiStateRef', function() {
15
15
} ) . state ( 'other' , {
16
16
url : '/other/:id' ,
17
17
template : 'other'
18
+ } ) . state ( 'other.detail' , {
19
+ url : '/detail' ,
20
+ template : 'detail'
18
21
} ) . state ( 'contacts' , {
19
22
url : '/contacts' ,
20
23
template : '<a ui-sref=".item({ id: 5 })" class="item">Person</a> <ui-view></ui-view>'
@@ -26,8 +29,16 @@ describe('uiStateRef', function() {
26
29
} ) ;
27
30
} ) ) ;
28
31
29
- beforeEach ( inject ( function ( $document ) {
32
+ beforeEach ( inject ( function ( $document , $timeout ) {
30
33
document = $document [ 0 ] ;
34
+ timeoutFlush = function ( ) {
35
+ try {
36
+ $timeout . flush ( ) ;
37
+ } catch ( e ) {
38
+ // Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
39
+ // Behave as Angular >=1.1.5 and do nothing in such case.
40
+ }
41
+ }
31
42
} ) ) ;
32
43
33
44
function triggerClick ( el , options ) {
@@ -93,15 +104,6 @@ describe('uiStateRef', function() {
93
104
$compile ( el ) ( scope ) ;
94
105
$compile ( el2 ) ( scope ) ;
95
106
scope . $digest ( ) ;
96
-
97
- timeoutFlush = function ( ) {
98
- try {
99
- $timeout . flush ( ) ;
100
- } catch ( e ) {
101
- // Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
102
- // Behave as Angular >=1.1.5 and do nothing in such case.
103
- }
104
- }
105
107
} ;
106
108
107
109
describe ( 'links' , function ( ) {
@@ -236,20 +238,48 @@ describe('uiStateRef', function() {
236
238
expect ( obj ( $stateParams ) ) . toEqualData ( { } ) ;
237
239
} ) ) ;
238
240
239
- it ( 'should allow passing params to current state' , inject ( function ( $compile , $rootScope , $state ) {
240
- $state . current . name = 'contacts.item.detail' ;
241
+ // Test for #1031
242
+ it ( 'should allow passing params to current state' , inject ( function ( $compile , $rootScope , $state , $q ) {
243
+ $state . go ( 'other' , { id : 'abc' } ) ;
244
+ $rootScope . $index = 'def' ;
245
+ $rootScope . $digest ( ) ;
241
246
242
247
el = angular . element ( "<a ui-sref=\"{id: $index}\">Details</a>" ) ;
243
- $rootScope . $index = 3 ;
244
- $rootScope . $apply ( ) ;
245
-
246
248
$compile ( el ) ( $rootScope ) ;
247
249
$rootScope . $digest ( ) ;
248
- expect ( el . attr ( 'href' ) ) . toBe ( '#/contacts/3' ) ;
250
+
251
+ expect ( $state . current . name ) . toBe ( 'other' ) ;
252
+ expect ( $state . params ) . toEqualValues ( { id : 'abc' } ) ;
253
+ expect ( el . attr ( 'href' ) ) . toBe ( '#/other/def' ) ;
254
+
255
+ triggerClick ( el ) ;
256
+ timeoutFlush ( ) ;
257
+ $q . flush ( ) ;
258
+
259
+ expect ( $state . current . name ) . toBe ( 'other' ) ;
260
+ expect ( $state . params ) . toEqualValues ( { id : 'def' } ) ;
261
+
262
+ $rootScope . $index = 'ghi' ;
263
+ $state . go ( 'other.detail' ) ;
264
+ $rootScope . $digest ( ) ;
265
+
266
+ expect ( $state . current . name ) . toBe ( 'other.detail' ) ;
267
+ expect ( $state . params ) . toEqualValues ( { id : 'def' } ) ;
268
+
269
+ expect ( el . attr ( 'href' ) ) . toBe ( '#/other/ghi/detail' ) ;
270
+
271
+ triggerClick ( el ) ;
272
+ timeoutFlush ( ) ;
273
+ $q . flush ( ) ;
274
+
275
+ expect ( $state . current . name ) . toBe ( 'other.detail' ) ;
276
+ expect ( $state . params ) . toEqualValues ( { id : 'ghi' } ) ;
277
+
249
278
} ) ) ;
250
279
251
280
it ( 'should allow multi-line attribute values when passing params to current state' , inject ( function ( $compile , $rootScope , $state ) {
252
- $state . current . name = 'contacts.item.detail' ;
281
+ $state . go ( 'contacts.item.detail' , { id : '123' } ) ;
282
+ $rootScope . $digest ( ) ;
253
283
254
284
el = angular . element ( "<a ui-sref=\"{\n\tid: $index\n}\">Details</a>" ) ;
255
285
$rootScope . $index = 3 ;
@@ -376,6 +406,44 @@ describe('uiStateRef', function() {
376
406
expect ( angular . element ( template [ 0 ] ) . attr ( 'href' ) ) . toBe ( '#/other/123' ) ;
377
407
} ) ;
378
408
409
+ // Test for #1031
410
+ it ( 'should allow passing params to current state using empty ui-state' , inject ( function ( $compile , $rootScope , $state , $q ) {
411
+ $state . go ( 'other' , { id : 'abc' } ) ;
412
+ $rootScope . $index = 'def' ;
413
+ $rootScope . $digest ( ) ;
414
+
415
+ el = angular . element ( '<a ui-state="" ui-state-params="{id: $index}">Details</a>' ) ;
416
+ $compile ( el ) ( $rootScope ) ;
417
+ $rootScope . $digest ( ) ;
418
+
419
+ expect ( $state . current . name ) . toBe ( 'other' ) ;
420
+ expect ( $state . params ) . toEqualValues ( { id : 'abc' } ) ;
421
+ expect ( el . attr ( 'href' ) ) . toBe ( '#/other/def' ) ;
422
+
423
+ triggerClick ( el ) ;
424
+ timeoutFlush ( ) ;
425
+ $q . flush ( ) ;
426
+
427
+ expect ( $state . current . name ) . toBe ( 'other' ) ;
428
+ expect ( $state . params ) . toEqualValues ( { id : 'def' } ) ;
429
+
430
+ $rootScope . $index = 'ghi' ;
431
+ $state . go ( 'other.detail' ) ;
432
+ $rootScope . $digest ( ) ;
433
+
434
+ expect ( $state . current . name ) . toBe ( 'other.detail' ) ;
435
+ expect ( $state . params ) . toEqualValues ( { id : 'def' } ) ;
436
+
437
+ expect ( el . attr ( 'href' ) ) . toBe ( '#/other/ghi/detail' ) ;
438
+
439
+ triggerClick ( el ) ;
440
+ timeoutFlush ( ) ;
441
+ $q . flush ( ) ;
442
+
443
+ expect ( $state . current . name ) . toBe ( 'other.detail' ) ;
444
+ expect ( $state . params ) . toEqualValues ( { id : 'ghi' } ) ;
445
+ } ) ) ;
446
+
379
447
it ( 'retains the old href if the new points to a non-state' , function ( ) {
380
448
expect ( angular . element ( template [ 0 ] ) . attr ( 'href' ) ) . toBe ( '#/contacts' ) ;
381
449
scope . state = 'nostate' ;
0 commit comments