Challenge - 5 Problems
Routing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when navigating to a non-existent route?
Given an Angular app with a routing module that defines routes for
/home and /about, what will be the rendered output if the user navigates to /contact?Angular
const routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '**', component: NotFoundComponent } ];
Attempts:
2 left
💡 Hint
Look at the wildcard route
** in the routes array.✗ Incorrect
The wildcard route
** catches any undefined paths and renders the NotFoundComponent. This prevents errors and shows a friendly 404 page.❓ state_output
intermediate2:00remaining
What is the value of
router.url after navigation?In an Angular component, after calling
router.navigate(['/profile', 42]), what will router.url contain?Angular
constructor(private router: Router) {}
async navigateToProfile() {
await this.router.navigate(['/profile', 42]);
console.log(this.router.url);
}Attempts:
2 left
💡 Hint
Check how path parameters are passed in the navigate method.
✗ Incorrect
The array ['/profile', 42] creates a URL with the segment 'profile' followed by '42', resulting in '/profile/42'.
🔧 Debug
advanced2:00remaining
Why does this test fail when checking navigation?
Consider this Angular test snippet:
it('should navigate to dashboard', () => {
const router = TestBed.inject(Router);
const spy = spyOn(router, 'navigate');
component.goToDashboard();
expect(spy).toHaveBeenCalledWith(['/dashboard']);
});
The test fails because spyOn is never called. Why?Angular
goToDashboard() {
this.router.navigateByUrl('/dashboard');
}Attempts:
2 left
💡 Hint
Check which router method is called in the component versus which is spied on.
✗ Incorrect
The component calls
navigateByUrl, but the spy watches navigate. Since these are different methods, the spy does not detect the call.📝 Syntax
advanced2:00remaining
Which option correctly mocks ActivatedRoute with query params in a test?
You want to test a component that reads query parameters from
ActivatedRoute. Which mock setup is correct?Attempts:
2 left
💡 Hint
Remember that queryParams is an Observable in Angular.
✗ Incorrect
queryParams is an Observable, so it must be mocked with an Observable like
of(). Option C correctly uses of({ search: 'test' }).🧠 Conceptual
expert2:00remaining
What is the main benefit of using RouterTestingModule in Angular tests?
Why should you use
RouterTestingModule instead of RouterModule in Angular unit tests that involve routing?Attempts:
2 left
💡 Hint
Think about how tests should avoid side effects like real navigation.
✗ Incorrect
RouterTestingModule provides a mock router environment that prevents actual navigation and lets you control routing behavior in tests.