Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material-experimenta/mdc-core): add test harnesses for MDC optio…
…n and option group Adds test harnesses for the MDC-based `MatOption` and `MatOptgroup`.
- Loading branch information
1 parent
d7754e7
commit 2d4cab2
Showing
12 changed files
with
230 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ts_library( | ||
name = "testing", | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = ["**/*.spec.ts"], | ||
), | ||
module_name = "@angular/material-experimental/mdc-core/testing", | ||
deps = [ | ||
"//src/cdk/testing", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "source-files", | ||
srcs = glob(["**/*.ts"]), | ||
) | ||
|
||
ng_test_library( | ||
name = "unit_tests_lib", | ||
srcs = glob(["**/*.spec.ts"]), | ||
deps = [ | ||
":testing", | ||
"//src/material-experimental/mdc-core", | ||
"//src/material/core/testing:harness_tests_lib", | ||
], | ||
) | ||
|
||
ng_web_test_suite( | ||
name = "unit_tests", | ||
deps = [":unit_tests_lib"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export * from './public-api'; |
13 changes: 13 additions & 0 deletions
13
src/material-experimental/mdc-core/testing/optgroup-harness-filters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {BaseHarnessFilters} from '@angular/cdk/testing'; | ||
|
||
export interface OptgroupHarnessFilters extends BaseHarnessFilters { | ||
labelText?: string | RegExp; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/material-experimental/mdc-core/testing/optgroup-harness.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {MatOptionModule} from '@angular/material-experimental/mdc-core'; | ||
import {runHarnessTests} from '@angular/material/core/testing/optgroup-shared.spec'; | ||
import {MatOptgroupHarness} from './optgroup-harness'; | ||
|
||
describe('MDC-based MatOptgroupHarness', () => { | ||
runHarnessTests(MatOptionModule, MatOptgroupHarness as any); | ||
}); |
51 changes: 51 additions & 0 deletions
51
src/material-experimental/mdc-core/testing/optgroup-harness.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; | ||
import {OptgroupHarnessFilters} from './optgroup-harness-filters'; | ||
import {MatOptionHarness} from './option-harness'; | ||
import {OptionHarnessFilters} from './option-harness-filters'; | ||
|
||
/** Harness for interacting with an MDC-based `mat-optgroup` in tests. */ | ||
export class MatOptgroupHarness extends ComponentHarness { | ||
/** Selector used to locate option group instances. */ | ||
static hostSelector = '.mat-mdc-optgroup'; | ||
private _label = this.locatorFor('.mat-mdc-optgroup-label'); | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for a `MatOptgroupHarness` that meets | ||
* certain criteria. | ||
* @param options Options for filtering which option instances are considered a match. | ||
* @return a `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with(options: OptgroupHarnessFilters = {}) { | ||
return new HarnessPredicate(MatOptgroupHarness, options) | ||
.addOption('labelText', options.labelText, | ||
async (harness, title) => | ||
HarnessPredicate.stringMatches(await harness.getLabelText(), title)); | ||
} | ||
|
||
/** Gets the option group's label text. */ | ||
async getLabelText(): Promise<string> { | ||
return (await this._label()).text(); | ||
} | ||
|
||
/** Gets whether the option group is disabled. */ | ||
async isDisabled(): Promise<boolean> { | ||
return (await (await this.host()).getAttribute('aria-disabled')) === 'true'; | ||
} | ||
|
||
/** | ||
* Gets the options that are inside the group. | ||
* @param filter Optionally filters which options are included. | ||
*/ | ||
async getOptions(filter: OptionHarnessFilters = {}): Promise<MatOptionHarness[]> { | ||
return this.locatorForAll(MatOptionHarness.with(filter))(); | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
src/material-experimental/mdc-core/testing/option-harness-filters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {BaseHarnessFilters} from '@angular/cdk/testing'; | ||
|
||
export interface OptionHarnessFilters extends BaseHarnessFilters { | ||
text?: string | RegExp; | ||
isSelected?: boolean; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/material-experimental/mdc-core/testing/option-harness.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {MatOptionModule, MatOption} from '@angular/material-experimental/mdc-core'; | ||
import {runHarnessTests} from '@angular/material/core/testing/option-shared.spec'; | ||
import {MatOptionHarness} from './option-harness'; | ||
|
||
describe('MDC-based MatOptionHarness', () => { | ||
runHarnessTests(MatOptionModule, MatOptionHarness as any, MatOption); | ||
}); |
62 changes: 62 additions & 0 deletions
62
src/material-experimental/mdc-core/testing/option-harness.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; | ||
import {OptionHarnessFilters} from './option-harness-filters'; | ||
|
||
/** Harness for interacting with an MDC-based `mat-option` in tests. */ | ||
export class MatOptionHarness extends ComponentHarness { | ||
/** Selector used to locate option instances. */ | ||
static hostSelector = '.mat-mdc-option'; | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for a `MatOptionsHarness` that meets | ||
* certain criteria. | ||
* @param options Options for filtering which option instances are considered a match. | ||
* @return a `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with(options: OptionHarnessFilters = {}) { | ||
return new HarnessPredicate(MatOptionHarness, options) | ||
.addOption('text', options.text, | ||
async (harness, title) => | ||
HarnessPredicate.stringMatches(await harness.getText(), title)) | ||
.addOption('isSelected', options.isSelected, | ||
async (harness, isSelected) => await harness.isSelected() === isSelected); | ||
|
||
} | ||
|
||
/** Clicks the option. */ | ||
async click(): Promise<void> { | ||
return (await this.host()).click(); | ||
} | ||
|
||
/** Gets the option's label text. */ | ||
async getText(): Promise<string> { | ||
return (await this.host()).text(); | ||
} | ||
|
||
/** Gets whether the option is disabled. */ | ||
async isDisabled(): Promise<boolean> { | ||
return (await this.host()).hasClass('mdc-list-item--disabled'); | ||
} | ||
|
||
/** Gets whether the option is selected. */ | ||
async isSelected(): Promise<boolean> { | ||
return (await this.host()).hasClass('mdc-list-item--selected'); | ||
} | ||
|
||
/** Gets whether the option is active. */ | ||
async isActive(): Promise<boolean> { | ||
return (await this.host()).hasClass('mat-mdc-option-active'); | ||
} | ||
|
||
/** Gets whether the option is in multiple selection mode. */ | ||
async isMultiple(): Promise<boolean> { | ||
return (await this.host()).hasClass('mat-mdc-option-multiple'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export * from './option-harness'; | ||
export * from './option-harness-filters'; | ||
export * from './optgroup-harness'; | ||
export * from './optgroup-harness-filters'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import {MatOptionModule} from '@angular/material/core'; | ||
import {MatOptionModule, MatOption} from '@angular/material/core'; | ||
import {runHarnessTests} from './option-shared.spec'; | ||
import {MatOptionHarness} from './option-harness'; | ||
|
||
describe('Non-MDC-based MatOptionHarness', () => { | ||
runHarnessTests(MatOptionModule, MatOptionHarness); | ||
runHarnessTests(MatOptionModule, MatOptionHarness, MatOption); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters