@@ -162,53 +162,108 @@ describe('$route', function() {
162
162
} ) ;
163
163
164
164
165
- it ( 'should handle unknown routes with "otherwise" route definition' , function ( ) {
166
- function NotFoundCtrl ( ) { }
167
-
165
+ it ( 'should chain whens and otherwise' , function ( ) {
168
166
module ( function ( $routeProvider ) {
169
- $routeProvider . when ( '/foo' , { templateUrl : 'foo.html' } ) ;
170
- $routeProvider . otherwise ( { templateUrl : '404.html' , controller : NotFoundCtrl } ) ;
167
+ $routeProvider . when ( '/foo' , { templateUrl : 'foo.html' } ) .
168
+ otherwise ( { templateUrl : 'bar.html' } ) .
169
+ when ( '/baz' , { templateUrl : 'baz.html' } ) ;
171
170
} ) ;
172
171
173
172
inject ( function ( $route , $location , $rootScope ) {
174
- var onChangeSpy = jasmine . createSpy ( 'onChange' ) ;
175
-
176
- $rootScope . $on ( '$routeChangeStart' , onChangeSpy ) ;
177
- expect ( $route . current ) . toBeUndefined ( ) ;
178
- expect ( onChangeSpy ) . not . toHaveBeenCalled ( ) ;
179
-
180
- $location . path ( '/unknownRoute' ) ;
181
173
$rootScope . $digest ( ) ;
174
+ expect ( $route . current . templateUrl ) . toBe ( 'bar.html' ) ;
182
175
183
- expect ( $route . current . templateUrl ) . toBe ( '404.html' ) ;
184
- expect ( $route . current . controller ) . toBe ( NotFoundCtrl ) ;
185
- expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
186
-
187
- onChangeSpy . reset ( ) ;
188
- $location . path ( '/foo' ) ;
176
+ $location . url ( '/baz' ) ;
189
177
$rootScope . $digest ( ) ;
190
-
191
- expect ( $route . current . templateUrl ) . toEqual ( 'foo.html' ) ;
192
- expect ( $route . current . controller ) . toBeUndefined ( ) ;
193
- expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
178
+ expect ( $route . current . templateUrl ) . toBe ( 'baz.html' ) ;
194
179
} ) ;
195
180
} ) ;
196
181
197
182
198
- it ( 'should chain whens and otherwise' , function ( ) {
199
- module ( function ( $routeProvider ) {
200
- $routeProvider . when ( '/foo' , { templateUrl : 'foo.html' } ) .
201
- otherwise ( { templateUrl : 'bar.html' } ) .
202
- when ( '/baz' , { templateUrl : 'baz.html' } ) ;
183
+ describe ( 'otherwise' , function ( ) {
184
+
185
+ it ( 'should handle unknown routes with "otherwise" route definition' , function ( ) {
186
+ function NotFoundCtrl ( ) { }
187
+
188
+ module ( function ( $routeProvider ) {
189
+ $routeProvider . when ( '/foo' , { templateUrl : 'foo.html' } ) ;
190
+ $routeProvider . otherwise ( { templateUrl : '404.html' , controller : NotFoundCtrl } ) ;
191
+ } ) ;
192
+
193
+ inject ( function ( $route , $location , $rootScope ) {
194
+ var onChangeSpy = jasmine . createSpy ( 'onChange' ) ;
195
+
196
+ $rootScope . $on ( '$routeChangeStart' , onChangeSpy ) ;
197
+ expect ( $route . current ) . toBeUndefined ( ) ;
198
+ expect ( onChangeSpy ) . not . toHaveBeenCalled ( ) ;
199
+
200
+ $location . path ( '/unknownRoute' ) ;
201
+ $rootScope . $digest ( ) ;
202
+
203
+ expect ( $route . current . templateUrl ) . toBe ( '404.html' ) ;
204
+ expect ( $route . current . controller ) . toBe ( NotFoundCtrl ) ;
205
+ expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
206
+
207
+ onChangeSpy . reset ( ) ;
208
+ $location . path ( '/foo' ) ;
209
+ $rootScope . $digest ( ) ;
210
+
211
+ expect ( $route . current . templateUrl ) . toEqual ( 'foo.html' ) ;
212
+ expect ( $route . current . controller ) . toBeUndefined ( ) ;
213
+ expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
214
+ } ) ;
203
215
} ) ;
204
216
205
- inject ( function ( $route , $location , $rootScope ) {
206
- $rootScope . $digest ( ) ;
207
- expect ( $route . current . templateUrl ) . toBe ( 'bar.html' ) ;
208
217
209
- $location . url ( '/baz' ) ;
210
- $rootScope . $digest ( ) ;
211
- expect ( $route . current . templateUrl ) . toBe ( 'baz.html' ) ;
218
+ it ( 'should update $route.current and $route.next when default route is matched' , function ( ) {
219
+ module ( function ( $routeProvider ) {
220
+ $routeProvider . when ( '/foo' , { templateUrl : 'foo.html' } ) ;
221
+ $routeProvider . otherwise ( { templateUrl : '404.html' } ) ;
222
+ } ) ;
223
+
224
+ inject ( function ( $route , $location , $rootScope ) {
225
+ var currentRoute , nextRoute ,
226
+ onChangeSpy = jasmine . createSpy ( 'onChange' ) . andCallFake ( function ( e , next ) {
227
+ currentRoute = $route . current ;
228
+ nextRoute = next ;
229
+ } ) ;
230
+
231
+
232
+ // init
233
+ $rootScope . $on ( '$routeChangeStart' , onChangeSpy ) ;
234
+ expect ( $route . current ) . toBeUndefined ( ) ;
235
+ expect ( onChangeSpy ) . not . toHaveBeenCalled ( ) ;
236
+
237
+
238
+ // match otherwise route
239
+ $location . path ( '/unknownRoute' ) ;
240
+ $rootScope . $digest ( ) ;
241
+
242
+ expect ( currentRoute ) . toBeUndefined ( ) ;
243
+ expect ( nextRoute . templateUrl ) . toBe ( '404.html' ) ;
244
+ expect ( $route . current . templateUrl ) . toBe ( '404.html' ) ;
245
+ expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
246
+ onChangeSpy . reset ( ) ;
247
+
248
+ // match regular route
249
+ $location . path ( '/foo' ) ;
250
+ $rootScope . $digest ( ) ;
251
+
252
+ expect ( currentRoute . templateUrl ) . toBe ( '404.html' ) ;
253
+ expect ( nextRoute . templateUrl ) . toBe ( 'foo.html' ) ;
254
+ expect ( $route . current . templateUrl ) . toEqual ( 'foo.html' ) ;
255
+ expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
256
+ onChangeSpy . reset ( ) ;
257
+
258
+ // match otherwise route again
259
+ $location . path ( '/anotherUnknownRoute' ) ;
260
+ $rootScope . $digest ( ) ;
261
+
262
+ expect ( currentRoute . templateUrl ) . toBe ( 'foo.html' ) ;
263
+ expect ( nextRoute . templateUrl ) . toBe ( '404.html' ) ;
264
+ expect ( $route . current . templateUrl ) . toEqual ( '404.html' ) ;
265
+ expect ( onChangeSpy ) . toHaveBeenCalled ( ) ;
266
+ } ) ;
212
267
} ) ;
213
268
} ) ;
214
269
0 commit comments