Skip to content

feat(material-experimental/mdc-list): add support for activated state in harness #24934

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

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ActionListItemHarnessFilters extends BaseListItemHarnessFilters

export interface NavListItemHarnessFilters extends BaseListItemHarnessFilters {
href?: string | RegExp | null;
activated?: boolean;
}

export interface ListOptionHarnessFilters extends BaseListItemHarnessFilters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ describe('MatNavListHarness', () => {
expect(items.length).toBe(1);
expect(await items[0].getHref()).toBe('/somestuff');
});

it('should get activated state', async () => {
const items = await harness.getItems();
const activated = await parallel(() => items.map(item => item.isActivated()));
expect(activated).toEqual([false, true, false, false, false]);
});

it('should get item harness by activated state', async () => {
const activatedItems = await harness.getItems({activated: true});
const activatedItemsText = await parallel(() =>
activatedItems.map(item => item.getFullText()),
);
expect(activatedItemsText).toEqual(['Item 2']);
});
});
});

Expand Down Expand Up @@ -587,7 +601,7 @@ class ActionListHarnessTest {
</a>
<div matSubheader>Section 1</div>
<mat-divider></mat-divider>
<a mat-list-item href (click)="onClick($event, 'Item 2')">
<a mat-list-item activated href (click)="onClick($event, 'Item 2')">
<span class="test-item-content">Item 2</span>
</a>
<a
Expand Down
19 changes: 14 additions & 5 deletions src/material-experimental/mdc-list/testing/nav-list-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ export class MatNavListItemHarness extends MatListItemHarnessBase {
this: ComponentHarnessConstructor<T>,
options: NavListItemHarnessFilters = {},
): HarnessPredicate<T> {
return getListItemPredicate(this, options).addOption(
'href',
options.href,
async (harness, href) => HarnessPredicate.stringMatches(harness.getHref(), href),
);
return getListItemPredicate(this, options)
.addOption('href', options.href, async (harness, href) =>
HarnessPredicate.stringMatches(harness.getHref(), href),
)
.addOption(
'activated',
options.activated,
async (harness, activated) => (await harness.isActivated()) === activated,
);
}

/** Gets the href for this nav list item. */
Expand All @@ -82,4 +86,9 @@ export class MatNavListItemHarness extends MatListItemHarnessBase {
async isFocused(): Promise<boolean> {
return (await this.host()).isFocused();
}

/** Whether the list item is activated. Should only be used for nav list items. */
async isActivated(): Promise<boolean> {
return (await this.host()).hasClass('mdc-list-item--activated');
}
}