-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Testing for functional guards #640
Comments
You are welcome to play with some ideas you have and see what's the best fit and open a PR. |
I've been working on this on-and-off and it turns out to be quite a bit more annoying than I anticipated. The first step seems to be using Exposing it via the public runInInjectionContext<T>(cb: () => T): T {
return TestBed.runInInjectionContext(cb)
} spectator.runInInjectionContext(() => {
const activatedRouteSnapshot = spectator.inject(ActivatedRouteSnapshot);
const routerStateSnapshot = spectator.inject(RouterStateSnapshot);
expect(functionalGuard(activatedRouteSnapshot, routerStateSnapshot)).toBe(true);
}); Some problems: Functional guards most often use the export declare type CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree; As of yet the I'll continue working on this and see if I can create something useful (Progress in fork - https://github.com/RiskChallenger/spectator) |
any progress here? 😁 |
I used a bit of a hack but I was able to test functional guards and resolvers We need to use the Example resolver
Then in your test
I think this could be wrapped into a nice What do you think @NetanelBasal ? |
Description
There does not seem to be a proper way to test functional guard with spectator as yet.
It seems like they need to be tested using angular
runInInjectionContext
method.https://netbasal.com/getting-to-know-the-runincontext-api-in-angular-f8996d7e00da
It would be nice to have first-class support for functional guards.
Proposed solution
Create a new API to test functional guards that can use spectators injector.
Alternatives considered
More so a work-around than an alternative, creating an empty component and "stealing" its injector.
Do you want to create a pull request?
I'd be open to trying to create a pull request if we can settle on a nice API.
The text was updated successfully, but these errors were encountered: