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.
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.
The bare minimum code for DOM testing is only:
createComponent()
createComponent()This method freezes the current TestBed definition, closing it to further configuration.
Do not re-configure TestBed after calling createComponent.
createComponent returns a ComponentFixture.
ComponentFixture
ComponentFixtureWhich is a test harness for interacting with the created component and its corresponding element.
DOM Elements Access
nativeElement
nativeElementComponentFixture.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
DebugElementAngular 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.
Check more of DebugElement methods and properties here.
By.css()
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()
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.
The second and third test reveal an important limitation.
The Angular testing environment does not run change detection synchronously when updates happen inside the test case that changed the component's title.
The test must call await fixture.whenStable to wait for another of change detection.
Examples
Testing Components with async pipe
async pipeWhen testing the components, we will mock the ApiService.getFruits() since the API should not be reached in the test.
Last updated