Reputation: 19686
I'm implementing tests for a simple canActivate
guard. See below:
// guard
export const hasGraphResultsCanActiveGuard: CanActivateFn = (
route: ActivatedRouteSnapshot,
routerState: RouterStateSnapshot
) => {
return inject(Store)
.select(selectGraphResults)
.pipe(
map((results) => {
return results.length > 0;
}),
tap((hasResults) => {
if (!hasResults) {
inject(Router).navigate(['/app/map']);
}
})
);
};
// routes in the module where its used
const routes: Routes = [
{
path: 'analysis',
component: AnalysisComponent,
canActivate: [hasGraphResultsCanActiveGuard],
},
{
path: '**',
pathMatch: 'full',
redirectTo: 'analysis',
},
];
// Test
describe('HasGraphResultsGuard', () => {
beforeEach(() => {
return (
MockBuilder(
[
RouterModule,
RouterTestingModule.withRoutes([]),
NG_MOCKS_ROOT_PROVIDERS,
],
DisruptionPageModule
)
.exclude(NG_MOCKS_GUARDS)
.keep(hasGraphResultsCanActiveGuard)
.provide(
provideMockStore({
initialState: {
graph: cloneDeep(initialGraphState),
},
})
)
);
});
it('Redirects to /app/map', fakeAsync(() => {
const fixture = MockRender(RouterOutlet, {});
const router = ngMocks.get(Router);
const location = ngMocks.get(Location);
// Initialize navigation
if (fixture.ngZone) {
fixture.ngZone.run(() => router.initialNavigation());
tick();
}
// The initial state has no graph results so it should redirect
expect(location.path()).toEqual('/app/map');
}));
});
Unless I've missed something I believe I've implemented the test exactly as the docs suggest. Here's the error I get:
Error: MockBuilder has found a missing dependency: hasGraphResultsCanActiveGuard. It means no module provides it. Please, use the "export" flag if you want to add it explicitly. https://ng-mocks.sudo.eu/api/MockBuilder#export-flag
I've tried adding the export flag and then it says it cant resolve the parameters.
The guard works as intended in my app but I cant connect things correctly in the test. Am I missing something or are the docs incomplete?
Upvotes: 0
Views: 409
Reputation: 33
had the same issue, i think a ng-mocks update to 14.11.0 solved it
Upvotes: 1