3 Testing components

 

This chapter covers

  • Testing components
  • Knowing the differences between isolated and shallow tests
  • Testing classes and functions

Angular applications are built from components, so the most important place to start when testing an Angular application is with component tests. For example, imagine a component that displays a calendar. It might enable a user to select a date, change the selected date, cycle through months and years, and so on. You need to write a test case for each of these pieces of functionality.

In this chapter, we’ll cover key testing classes and functions, such as TestBed, ComponentFixture, and fakeAsync, which help you test your components. You’ll need a good grasp of these classes and functions to write component tests.

Don’t worry if you've never heard of these concepts before. You’ll practice using them as we go. By the end of this chapter, you’ll understand what components are and know how to write tests for them. Let’s kick off the chapter by looking at some basic component tests.

3.1 Basic component tests

The best way to get comfortable writing component tests is to write a few tests for a basic component. In this section, you’ll write tests for the ContactsComponent component. ContactsComponent has almost no functionality and will be easy to test.

3.2 Real-world component testing

3.2.1 Importing the dependencies

3.2.2 Setting up the tests

3.2.3 Adding the tests

Summary