Skip to content

Commit

Permalink
feat(material/slide-toggle/testing): polish harness API (#17416)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba authored Oct 16, 2019
1 parent 9e79940 commit db999ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export class MatSlideToggleHarness extends ComponentHarness {
static with(options: SlideToggleHarnessFilters = {}): HarnessPredicate<MatSlideToggleHarness> {
return new HarnessPredicate(MatSlideToggleHarness, options)
.addOption('label', options.label,
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label));
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
// We want to provide a filter option for "name" because the name of the slide-toggle is
// only set on the underlying input. This means that it's not possible for developers
// to retrieve the harness of a specific checkbox with name through a CSS selector.
.addOption('name', options.name, async (harness, name) => await harness.getName() === name);
}

private _label = this.locatorFor('label');
Expand Down
7 changes: 7 additions & 0 deletions src/material/slide-toggle/testing/shared.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export function runHarnessTests(
expect(await slideToggles[0].getLabelText()).toBe('Second');
});

it('should load slide-toggle with name', async () => {
const slideToggles = await loader.getAllHarnesses(
slideToggleHarness.with({name: 'first-name'}));
expect(slideToggles.length).toBe(1);
expect(await slideToggles[0].getLabelText()).toBe('First');
});

it('should get checked state', async () => {
const [checkedToggle, uncheckedToggle] = await loader.getAllHarnesses(slideToggleHarness);
expect(await checkedToggle.isChecked()).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import {BaseHarnessFilters} from '@angular/cdk/testing';

export interface SlideToggleHarnessFilters extends BaseHarnessFilters {
label?: string | RegExp;
name?: string;
}
6 changes: 5 additions & 1 deletion src/material/slide-toggle/testing/slide-toggle-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export class MatSlideToggleHarness extends ComponentHarness {
static with(options: SlideToggleHarnessFilters = {}): HarnessPredicate<MatSlideToggleHarness> {
return new HarnessPredicate(MatSlideToggleHarness, options)
.addOption('label', options.label,
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label));
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
// We want to provide a filter option for "name" because the name of the slide-toggle is
// only set on the underlying input. This means that it's not possible for developers
// to retrieve the harness of a specific checkbox with name through a CSS selector.
.addOption('name', options.name, async (harness, name) => await harness.getName() === name);
}

private _label = this.locatorFor('label');
Expand Down

0 comments on commit db999ff

Please sign in to comment.