Reputation: 3018
I'm trying to test the following code:
$rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
if(rejection === 'Unauthenticated') {
$location.path('/');
}
});
Here is the test:
it('should catch unauthorised route changes and route to log in', function() {
spyOn(this.$location, 'path');
this.$rootScope.$emit('$routeChangeError', { event: null, current: null, previous: null, rejection: 'Unauthenticated' });
expect(this.$location.path).toBe('/');
});
However because angular controls this event any parameters I pass in to $emit fail to arrive at the other end. In fact everything is undefined except 'event', which is populated with the actual event.
the route looks like this:
.when('/welcome', {
controller: 'main.controller',
templateUrl: '/welcome.html',
resolve: { 'authFilter': 'oauthFilter.factory' }
});
and the authFilter looks like this:
(function(mainModule) {
'use strict';
mainModule.factory('oauthFilter.factory',
['$location', '$q', 'authentication.service',
function($location, $q, authenticationService) {
if(authenticationService.isAuthenticated()) {
return true;
}
return $q.reject('Unauthenticated');
}
]);
})(angular.module('main'));
I've tried creating the controller too and injecting this with $q.reject('Unauthenticated'), the applying the scope but this doesn't raise the event at all.
What am I missing? Thanks in advance.
Upvotes: 1
Views: 645
Reputation: 1404
The arguments need to be passed into $emit individually, rather than as a single object, i.e:
this.$rootScope.$emit('$routeChangeError', null, null, null, 'Unauthenticated');
Upvotes: 2