Skip to content

feat(material/testing): Extend Angular harness testing functionality #30960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wildcardalice
Copy link

@wildcardalice wildcardalice commented Apr 25, 2025

This pull request adds the following functionalities to the harness test kit

getHarnessAtIndex

This new function works acts like getHarness, but returns an instance of the harness corresponding to the matching element at the given index. This throws an error if the index is out of bounds.

Example usage:

// Retrieves the 4th option on screen
const option = await loader.getHarnessAtIndex(MatOptionHarness, 3);

countHarnesses

This new function counts the number of matching component instances and returns the result.

Example usage:

// Expect there to be five options on the screen
expect(await loader.countHarnesses(MatOptionHarness)).toBe(5);

Filter by Label

New functionality makes it possible to fetch harnesses for certain form components using their floating label text. Previously, the user would have to either label the component with an id or class, or locate it in a form field using MatFormFieldHarness.getControl().

This affects the following harnesses:

  • MatInputHarness
  • MatSelectHarness
  • MatNativeSelectHarness
  • MatDatepickerInputHarness
  • MatDateRangeInputHarness

Example usage:

// template
<mat-form-field>
  <mat-label>Favorite food</mat-label>
  <input matInput value="Pizza"/>
</mat-form-field>

// test.ts
const input = await loader.getHarness(MatInputHarness.with({label: 'Favorite food'}));

harnesses

This facilitates testing by making it possible to fetch certain
harnesses using their floating label text (mat-label). Previously, the
user would have to locate the harness using an id or class, or by
calling MatFormFieldHarness.getControl().

This affects the following harnesses:
- MatInputHarness
- MatSelectHarness
- MatNativeSelectHarness
- MatDatepickerInputHarness
- MatDateRangeInputHarness

Tests via unit tests
@wildcardalice wildcardalice requested a review from a team as a code owner April 25, 2025 22:01
@wildcardalice wildcardalice requested review from mmalerba and andrewseguin and removed request for a team April 25, 2025 22:01
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: material/testing labels Apr 25, 2025
@wildcardalice wildcardalice changed the title feat(material/testing): Add 'label' filter to form field control harnesses feat(material/testing): Extend Angular harness testing functionality with getHarnessWithOffset, countHarnesses, and filter by label Apr 28, 2025
@wildcardalice wildcardalice changed the title feat(material/testing): Extend Angular harness testing functionality with getHarnessWithOffset, countHarnesses, and filter by label feat(material/testing): Extend Angular harness testing functionality Apr 28, 2025
@wildcardalice wildcardalice force-pushed the main branch 5 times, most recently from 19da548 to cbb2058 Compare April 29, 2025 00:13
`getHarnessAtIndex` and `countHarnesses`

These two new functions are intended to expand harness testing
functionality by providing built-in functions for commonly used
patterns.

* `getHarnessAtIndex` functions similarly to `getHarness`, but returns
  a harness for the matching component instance with the given index.
  An example use case is to fetch the nth MatOptionHarness on screen.
* `countHarnesses` simply counts the number of matching component
  instances and returns the result.

Documentation is updated to reflect this changes, and adds a missing row
for the `hasHarness` function

Manually tested using the MatInputHarness tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant