1
1
describe ( 'uiStateRef' , function ( ) {
2
2
3
- var el , template , scope , document ;
3
+ var timeoutFlush , el , el2 , template , scope , document , _locationProvider ;
4
4
5
5
beforeEach ( module ( 'ui.router' ) ) ;
6
6
7
- beforeEach ( module ( function ( $stateProvider ) {
7
+ beforeEach ( module ( function ( $stateProvider , $locationProvider ) {
8
+ _locationProvider = $locationProvider ;
8
9
$stateProvider . state ( 'top' , {
9
10
url : ''
10
11
} ) . state ( 'contacts' , {
@@ -74,29 +75,30 @@ describe('uiStateRef', function() {
74
75
} ) ) ;
75
76
} ) ;
76
77
77
- describe ( 'links' , function ( ) {
78
- var timeoutFlush , el2 ;
79
78
80
- beforeEach ( inject ( function ( $rootScope , $compile , $timeout ) {
81
- el = angular . element ( '<a ui-sref="contacts.item.detail({ id: contact.id })">Details</a>' ) ;
82
- el2 = angular . element ( '<a ui-sref="top">Top</a>' ) ;
83
- scope = $rootScope ;
84
- scope . contact = { id : 5 } ;
85
- scope . $apply ( ) ;
79
+ function buildDOM ( $rootScope , $compile , $timeout ) {
80
+ el = angular . element ( '<a ui-sref="contacts.item.detail({ id: contact.id })">Details</a>' ) ;
81
+ el2 = angular . element ( '<a ui-sref="top">Top</a>' ) ;
82
+ scope = $rootScope ;
83
+ scope . contact = { id : 5 } ;
84
+ scope . $apply ( ) ;
86
85
87
- $compile ( el ) ( scope ) ;
88
- $compile ( el2 ) ( scope ) ;
89
- scope . $digest ( ) ;
86
+ $compile ( el ) ( scope ) ;
87
+ $compile ( el2 ) ( scope ) ;
88
+ scope . $digest ( ) ;
90
89
91
- timeoutFlush = function ( ) {
92
- try {
93
- $timeout . flush ( ) ;
94
- } catch ( e ) {
95
- // Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
96
- // Behave as Angular >=1.1.5 and do nothing in such case.
97
- }
90
+ timeoutFlush = function ( ) {
91
+ try {
92
+ $timeout . flush ( ) ;
93
+ } catch ( e ) {
94
+ // Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
95
+ // Behave as Angular >=1.1.5 and do nothing in such case.
98
96
}
99
- } ) ) ;
97
+ }
98
+ } ;
99
+
100
+ describe ( 'links' , function ( ) {
101
+ beforeEach ( inject ( buildDOM ) ) ;
100
102
101
103
it ( 'should generate the correct href' , function ( ) {
102
104
expect ( el . attr ( 'href' ) ) . toBe ( '#/contacts/5' ) ;
@@ -217,7 +219,7 @@ describe('uiStateRef', function() {
217
219
expect ( $state . current . name ) . toEqual ( 'top' ) ;
218
220
expect ( $stateParams ) . toEqualData ( { } ) ;
219
221
} ) ) ;
220
-
222
+
221
223
it ( 'should allow passing params to current state' , inject ( function ( $compile , $rootScope , $state ) {
222
224
$state . current . name = 'contacts.item.detail' ;
223
225
@@ -243,6 +245,38 @@ describe('uiStateRef', function() {
243
245
} ) ) ;
244
246
} ) ;
245
247
248
+ describe ( 'links in html5 mode' , function ( ) {
249
+ beforeEach ( function ( ) {
250
+ _locationProvider . html5Mode ( true ) ;
251
+ } ) ;
252
+
253
+ beforeEach ( inject ( buildDOM ) ) ;
254
+
255
+ it ( 'should generate the correct href' , function ( ) {
256
+ expect ( el . attr ( 'href' ) ) . toBe ( '/contacts/5' ) ;
257
+ expect ( el2 . attr ( 'href' ) ) . toBe ( '' ) ;
258
+ } ) ;
259
+
260
+ it ( 'should update the href when parameters change' , function ( ) {
261
+ expect ( el . attr ( 'href' ) ) . toBe ( '/contacts/5' ) ;
262
+ scope . contact . id = 6 ;
263
+ scope . $apply ( ) ;
264
+ expect ( el . attr ( 'href' ) ) . toBe ( '/contacts/6' ) ;
265
+ } ) ;
266
+
267
+ it ( 'should transition states when the url is empty' , inject ( function ( $state , $stateParams , $q ) {
268
+ // Odd, in html5Mode, the initial state isn't matching on empty url, but does match if top.url is "/".
269
+ // expect($state.$current.name).toEqual('top');
270
+
271
+ triggerClick ( el2 ) ;
272
+ timeoutFlush ( ) ;
273
+ $q . flush ( ) ;
274
+
275
+ expect ( $state . current . name ) . toEqual ( 'top' ) ;
276
+ expect ( $stateParams ) . toEqualData ( { } ) ;
277
+ } ) ) ;
278
+ } ) ;
279
+
246
280
describe ( 'forms' , function ( ) {
247
281
var el , scope ;
248
282
0 commit comments