Skip to content

Commit cbbadda

Browse files
committed
rearrange files
1 parent 26e835c commit cbbadda

11 files changed

+548
-540
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {HarnessPredicate} from '@angular/cdk/testing';
10+
import {MatListHarnessBase} from './list-harness-base';
11+
import {ActionListHarnessFilters, ActionListItemHarnessFilters} from './list-harness-filters';
12+
import {getListItemPredicate, MatListItemHarnessBase} from './list-item-harness-base';
13+
14+
/** Harness for interacting with a standard mat-action-list in tests. */
15+
export class MatActionListHarness extends MatListHarnessBase<
16+
typeof MatActionListItemHarness, MatActionListItemHarness, ActionListItemHarnessFilters> {
17+
/** The selector for the host element of a `MatActionList` instance. */
18+
static hostSelector = 'mat-action-list';
19+
20+
/**
21+
* Gets a `HarnessPredicate` that can be used to search for a `MatActionListHarness` that meets
22+
* certain criteria.
23+
* @param options Options for filtering which action list instances are considered a match.
24+
* @return a `HarnessPredicate` configured with the given options.
25+
*/
26+
static with(options: ActionListHarnessFilters = {}): HarnessPredicate<MatActionListHarness> {
27+
return new HarnessPredicate(MatActionListHarness, options);
28+
}
29+
30+
_itemHarness = MatActionListItemHarness;
31+
}
32+
33+
/** Harness for interacting with an action list item. */
34+
export class MatActionListItemHarness extends MatListItemHarnessBase {
35+
/** The selector for the host element of a `MatListItem` instance. */
36+
static hostSelector = ['mat-list-item', 'a[mat-list-item]', 'button[mat-list-item]']
37+
.map(selector => `${MatActionListHarness.hostSelector} ${selector}`)
38+
.join(',');
39+
40+
/**
41+
* Gets a `HarnessPredicate` that can be used to search for a `MatActionListItemHarness` that
42+
* meets certain criteria.
43+
* @param options Options for filtering which action list item instances are considered a match.
44+
* @return a `HarnessPredicate` configured with the given options.
45+
*/
46+
static with(options: ActionListItemHarnessFilters = {}):
47+
HarnessPredicate<MatActionListItemHarness> {
48+
return getListItemPredicate(MatActionListItemHarness, options);
49+
}
50+
51+
/** Clicks on the action list item. */
52+
async click(): Promise<void> {
53+
return (await this.host()).click();
54+
}
55+
56+
/** Focuses the action list item. */
57+
async focus(): Promise<void> {
58+
return (await this.host()).focus();
59+
}
60+
61+
/** Blurs the action list item. */
62+
async blur(): Promise<void> {
63+
return (await this.host()).blur();
64+
}
65+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {
10+
ComponentHarness,
11+
ComponentHarnessConstructor,
12+
HarnessPredicate
13+
} from '@angular/cdk/testing';
14+
import {DividerHarnessFilters, MatDividerHarness} from '@angular/material/divider/testing';
15+
import {BaseListItemHarnessFilters, SubheaderHarnessFilters} from './list-harness-filters';
16+
import {MatSubheaderHarness} from './list-item-harness-base';
17+
18+
/** Represents a section of a list falling under a specific header. */
19+
export interface ListSection<I> {
20+
/** The heading for this list section. `undefined` if there is no heading. */
21+
heading?: string;
22+
23+
/** The items in this list section. */
24+
items: I[];
25+
}
26+
27+
/**
28+
* Shared behavior among the harnesses for the various `MatList` flavors.
29+
* @template T A constructor type for a list item harness type used by this list harness.
30+
* @template C The list item harness type that `T` constructs.
31+
* @template F The filter type used filter list item harness of type `C`.
32+
* @docs-private
33+
*/
34+
export class MatListHarnessBase
35+
<
36+
T extends (ComponentHarnessConstructor<C> & {with: (options?: F) => HarnessPredicate<C>}),
37+
C extends ComponentHarness,
38+
F extends BaseListItemHarnessFilters
39+
> extends ComponentHarness {
40+
protected _itemHarness: T;
41+
42+
/**
43+
* Gets a list of harnesses representing the items in this list.
44+
* @param filters Optional filters used to narrow which harnesses are included
45+
* @return The list of items matching the given filters.
46+
*/
47+
async getItems(filters?: F): Promise<C[]> {
48+
return this.locatorForAll(this._itemHarness.with(filters))();
49+
}
50+
51+
/**
52+
* Gets a list of `ListSection` representing the list items grouped by subheaders. If the list has
53+
* no subheaders it is represented as a single `ListSection` with an undefined `heading` property.
54+
* @param filters?? Optional filters used to narrow which list item harnesses are included
55+
* @return The list of items matching the given filters, grouped into sections by subheader.
56+
*/
57+
async getItemsGroupedBySubheader(filters?: F): Promise<ListSection<C>[]> {
58+
const listSections = [];
59+
let currentSection: ListSection<C> = {items: []};
60+
const itemsAndSubheaders =
61+
await this.getItemsWithSubheadersAndDividers({item: filters, divider: false});
62+
for (const itemOrSubheader of itemsAndSubheaders) {
63+
if (itemOrSubheader instanceof MatSubheaderHarness) {
64+
if (currentSection.heading !== undefined || currentSection.items.length) {
65+
listSections.push(currentSection);
66+
}
67+
currentSection = {heading: await itemOrSubheader.getText(), items: []};
68+
} else {
69+
currentSection.items.push(itemOrSubheader);
70+
}
71+
}
72+
if (currentSection.heading !== undefined || currentSection.items.length ||
73+
!listSections.length) {
74+
listSections.push(currentSection);
75+
}
76+
return listSections;
77+
}
78+
79+
/**
80+
* Gets a list of sub-lists representing the list items grouped by dividers. If the list has no
81+
* dividers it is represented as a list with a single sub-list.
82+
* @param filters Optional filters used to narrow which list item harnesses are included
83+
* @return The list of items matching the given filters, grouped into sub-lists by divider.
84+
*/
85+
async getItemsGroupedByDividers(filters?: F): Promise<C[][]> {
86+
const listSections = [];
87+
let currentSection = [];
88+
const itemsAndDividers =
89+
await this.getItemsWithSubheadersAndDividers({item: filters, subheader: false});
90+
for (const itemOrDivider of itemsAndDividers) {
91+
if (itemOrDivider instanceof MatDividerHarness) {
92+
listSections.push(currentSection);
93+
currentSection = [];
94+
} else {
95+
currentSection.push(itemOrDivider);
96+
}
97+
}
98+
listSections.push(currentSection);
99+
return listSections;
100+
}
101+
102+
/**
103+
* Gets a list of harnesses representing all of the items, subheaders, and dividers
104+
* (in the order they appear in the list). Use `instanceof` to check which type of harness a given
105+
* item is.
106+
* @param filters Optional filters used to narrow which list items, subheaders, and dividers are
107+
* included. A value of `false` for the `item`, `subheader`, or `divider` properties indicates
108+
* that the respective harness type should be omitted completely.
109+
* @return The list of harnesses representing the items, subheaders, and dividers matching the
110+
* given filters.
111+
*/
112+
getItemsWithSubheadersAndDividers(filters: {
113+
item: false,
114+
subheader: false,
115+
divider: false
116+
}): Promise<[]>;
117+
getItemsWithSubheadersAndDividers(filters: {
118+
item?: F | false,
119+
subheader: false,
120+
divider: false
121+
}): Promise<C[]>;
122+
getItemsWithSubheadersAndDividers(filters: {
123+
item: false,
124+
subheader?: SubheaderHarnessFilters | false,
125+
divider: false
126+
}): Promise<MatSubheaderHarness[]>;
127+
getItemsWithSubheadersAndDividers(filters: {
128+
item: false,
129+
subheader: false,
130+
divider?: DividerHarnessFilters | false
131+
}): Promise<MatDividerHarness[]>;
132+
getItemsWithSubheadersAndDividers(filters: {
133+
item?: F | false,
134+
subheader?: SubheaderHarnessFilters | false,
135+
divider: false
136+
}): Promise<(C | MatSubheaderHarness)[]>;
137+
getItemsWithSubheadersAndDividers(filters: {
138+
item?: F | false,
139+
subheader: false,
140+
divider?: false | DividerHarnessFilters
141+
}): Promise<(C | MatDividerHarness)[]>;
142+
getItemsWithSubheadersAndDividers(filters: {
143+
item: false,
144+
subheader?: false | SubheaderHarnessFilters,
145+
divider?: false | DividerHarnessFilters
146+
}): Promise<(MatSubheaderHarness | MatDividerHarness)[]>;
147+
getItemsWithSubheadersAndDividers(filters?: {
148+
item?: F | false,
149+
subheader?: SubheaderHarnessFilters | false,
150+
divider?: DividerHarnessFilters | false
151+
}): Promise<(C | MatSubheaderHarness | MatDividerHarness)[]>;
152+
async getItemsWithSubheadersAndDividers(filters: {
153+
item?: F | false,
154+
subheader?: SubheaderHarnessFilters | false,
155+
divider?: DividerHarnessFilters | false
156+
} = {}): Promise<(C | MatSubheaderHarness | MatDividerHarness)[]> {
157+
const query = [];
158+
if (filters.item !== false) {
159+
query.push(this._itemHarness.with(filters.item || {} as F));
160+
}
161+
if (filters.subheader !== false) {
162+
query.push(MatSubheaderHarness.with(filters.subheader));
163+
}
164+
if (filters.divider !== false) {
165+
query.push(MatDividerHarness.with(filters.divider));
166+
}
167+
return this.locatorForAll(...query)();
168+
}
169+
}

src/material/list/testing/list-harness.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
MatListHarness,
66
MatNavListHarness,
77
MatSelectionListHarness
8-
} from './list-harness';
9-
import {MatListItemHarnessBase, MatSubheaderHarness} from './list-item-harness';
8+
} from './list-harness-base';
9+
import {MatListItemHarnessBase, MatSubheaderHarness} from './list-item-harness-base';
1010
import {runHarnessTests} from './shared.spec';
1111

1212
describe('Non-MDC-based list harnesses', () => {

0 commit comments

Comments
 (0)