Skip to content

Commit cb560e2

Browse files
committed
test($route): add tests for matching 'otherwise' routes
1 parent 5585656 commit cb560e2

File tree

1 file changed

+88
-33
lines changed

1 file changed

+88
-33
lines changed

test/ng/routeSpec.js

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -162,53 +162,108 @@ describe('$route', function() {
162162
});
163163

164164

165-
it('should handle unknown routes with "otherwise" route definition', function() {
166-
function NotFoundCtrl() {}
167-
165+
it('should chain whens and otherwise', function() {
168166
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'});
171170
});
172171

173172
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');
181173
$rootScope.$digest();
174+
expect($route.current.templateUrl).toBe('bar.html');
182175

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');
189177
$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');
194179
});
195180
});
196181

197182

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+
});
203215
});
204216

205-
inject(function($route, $location, $rootScope) {
206-
$rootScope.$digest();
207-
expect($route.current.templateUrl).toBe('bar.html');
208217

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+
});
212267
});
213268
});
214269

0 commit comments

Comments
 (0)