The guard is not modifying the route in Jest tests. Angular 17

64 views Asked by At

My guard has a condition that, if false: changes the route path to '/maintenance'. The associated service is responsible for controlling the boolean variable that determines whether the site is online or offline.

During the execution of the development server using the 'ng s' command, the guard's behavior is consistent. If I force the value to true, it allows normal access to all routes. If I force the value to false, it automatically redirects the route to '/maintenance' on any attempt to access, including non-existent routes.

However, during tests, this logic does not unfold as expected. The service, by default, initializes the variable isSiteOnline as true and provides get and set functions to manipulate this value. When I use the setIsSiteOnline method in my mockSiteStatusService in the test files, the value is indeed changed, as confirmed by using getIsSiteOnline. However, the route, which is managed by the guard, does not reflect this change during tests.

My goal is to be able to test the dynamic route changes performed by the guard during tests, ensuring that the redirection logic is effective in response to changes in the state of the variable controlled by the service.

maintenance.guard.ts

maintenance.guard.spec.ts

siteStatusService.ts

siteStatusService.spec.ts

app.routes.ts

package.json

I have already tried reversing the logic, forcing isSiteOnline to false and adapting the project accordingly. However, in this scenario, the route consistently remains at '/maintenance' during tests and does not change to '/' or any other route, as expected.

0

There are 0 answers