|
| 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 | +} |
0 commit comments