31
loading...
This website collects cookies to deliver better user experience
@angular/cdk/testing
API to understand how we work with component test harnesses.@angular/cdk/testing
supports test harnesses in Protractor for e2e and Karma for unit tests. If you use other testing frameworks, you'll have to find an implementation of TestBedHarnessEnvironment
for your framework of choice.HarnessLoader
for your test suite, you'll set it up as part of your TestBed
setup by passing in the ComponentFixture
instance.import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
describe('Tidy Test', () => {
let fixture: ComponentFixture<TidyTestComponent>;
let loader: HarnessLoader;
beforeEach(() => {
TestBed.configureTestingModule({...});
fixture = TestBed.createComponent(TidyTestComponent);
loader = TestbedHarnessEnvironment.loader(fixture);
});
});
HarnessLoader
compatible with elements contained inside the fixture's DOM root. If you need to test an element outside the fixture's DOM root, such as a dialog, you can create a HarnessLoader
at the DOM root.const rootLoader = TestbedHarnessEnvironment.documentRootLoader(fixture);
HarnessLoader
, we can get the component test harnesses we want to interact with. The HarnessLoader
and component test harnesses return promises, so you'll use the async/await
pattern.getHarness
method. The method returns a test harness for the first requested test harness element type it comes across or rejects the promise if none exists.const btn: MatButtonHarness = await loader.getHarness(MatButtonHarness);
getAllHarnesses
method. In this example, the method returns an array of the requested test harness element type MatButtonHarness
.const btns: MatButtonHarness[] = await loader.getAllHarnesses(MatButtonHarness);
const childLoader: HarnessLoader = await loader.getChildLoader('.my-selector');
const
MatButtonHarness
can filter by text to find all MatButtonHarness
with the text "delete" like this.const btns: MatButtonHarness[] = await loader.getAllHarnesses(MatButtonHarness.with({text: 'delete'}));
MatCheckBoxHarness
, for example. You will have to read each component test harness documentation or inspect its API.TestElement
host. The TestElement
is like the DebugElement
's nativeElement
. With the TestElement
, you can hover, click, blur, access class list and dimensions, and more! Access the TestElement
like thisconst btn: MatButtonHarness = await loader.getHarness(MatButtonHarness);
const btnHost: TestElement = await btn.host();
await btn.hover();
parallel
, which parallelizes the async operations and optimizes for change detection! For example, you can get the checked status and the label for a checkbox in one go like this.const checkbox: MatCheckboxHarness = await loader.getHarness(MatCheckboxHarness);
const [checked, label] = await parallel(() => [
checkbox.isChecked(),
checkbox.getLabelText()
]);
HarnessLoader
to get test harnesses and ways we can interact with them. But what if you don't work with Material? Or what if you have custom UI components in your app? You can use the @angular/cdk/testing
API to create custom component harnesses. We'll talk about how to do that in the next post in the series.