Component Testing

Tutorial 2 of 4

Angular Component Testing Tutorial

1. Introduction

Goal of the Tutorial

This tutorial aims to provide a thorough understanding of component testing in Angular. By the end of this tutorial, you will learn how to write and run comprehensive unit tests for Angular components.

Learning Outcomes

  • Understand the basics of component testing
  • Learn how to write unit tests for Angular components
  • Run tests on Angular components

Prerequisites

  • Basic understanding of Angular and TypeScript
  • Familiarity with Angular CLI
  • Basic understanding of testing and Jest testing framework

2. Step-by-Step Guide

Component testing is an essential part of the software development process, especially in Angular applications. Component tests ensure that individual components (like buttons, input fields, etc.) function as expected.

Writing Unit Tests

In Angular, we usually write unit tests in a separate .spec.ts file. We use the describe function to group related tests, and it function to define a single test.

Running Tests

You can run the tests using the Angular CLI command ng test.

Best Practices and Tips

  • Write isolated tests: Each test should cover only one functionality.
  • Use the Arrange-Act-Assert pattern: Set up the conditions for the test, act by executing some function, and then assert that the expected result has occurred.

3. Code Examples

Example 1: Testing a Button

Let's write a test for a button component.

// button.component.spec.ts
import { ButtonComponent } from './button.component';

describe('ButtonComponent', () => {
  let component: ButtonComponent;

  beforeEach(() => {
    component = new ButtonComponent();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

In this code, we're creating an instance of ButtonComponent and then checking if it's created successfully.

Example 2: Testing an Input Field

// input.component.spec.ts
import { InputComponent } from './input.component';

describe('InputComponent', () => {
  let component: InputComponent;

  beforeEach(() => {
    component = new InputComponent();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should emit valueChange event on input change', () => {
    spyOn(component.valueChange, 'emit');

    const value = 'New Input Value';
    component.onChange(value);

    expect(component.valueChange.emit).toHaveBeenCalledWith(value);
  });
});

This test checks whether the valueChange event is correctly emitting the new input value when the onChange method is called.

4. Summary

In this tutorial, you learned the basics of component testing in Angular, wrote unit tests for Angular components, and ran tests using Angular CLI.

Keep practicing to master these concepts. You can also explore Angular's documentation for more details.

5. Practice Exercises

  1. Write a test for a CheckboxComponent that checks whether a valueChange event is emitted when the checkbox state changes.
  2. Write a test for a ListComponent that checks whether the correct number of items are rendered when the items input prop changes.

Remember to follow the Arrange-Act-Assert pattern, and isolate each test. Happy testing!