0
0
Angularframework~20 mins

Testing routing and navigation in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Routing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 }
];
AThe <code>NotFoundComponent</code> is rendered showing a 404 message.
BThe app crashes with a routing error.
CThe <code>HomeComponent</code> is rendered by default.
DNothing is rendered and the page stays blank.
Attempts:
2 left
💡 Hint
Look at the wildcard route ** in the routes array.
state_output
intermediate
2: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);
}
A'/profile/undefined'
B'/profile/42'
C'/profile'
D'/profile?id=42'
Attempts:
2 left
💡 Hint
Check how path parameters are passed in the navigate method.
🔧 Debug
advanced
2: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');
}
AThe router is not injected properly in the test.
BThe spy is set after the method call, so it misses the call.
CThe test should use <code>fakeAsync</code> to detect navigation calls.
DThe component calls <code>navigateByUrl</code> but the spy watches <code>navigate</code>, so the spy is not triggered.
Attempts:
2 left
💡 Hint
Check which router method is called in the component versus which is spied on.
📝 Syntax
advanced
2: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?
A{ provide: ActivatedRoute, useValue: { queryParams: { search: 'test' } } }
B{ provide: ActivatedRoute, useValue: { queryParams: new BehaviorSubject({ search: 'test' }) } }
C{ provide: ActivatedRoute, useValue: { queryParams: of({ search: 'test' }) } }
D{ provide: ActivatedRoute, useValue: { queryParams: () => ({ search: 'test' }) } }
Attempts:
2 left
💡 Hint
Remember that queryParams is an Observable in Angular.
🧠 Conceptual
expert
2: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?
ARouterTestingModule provides a simplified router setup that avoids real navigation and allows mocking routes easily.
BRouterTestingModule automatically runs all routes in a real browser environment during tests.
CRouterTestingModule disables routing completely to speed up tests.
DRouterTestingModule replaces the router with a synchronous version that blocks until navigation completes.
Attempts:
2 left
💡 Hint
Think about how tests should avoid side effects like real navigation.