24
loading...
This website collects cookies to deliver better user experience
@angular/cdk/testing
library, in the Angular Component Development Kit(CDK). The CDK testing library supports testing interactions with components. The idea for test harnesses comes from the PageObject
pattern, used for integration style testing.A page object wraps an HTML page, or fragment, with an application-specific API, allowing you to manipulate page elements without digging around in the HTML.
– Martin Fowler, PageObject
ngClass
attribute to conditionally add the CSS class .task-completed
when the task is complete. The .task-completed
CSS class adds a strikethrough on the text.mat
prefix, so a checkbox becomes mat-checkbox
. A snippet of code to display a to-do task and handle the strikethrough behavior for a MatCheckbox
component looks something like this.<mat-checkbox
#task
[ngClass]="task.checked ? 'task-completed' : ''">
{{todo.description}}
</mat-checkbox>
task-completed
task-completed
TestBed
setup and dive right into the test.it('should apply completed class to match task completion', () => {
// 1. Access mat-checkbox and the checkbox element within
const matCb = fixture.debugElement.query(By.css('mat-checkbox'));
expect(matCb).toBeTruthy();
const cbEl = matCb.query(By.css('input'));
expect(cbEl).toBeTruthy();
// 2. Assert the checkbox element is not checked
expect(cbEl.nativeElement.checked).toBe(false);
// 3. Assert the mat-checkox doesn't contain the CSS class
expect(matCb.nativeElement.classList).not.toContain('task-completed');
// 4. Toggle the mat-checkbox to mark as checked
const cbClickEl =
fixture.debugElement.query(By.css('.mat-checkbox-inner-container'));
cbClickEl.nativeElement.click();
fixture.detectChanges();
// 5. Assert the checkbox element is checked
expect(cbEl.nativeElement.checked).toBe(true);
// 6. Assert the mat-checkbox contains the CSS class
expect(matCb.nativeElement.classList).toContain('task-completed');
});
mat-checkbox
), which has the bindings for the attribute directiveinput
within the mat-checkbox
element), which is the checkmark.mat-checkbox-inner-container
, which is the clickable element of the mat-checkbox
mat-checkbox
implementation and use potentially non-supported selectors, which could change in the future.MatCheckbox
component test harnesses. To make it easier to compare, we'll follow the same order of operations.MatCheckbox
test harnessesit('should apply completed class to match task completion', async () => {
// 1. Access the mat-checkbox
const cbHarness = await loader.getHarness(MatCheckboxHarness);
// 2. Assert the checkbox element is not checked.
expect(await cbHarness.isChecked()).toBeFalse();
// 3. Assert the mat-checkox doesn't contain the CSS class
const cbHost = await cbHarness.host();
expect(await cbHost.hasClass('task-completed')).not.toBeTrue();
// 4. Toggle the mat-checkbox to mark as checked
await cbHarness.toggle();
// 5. Assert the checkbox element is checked
expect(await cbHarness.isChecked()).toBeTrue();
// 6. Assert the mat-checkbox contains the CSS class
expect(await cbHost.hasClass('task-completed')).toBeTrue();
});
MatCheckbox
code to write this test. Everything we did was via the public API of the MatCheckboxHarness
.@angular/cdk/testing
API to better understand what we get from the library.PageObjects
or something else.