I have a simple router guard and I am trying to test the canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot )
. I can create the ActivatedRouteSnapshot like this new ActivatedRouteSnapshot()
but I cannot figure out how to create a mocked RouterStateSnapshot
.
Per the code I tried...
let createEmptyStateSnapshot = function(
urlTree: UrlTree, rootComponent: Type<any>){
const emptyParams = {};
const emptyData = {};
const emptyQueryParams = {};
const fragment = '';
const activated = new ActivatedRouteSnapshot();
const state = new RouterStateSnapshot(new TreeNode<ActivatedRouteSnapshot>(activated, []));
return {
state: state,
activated: activated
}
}
But import {TreeNode} from "@angular/router/src/utils/tree";
seems to need to be transpiled or something because I get...
Uncaught SyntaxError: Unexpected token export at webpack:///~/@angular/router/src/utils/tree.js:8:0 <- test.bundle.ts:72431
I needed to get the data in the route to test for user roles in my guard, so I mocked it this way:
I managed to do it slightly differently but it should work for you :
Based on a previous question I had about Router I tried this...
The problem I now have is that I need the toString here
mockSnapshot = jasmine.createSpyObj("RouterStateSnapshot", ['toString']);
. This is because jasmine createSpyObj requires at least one mocked method. Since I am not testing the side effects of RouterStateSnapshot, this seems like extra work for nothing.