Currently, I am in the process of testing the code snippet below:
$rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
if(rejection === 'Unauthenticated') {
$location.path('/');
}
});
This is the test I have set up:
it('is expected to detect unauthorized route changes and redirect to login page', function() {
spyOn(this.$location, 'path');
this.$rootScope.$emit('$routeChangeError', { event: null, current: null, previous: null, rejection: 'Unauthenticated' });
expect(this.$location.path).toBe('/');
});
However, I noticed that when I pass parameters to $emit, they do not reach the other end as expected. The only populated parameter is 'event', while everything else turns out to be undefined.
The route definition is as follows:
.when('/welcome', {
controller: 'main.controller',
templateUrl: '/welcome.html',
resolve: { 'authFilter': 'oauthFilter.factory' }
});
Additionally, the authFilter code snippet appears 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 have attempted to create the controller, inject $q.reject('Unauthenticated'), then apply the scope. Unfortunately, this approach does not trigger the event at all.
Any insights on what I might be overlooking? I appreciate your help in advance.