7 Testing the router

 

This chapter covers

  • An overview of the router and what it does in single-page applications
  • Configuring the router for an Angular application
  • Testing components that use the router
  • Testing advanced router configuration options

Almost every Angular application needs a way to convert the web address in the location bar to some destination in the web application, and that’s where the router comes in.

In simple applications, such as the Contacts app you use throughout this book, the router configuration may only involve associating a URL path with a component, but you can do much more with the router. For example, your application may have sections that a user can only access if they have permission to see the data. A router can verify the user’s credentials before even loading that part of the application.

In this chapter, you’ll learn more about the router, including how to configure it, and go through some examples of testing both the router code and components that need to use it. Understanding how to test the router in your application builds on skills you’ve already learned for testing components and services, so make sure you understand the chapters on those topics before continuing with the material in this chapter.

7.1 What is the Angular router?

7.1.1 Configuring the router

7.1.2 Route guards: the router’s lifecycle hooks

7.2 Testing routed components

7.2.1 Testing router navigation with RouterTestingModule

7.2.2 Testing router parameters

7.3 Testing advanced routes

7.3.1 Route guards

7.3.2 Resolving data before loading a route

Summary

sitemap