You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
exportconstfunctionalGuard=(()=>{constsomeService=inject(SomeService);returnsomeService.canDoStuff();})satisfiesCanActivateFn;describe('functionalGuard',()=>{letspectator: Spectator<theFunctionalGuard>;constcreateFunctionalGuard=createFunctionalGuardFactory({guard: theFunctionalGuard,});beforeEach(()=>{spectator=createFunctionalGuard();});it('should test the guard',()=>{spectator.runInInjectionContext((guard)=>{// set some inputs for the guard, mock valuesexpect(guard).toReturnWith(true);});});});
Alternatives considered
More so a work-around than an alternative, creating an empty component and "stealing" its injector.
@Component({selector: 'app-empty-component',template: '',changeDetection: ChangeDetectionStrategy.OnPush,standalone: true,})classEmptyComponent{}describe('functionalGuard',()=>{letspectator: Spectator<EmptyComponent>;constcreateComponent=createComponentFactory(EmptyComponent);beforeEach(()=>{spectator=createComponent();});it('should test the guard',()=>{runInInjectionContext(spectator.inject(EnvironmentInjector),()=>{// set some inputs for the guard, mock valuesexpect(functionalGuard).toReturnWith(true);});});});
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:
As of yet the ActivatedRouteSnapshot can be created using the ActivedRouteStub of spectator fairly easily, I have not been able to find a proper way to generate a RouterStateSnapshot.
I'll continue working on this and see if I can create something useful
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: