Skip to content

Commit db999ff

Browse files
authored
feat(material/slide-toggle/testing): polish harness API (#17416)
1 parent 9e79940 commit db999ff

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

src/material-experimental/mdc-slide-toggle/testing/slide-toggle-harness.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export class MatSlideToggleHarness extends ComponentHarness {
2828
static with(options: SlideToggleHarnessFilters = {}): HarnessPredicate<MatSlideToggleHarness> {
2929
return new HarnessPredicate(MatSlideToggleHarness, options)
3030
.addOption('label', options.label,
31-
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label));
31+
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
32+
// We want to provide a filter option for "name" because the name of the slide-toggle is
33+
// only set on the underlying input. This means that it's not possible for developers
34+
// to retrieve the harness of a specific checkbox with name through a CSS selector.
35+
.addOption('name', options.name, async (harness, name) => await harness.getName() === name);
3236
}
3337

3438
private _label = this.locatorFor('label');

src/material/slide-toggle/testing/shared.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ export function runHarnessTests(
4343
expect(await slideToggles[0].getLabelText()).toBe('Second');
4444
});
4545

46+
it('should load slide-toggle with name', async () => {
47+
const slideToggles = await loader.getAllHarnesses(
48+
slideToggleHarness.with({name: 'first-name'}));
49+
expect(slideToggles.length).toBe(1);
50+
expect(await slideToggles[0].getLabelText()).toBe('First');
51+
});
52+
4653
it('should get checked state', async () => {
4754
const [checkedToggle, uncheckedToggle] = await loader.getAllHarnesses(slideToggleHarness);
4855
expect(await checkedToggle.isChecked()).toBe(true);

src/material/slide-toggle/testing/slide-toggle-harness-filters.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import {BaseHarnessFilters} from '@angular/cdk/testing';
1010

1111
export interface SlideToggleHarnessFilters extends BaseHarnessFilters {
1212
label?: string | RegExp;
13+
name?: string;
1314
}

src/material/slide-toggle/testing/slide-toggle-harness.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export class MatSlideToggleHarness extends ComponentHarness {
2828
static with(options: SlideToggleHarnessFilters = {}): HarnessPredicate<MatSlideToggleHarness> {
2929
return new HarnessPredicate(MatSlideToggleHarness, options)
3030
.addOption('label', options.label,
31-
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label));
31+
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
32+
// We want to provide a filter option for "name" because the name of the slide-toggle is
33+
// only set on the underlying input. This means that it's not possible for developers
34+
// to retrieve the harness of a specific checkbox with name through a CSS selector.
35+
.addOption('name', options.name, async (harness, name) => await harness.getName() === name);
3236
}
3337

3438
private _label = this.locatorFor('label');

0 commit comments

Comments
 (0)