Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Component Testing in Angular

Component testing in Angular ensures that individual components work as expected. This tutorial covers the basics of component testing and how to use it effectively in your Angular applications.

What is Component Testing?

Component testing involves testing individual components in isolation to ensure they behave correctly. This includes testing their templates, data binding, inputs, outputs, and interactions with other components.

Setting Up Testing Environment

Angular comes with built-in support for testing using Jasmine and Karma. Ensure you have the necessary packages installed:

npm install --save-dev jasmine-core jasmine-spec-reporter karma karma-chrome-launcher karma-jasmine karma-jasmine-html-reporter @angular/cli @angular-devkit/build-angular

Creating a Test for a Component

Here is an example of creating a simple test for a component:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;
  let de: DebugElement;
  let el: HTMLElement;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ MyComponent ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    de = fixture.debugElement.query(By.css('h1'));
    el = de.nativeElement;
  });

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

  it('should display original title', () => {
    expect(el.textContent).toContain(component.title);
  });

  it('should display a different test title', () => {
    component.title = 'Test Title';
    fixture.detectChanges();
    expect(el.textContent).toContain('Test Title');
  });
});

Testing Component Inputs

To test component inputs, you can set the input properties directly in your test:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ MyComponent ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    component.inputProperty = 'Test Input';
    fixture.detectChanges();
  });

  it('should display the input property', () => {
    const el = fixture.debugElement.query(By.css('p')).nativeElement;
    expect(el.textContent).toContain('Test Input');
  });
});

Testing Component Outputs

To test component outputs, you can subscribe to the output event emitter in your test:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ MyComponent ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should emit the output event', () => {
    spyOn(component.outputEvent, 'emit');
    const button = fixture.debugElement.query(By.css('button')).nativeElement;
    button.click();
    expect(component.outputEvent.emit).toHaveBeenCalledWith('Test Output');
  });
});

Key Points

  • Component testing ensures individual components work as expected.
  • Use Jasmine and Karma for testing Angular components.
  • Test component inputs by setting input properties directly in your tests.
  • Test component outputs by subscribing to the output event emitter in your tests.

Conclusion

Component testing in Angular is essential for ensuring the reliability and correctness of your components. By understanding and using component testing effectively, you can create more robust and maintainable Angular applications. Happy coding!