-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(core): add test harnesses for mat-option and mat-optgroup #17940
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ ts_library( | |
deps = [ | ||
"//src/cdk/coercion", | ||
"//src/cdk/testing", | ||
"//src/material/core/testing", | ||
], | ||
) | ||
|
||
|
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,55 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library") | ||
|
||
ts_library( | ||
name = "testing", | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = ["**/*.spec.ts"], | ||
), | ||
module_name = "@angular/material/core/testing", | ||
deps = [ | ||
"//src/cdk/testing", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "source-files", | ||
srcs = glob(["**/*.ts"]), | ||
) | ||
|
||
ng_test_library( | ||
name = "harness_tests_lib", | ||
srcs = [ | ||
"optgroup-shared.spec.ts", | ||
"option-shared.spec.ts", | ||
], | ||
deps = [ | ||
":testing", | ||
"//src/cdk/testing", | ||
"//src/cdk/testing/testbed", | ||
"//src/material/core", | ||
], | ||
) | ||
|
||
ng_test_library( | ||
name = "unit_tests_lib", | ||
srcs = glob( | ||
["**/*.spec.ts"], | ||
exclude = [ | ||
"option-shared.spec.ts", | ||
"optgroup-shared.spec.ts", | ||
], | ||
), | ||
deps = [ | ||
":harness_tests_lib", | ||
":testing", | ||
"//src/material/core", | ||
], | ||
) | ||
|
||
ng_web_test_suite( | ||
name = "unit_tests", | ||
deps = [":unit_tests_lib"], | ||
) |
This file contains hidden or 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'; |
This file contains hidden or 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; | ||
} |
This file contains hidden or 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/core'; | ||
import {runHarnessTests} from './optgroup-shared.spec'; | ||
import {MatOptgroupHarness} from './optgroup-harness'; | ||
|
||
describe('Non-MDC-based MatOptgroupHarness', () => { | ||
runHarnessTests(MatOptionModule, MatOptgroupHarness); | ||
}); |
This file contains hidden or 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 a `mat-optgroup` in tests. */ | ||
export class MatOptgroupHarness extends ComponentHarness { | ||
/** Selector used to locate option group instances. */ | ||
static hostSelector = '.mat-optgroup'; | ||
private _label = this.locatorFor('.mat-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 this.host()).hasClass('mat-optgroup-disabled'); | ||
} | ||
|
||
/** | ||
* 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))(); | ||
} | ||
} | ||
|
This file contains hidden or 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,80 @@ | ||
import {HarnessLoader} from '@angular/cdk/testing'; | ||
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; | ||
import {Component} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {MatOptionModule} from '@angular/material/core'; | ||
import {MatOptgroupHarness} from './optgroup-harness'; | ||
|
||
/** Shared tests to run on both the original and MDC-based option groups. */ | ||
export function runHarnessTests( | ||
optionModule: typeof MatOptionModule, optionGroupHarness: typeof MatOptgroupHarness) { | ||
let fixture: ComponentFixture<OptgroupHarnessTest>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [optionModule], | ||
declarations: [OptgroupHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(OptgroupHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
}); | ||
|
||
it('should load all option group harnesses', async () => { | ||
const groups = await loader.getAllHarnesses(optionGroupHarness); | ||
expect(groups.length).toBe(2); | ||
}); | ||
|
||
it('should filter groups based on their text', async () => { | ||
const groups = await loader.getAllHarnesses(optionGroupHarness.with({ | ||
labelText: 'Disabled group' | ||
})); | ||
|
||
expect(groups.length).toBe(1); | ||
}); | ||
|
||
it('should filter group label text by a pattern', async () => { | ||
const groups = await loader.getAllHarnesses(optionGroupHarness.with({labelText: /group/})); | ||
expect(groups.length).toBe(2); | ||
}); | ||
|
||
it('should get the group label text', async () => { | ||
const groups = await loader.getAllHarnesses(optionGroupHarness); | ||
const texts = await Promise.all(groups.map(group => group.getLabelText())); | ||
expect(texts).toEqual(['Plain group', 'Disabled group']); | ||
}); | ||
|
||
it('should get the group disabled state', async () => { | ||
const groups = await loader.getAllHarnesses(optionGroupHarness); | ||
const disabledStates = await Promise.all(groups.map(group => group.isDisabled())); | ||
expect(disabledStates).toEqual([false, true]); | ||
}); | ||
|
||
it('should get the options inside the groups', async () => { | ||
const group = await loader.getHarness(optionGroupHarness); | ||
const optionTexts = await group.getOptions().then(async options => { | ||
return await Promise.all(options.map(option => option.getText())); | ||
}); | ||
|
||
expect(optionTexts).toEqual(['Option 1', 'Option 2']); | ||
}); | ||
} | ||
|
||
|
||
@Component({ | ||
template: ` | ||
<mat-optgroup label="Plain group"> | ||
<mat-option>Option 1</mat-option> | ||
<mat-option>Option 2</mat-option> | ||
</mat-optgroup> | ||
|
||
<mat-optgroup label="Disabled group" disabled> | ||
<mat-option>Disabled option 1</mat-option> | ||
</mat-optgroup> | ||
` | ||
}) | ||
class OptgroupHarnessTest { | ||
} | ||
|
This file contains hidden or 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; | ||
crisbeto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isSelected?: boolean; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.