Components

Testing Components

These tests require creating the component's host element in the browser DOM, and investigating the component class's interaction with the DOM as described by its template.

Angular TestBed facilitates this kind of testing, but in many cases, testing the component class alone, without DOM involvement can validate much of the component's behavior.

circle-info

For tests without DOM interaction you will be:

  1. Creating the Component with the new keyword, like in Services.

  2. Asseting expectations on its public data and methods.

Components are more than just a Class, they interact with DOM and with other components.

Only testing a component's class cannot tell if the component is going to render properly, respond to user input and gestures, or integrate with its parent and child components.

Writing these kind of tests will require TestBed as well as other testing helpers.

circle-info

When you create a Component with CLI, an initial test file is created.

The created .spec file has some boilerplate code antecipating more advanced tests.

The bare minimum code for DOM testing is only:

createComponent()

This method freezes the current TestBed definition, closing it to further configuration.

triangle-exclamation

createComponent returns a ComponentFixture.

ComponentFixture

Which is a test harness for interacting with the created component and its corresponding element.

circle-info

Check more of it properties and methods herearrow-up-right.

DOM Elements Access

nativeElement

ComponentFixture.nativeElement has an any type.

Angular cannot know at compile time what kind of HTMLElement the nativeElement ir or if it event is an HTMLElement. (The application might be running on a non-browser plataform, such as the server or a Web Worker)

Knowing that it is an HTMLElement, you may use querySelector to dive deeper into the element tree.

DebugElement

Angular fixture provides the component's element directly through the fixture.nativeElement, which is only a convenience method.

Angular real path to nativeElement is through fixture.debugElement.nativeElement.

This is because nativeElement depend upon the runtime environment, but you could be running these tests on a non-browser plataform, as said before.

So Angular relies on the DebugElement abstraction to work safely across all supported plataforms.

Instead of creating an HTML element tree, Angular creates a DebugElement tree that wraps the native elements for the runtime plataform.

circle-exclamation

By.css()

By.css(), is a static method to select DebugElement nodes with a standard CSS selector.

It also returns DebugElement, so you must unwrap it to nativeElement.

detectChanges()

createComponent does not bind data, meaning that if you try to test data from signals or other bindings it might not have the expected value.

This happens because createComponent does not trigger change detection by default.

For dealing with triggering change detection, use fixture.detectChanges().

A provider configured in TestBed to enable automatic change detection.

circle-exclamation

Examples

Testing Components with async pipe

When testing the components, we will mock the ApiService.getFruits() since the API should not be reached in the test.

Last updated