Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover all guards variants #1008

Open
satanTime opened this issue Aug 30, 2021 · 0 comments
Open

Cover all guards variants #1008

satanTime opened this issue Aug 30, 2021 · 0 comments
Labels
good first issue Good for newcomers

Comments

@satanTime
Copy link
Member

The question came from gitter: https://gitter.im/ng-mocks/community?at=6128e79d4c7be06b7986702b

Hi guys.
I'm trying to test a route resolver and everything seems to be working fine for the successful resolve, but for unsuccessful resolves, I redirect the user to a different route using

            return from(
                this.router.navigate(['app/maintenance'], { state: { tab } })
            ).pipe(map(() => null));

The problem seems to be that when I check the location.path, it has not navigated to the route yet (Error: Expected '/' to equal '/app/maintenance'). I can also see in coverage that the map operator has not been hit at all, which leads me to believe that the router.navigate promise has not resolved yet when the test ends.
I've tried multiple tick, flush, flushMicrotasks calls but I'm not able to get this router navigate to resolve.
Can anyone assist with this?


One thing I noticed is that the route has a guard on, and to get the tests to run I had to mock that guard, even though I'm adding .exclude(NG_MOCKS_GUARDS).

Here is my MockBuilder

return MockBuilder(WidgetResolver, [
    RuntimeModule,
    AppModule,
    MaintenanceModule,
    HomeModule,
    StoreFeaturesModule
])
    .exclude(NG_MOCKS_GUARDS)
    .keep(RouterModule)
    .keep(RouterTestingModule.withRoutes([]))
    .keep(RuntimeComponent)
    .keep(StoreRootModule)
    .mock(WidgetPreviewComponent)
    .mock(MaintenanceComponent)
    .mock(MessageService)
    .mock(MaintenanceGuard)
    .provide(provideMockStore({ initialState }));

If I don't mock that guard then I get

Error: Uncaught (in promise): TypeError: Cannot read property 'pipe' of undefined TypeError: Cannot read property 'pipe' of undefined at MaintenanceGuard.checkForMaintenancePermission (src/app/guards/maintenance.guard.ts:34:51) at MaintenanceGuard.canLoad (src/app/guards/maintenance.guard.ts:22:6)

Does the NG_MOCKS_GUARDS work for canLoadGuards?

@satanTime satanTime added the good first issue Good for newcomers label Mar 26, 2022
windmichael pushed a commit to windmichael/ng-mocks that referenced this issue Jul 25, 2024
windmichael pushed a commit to windmichael/ng-mocks that referenced this issue Aug 24, 2024
satanTime pushed a commit to windmichael/ng-mocks that referenced this issue Aug 25, 2024
satanTime added a commit that referenced this issue Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant