Skip to content

Commit 26e835c

Browse files
committed
address comments
1 parent 1048a4c commit 26e835c

File tree

4 files changed

+56
-47
lines changed

4 files changed

+56
-47
lines changed

src/cdk/testing/component-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ export class HarnessPredicate<T extends ComponentHarness> {
412412
* @param pattern The pattern the value is expected to match. If `pattern` is a string,
413413
* `value` is expected to match exactly. If `pattern` is a regex, a partial match is
414414
* allowed. If `pattern` is `null`, the value is expected to be `null`.
415-
* @return A Promise that resolves to whether the value matches the pattern.
415+
* @return Whether the value matches the pattern.
416416
*/
417417
static async stringMatches(value: string | null | Promise<string | null>,
418418
pattern: string | RegExp | null): Promise<boolean> {

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import {
10-
BaseHarnessFilters,
1110
ComponentHarness,
1211
ComponentHarnessConstructor,
1312
HarnessPredicate
@@ -26,7 +25,8 @@ import {
2625
ListItemHarnessFilters,
2726
ListOptionHarnessFilters,
2827
NavListHarnessFilters,
29-
NavListItemHarnessFilters, SelectionListHarnessFilters,
28+
NavListItemHarnessFilters,
29+
SelectionListHarnessFilters,
3030
SubheaderHarnessFilters
3131
} from './list-harness-filters';
3232
import {
@@ -48,13 +48,14 @@ export interface ListSection<I> {
4848

4949
/**
5050
* Shared behavior among the harnesses for the various `MatList` flavors.
51+
* @template T A constructor type for a list item harness type used by this list harness.
52+
* @template C The list item harness type that `T` constructs.
53+
* @template F The filter type used filter list item harness of type `C`.
5154
* @docs-private
5255
*/
5356
export class MatListHarnessBase
5457
<
55-
T extends (ComponentHarnessConstructor<C> & {
56-
with: (options?: BaseHarnessFilters) => HarnessPredicate<C>
57-
}),
58+
T extends (ComponentHarnessConstructor<C> & {with: (options?: F) => HarnessPredicate<C>}),
5859
C extends ComponentHarness,
5960
F extends BaseListItemHarnessFilters
6061
> extends ComponentHarness {
@@ -72,14 +73,14 @@ export class MatListHarnessBase
7273
/**
7374
* Gets a list of `ListSection` representing the list items grouped by subheaders. If the list has
7475
* no subheaders it is represented as a single `ListSection` with an undefined `heading` property.
75-
* @param filters Optional filters used to narrow which list item harnesses are included
76+
* @param filters?? Optional filters used to narrow which list item harnesses are included
7677
* @return The list of items matching the given filters, grouped into sections by subheader.
7778
*/
78-
async getItemsBySubheader(filters?: F): Promise<ListSection<C>[]> {
79+
async getItemsGroupedBySubheader(filters?: F): Promise<ListSection<C>[]> {
7980
const listSections = [];
8081
let currentSection: ListSection<C> = {items: []};
8182
const itemsAndSubheaders =
82-
await this.getItemsSubheadersAndDividers({item: filters, divider: false});
83+
await this.getItemsWithSubheadersAndDividers({item: filters, divider: false});
8384
for (const itemOrSubheader of itemsAndSubheaders) {
8485
if (itemOrSubheader instanceof MatSubheaderHarness) {
8586
if (currentSection.heading !== undefined || currentSection.items.length) {
@@ -103,11 +104,11 @@ export class MatListHarnessBase
103104
* @param filters Optional filters used to narrow which list item harnesses are included
104105
* @return The list of items matching the given filters, grouped into sub-lists by divider.
105106
*/
106-
async getItemsDivided(filters?: F): Promise<C[][]> {
107+
async getItemsGroupedByDividers(filters?: F): Promise<C[][]> {
107108
const listSections = [];
108109
let currentSection = [];
109110
const itemsAndDividers =
110-
await this.getItemsSubheadersAndDividers({item: filters, subheader: false});
111+
await this.getItemsWithSubheadersAndDividers({item: filters, subheader: false});
111112
for (const itemOrDivider of itemsAndDividers) {
112113
if (itemOrDivider instanceof MatDividerHarness) {
113114
listSections.push(currentSection);
@@ -130,47 +131,47 @@ export class MatListHarnessBase
130131
* @return The list of harnesses representing the items, subheaders, and dividers matching the
131132
* given filters.
132133
*/
133-
getItemsSubheadersAndDividers(filters: {
134+
getItemsWithSubheadersAndDividers(filters: {
134135
item: false,
135136
subheader: false,
136137
divider: false
137138
}): Promise<[]>;
138-
getItemsSubheadersAndDividers(filters: {
139+
getItemsWithSubheadersAndDividers(filters: {
139140
item?: F | false,
140141
subheader: false,
141142
divider: false
142143
}): Promise<C[]>;
143-
getItemsSubheadersAndDividers(filters: {
144+
getItemsWithSubheadersAndDividers(filters: {
144145
item: false,
145146
subheader?: SubheaderHarnessFilters | false,
146147
divider: false
147148
}): Promise<MatSubheaderHarness[]>;
148-
getItemsSubheadersAndDividers(filters: {
149+
getItemsWithSubheadersAndDividers(filters: {
149150
item: false,
150151
subheader: false,
151152
divider?: DividerHarnessFilters | false
152153
}): Promise<MatDividerHarness[]>;
153-
getItemsSubheadersAndDividers(filters: {
154+
getItemsWithSubheadersAndDividers(filters: {
154155
item?: F | false,
155156
subheader?: SubheaderHarnessFilters | false,
156157
divider: false
157158
}): Promise<(C | MatSubheaderHarness)[]>;
158-
getItemsSubheadersAndDividers(filters: {
159+
getItemsWithSubheadersAndDividers(filters: {
159160
item?: F | false,
160161
subheader: false,
161162
divider?: false | DividerHarnessFilters
162163
}): Promise<(C | MatDividerHarness)[]>;
163-
getItemsSubheadersAndDividers(filters: {
164+
getItemsWithSubheadersAndDividers(filters: {
164165
item: false,
165166
subheader?: false | SubheaderHarnessFilters,
166167
divider?: false | DividerHarnessFilters
167168
}): Promise<(MatSubheaderHarness | MatDividerHarness)[]>;
168-
getItemsSubheadersAndDividers(filters?: {
169+
getItemsWithSubheadersAndDividers(filters?: {
169170
item?: F | false,
170171
subheader?: SubheaderHarnessFilters | false,
171172
divider?: DividerHarnessFilters | false
172173
}): Promise<(C | MatSubheaderHarness | MatDividerHarness)[]>;
173-
async getItemsSubheadersAndDividers(filters: {
174+
async getItemsWithSubheadersAndDividers(filters: {
174175
item?: F | false,
175176
subheader?: SubheaderHarnessFilters | false,
176177
divider?: DividerHarnessFilters | false
@@ -274,18 +275,18 @@ export class MatSelectionListHarness extends MatListHarnessBase<
274275
* Selects all items matching any of the given filters.
275276
* @param filters Filters that specify which items should be selected.
276277
*/
277-
async checkItems(...filters: ListOptionHarnessFilters[]): Promise<void> {
278+
async selectItems(...filters: ListOptionHarnessFilters[]): Promise<void> {
278279
const items = await this._getItems(filters);
279-
await Promise.all(items.map(item => item.check()));
280+
await Promise.all(items.map(item => item.select()));
280281
}
281282

282283
/**
283284
* Deselects all items matching any of the given filters.
284285
* @param filters Filters that specify which items should be deselected.
285286
*/
286-
async uncheckItems(...filters: ListItemHarnessFilters[]): Promise<void> {
287+
async deselectItems(...filters: ListItemHarnessFilters[]): Promise<void> {
287288
const items = await this._getItems(filters);
288-
await Promise.all(items.map(item => item.uncheck()));
289+
await Promise.all(items.map(item => item.deselect()));
289290
}
290291

291292
/** Gets all items matching the given list of filters. */

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ import {
2525
SubheaderHarnessFilters
2626
} from './list-harness-filters';
2727

28-
function getListItemPredicate
29-
<H extends MatListItemHarnessBase, F extends BaseListItemHarnessFilters>
30-
(harnessType: ComponentHarnessConstructor<H>, options: F): HarnessPredicate<H> {
28+
/**
29+
* Gets a `HarnessPredicate` that applies the given `BaseListItemHarnessFilters` to the given
30+
* list item harness.
31+
* @template H The type of list item harness to create a predicate for.
32+
* @param harnessType A constructor for a list item harness.
33+
* @param options An instance of `BaseListItemHarnessFilters` to apply.
34+
* @return A `HarnessPredicate` for the given harness type with the given options applied.
35+
*/
36+
function getListItemPredicate<H extends MatListItemHarnessBase>(
37+
harnessType: ComponentHarnessConstructor<H>,
38+
options: BaseListItemHarnessFilters): HarnessPredicate<H> {
3139
return new HarnessPredicate(harnessType, options)
3240
.addOption('text', options.text,
3341
(harness, text) => HarnessPredicate.stringMatches(harness.getText(), text));
@@ -230,7 +238,7 @@ export class MatListOptionHarness extends MatListItemHarnessBase {
230238
* Puts the list option in a checked state by toggling it if it is currently unchecked, or doing
231239
* nothing if it is already checked.
232240
*/
233-
async check() {
241+
async select() {
234242
if (!await this.isSelected()) {
235243
return this.toggle();
236244
}
@@ -240,7 +248,7 @@ export class MatListOptionHarness extends MatListItemHarnessBase {
240248
* Puts the list option in an unchecked state by toggling it if it is currently checked, or doing
241249
* nothing if it is already unchecked.
242250
*/
243-
async uncheck() {
251+
async deselect() {
244252
if (await this.isSelected()) {
245253
return this.toggle();
246254
}

src/material/list/testing/shared.spec.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function runBaseListFunctionalityTests<
7878
});
7979

8080
it('should get items by subheader', async () => {
81-
const sections = await simpleListHarness.getItemsBySubheader();
81+
const sections = await simpleListHarness.getItemsGroupedBySubheader();
8282
expect(sections.length).toBe(3);
8383
expect(sections[0].heading).toBeUndefined();
8484
expect(await Promise.all(sections[0].items.map(i => i.getText()))).toEqual(['Item 1']);
@@ -90,28 +90,28 @@ function runBaseListFunctionalityTests<
9090
});
9191

9292
it('should get items by subheader for an empty list', async () => {
93-
const sections = await emptyListHarness.getItemsBySubheader();
93+
const sections = await emptyListHarness.getItemsGroupedBySubheader();
9494
expect(sections.length).toBe(1);
9595
expect(sections[0].heading).toBeUndefined();
9696
expect(sections[0].items.length).toBe(0);
9797
});
9898

9999
it('should get items grouped by divider', async () => {
100-
const sections = await simpleListHarness.getItemsDivided();
100+
const sections = await simpleListHarness.getItemsGroupedByDividers();
101101
expect(sections.length).toBe(3);
102102
expect(await Promise.all(sections[0].map(i => i.getText()))).toEqual(['Item 1']);
103103
expect(await Promise.all(sections[1].map(i => i.getText()))).toEqual(['Item 2', 'Item 3']);
104104
expect(sections[2].length).toBe(0);
105105
});
106106

107107
it('should get items grouped by divider for an empty list', async () => {
108-
const sections = await emptyListHarness.getItemsDivided();
108+
const sections = await emptyListHarness.getItemsGroupedByDividers();
109109
expect(sections.length).toBe(1);
110110
expect(sections[0].length).toBe(0);
111111
});
112112

113113
it('should get all items, subheaders, and dividers', async () => {
114-
const itemsSubheadersAndDividers = await simpleListHarness.getItemsSubheadersAndDividers();
114+
const itemsSubheadersAndDividers = await simpleListHarness.getItemsWithSubheadersAndDividers();
115115
expect(itemsSubheadersAndDividers.length).toBe(7);
116116
expect(itemsSubheadersAndDividers[0] instanceof listItemHarnessBase).toBe(true);
117117
expect(await (itemsSubheadersAndDividers[0] as MatListItemHarnessBase).getText())
@@ -134,11 +134,11 @@ function runBaseListFunctionalityTests<
134134

135135
it('should get all items, subheaders, and dividers excluding some harness types', async () => {
136136
const items =
137-
await simpleListHarness.getItemsSubheadersAndDividers({subheader: false, divider: false});
137+
await simpleListHarness.getItemsWithSubheadersAndDividers({subheader: false, divider: false});
138138
const subheaders =
139-
await simpleListHarness.getItemsSubheadersAndDividers({item: false, divider: false});
139+
await simpleListHarness.getItemsWithSubheadersAndDividers({item: false, divider: false});
140140
const dividers =
141-
await simpleListHarness.getItemsSubheadersAndDividers({item: false, subheader: false});
141+
await simpleListHarness.getItemsWithSubheadersAndDividers({item: false, subheader: false});
142142
expect(await Promise.all(items.map(i => i.getText())))
143143
.toEqual(['Item 1', 'Item 2', 'Item 3']);
144144
expect(await Promise.all(subheaders.map(s => s.getText())))
@@ -148,7 +148,7 @@ function runBaseListFunctionalityTests<
148148
});
149149

150150
it('should get all items, subheaders, and dividers with filters', async () => {
151-
const itemsSubheadersAndDividers = await simpleListHarness.getItemsSubheadersAndDividers({
151+
const itemsSubheadersAndDividers = await simpleListHarness.getItemsWithSubheadersAndDividers({
152152
item: {text: /1/},
153153
subheader: {text: /2/}
154154
});
@@ -328,22 +328,22 @@ export function runHarnessTests(
328328
it('should get all selected options', async () => {
329329
expect((await harness.getItems({selected: true})).length).toBe(0);
330330
const items = await harness.getItems();
331-
await Promise.all(items.map(item => item.check()));
331+
await Promise.all(items.map(item => item.select()));
332332
expect((await harness.getItems({selected: true})).length).toBe(3);
333333
});
334334

335335
it('should check multiple options', async () => {
336336
expect((await harness.getItems({selected: true})).length).toBe(0);
337-
await harness.checkItems({text: /1/}, {text: /3/});
337+
await harness.selectItems({text: /1/}, {text: /3/});
338338
const selected = await harness.getItems({selected: true});
339339
expect(await Promise.all(selected.map(item => item.getText())))
340340
.toEqual(['Item 1', 'Item 3']);
341341
});
342342

343343
it('should uncheck multiple options', async () => {
344-
await harness.checkItems();
344+
await harness.selectItems();
345345
expect((await harness.getItems({selected: true})).length).toBe(3);
346-
await harness.uncheckItems({text: /1/}, {text: /3/});
346+
await harness.deselectItems({text: /1/}, {text: /3/});
347347
const selected = await harness.getItems({selected: true});
348348
expect(await Promise.all(selected.map(item => item.getText()))).toEqual(['Item 2']);
349349
});
@@ -369,20 +369,20 @@ export function runHarnessTests(
369369
const items = await harness.getItems();
370370
expect(items.length).toBe(3);
371371
expect(await items[0].isSelected()).toBe(false);
372-
await items[0].check();
372+
await items[0].select();
373373
expect(await items[0].isSelected()).toBe(true);
374-
await items[0].check();
374+
await items[0].select();
375375
expect(await items[0].isSelected()).toBe(true);
376376
});
377377

378378
it('should uncheck single option', async () => {
379379
const items = await harness.getItems();
380380
expect(items.length).toBe(3);
381-
await items[0].check();
381+
await items[0].select();
382382
expect(await items[0].isSelected()).toBe(true);
383-
await items[0].uncheck();
383+
await items[0].deselect();
384384
expect(await items[0].isSelected()).toBe(false);
385-
await items[0].uncheck();
385+
await items[0].deselect();
386386
expect(await items[0].isSelected()).toBe(false);
387387
});
388388

0 commit comments

Comments
 (0)