-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(icon): add test harness #20072
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
feat(icon): add test harness #20072
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
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,22 @@ | ||
/** | ||
* @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'; | ||
|
||
/** Possible types of icons. */ | ||
export const enum IconType {SVG, FONT} | ||
|
||
/** A set of criteria that can be used to filter a list of `MatIconHarness` instances. */ | ||
export interface IconHarnessFilters extends BaseHarnessFilters { | ||
/** Filters based on the typef of the icon. */ | ||
type?: IconType; | ||
/** Filters based on the name of the icon. */ | ||
name?: string | RegExp; | ||
/** Filters based on the namespace of the icon. */ | ||
namespace?: string | null | 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 {MatIconModule, MatIconRegistry} from '@angular/material/icon'; | ||
import {runHarnessTests} from '@angular/material/icon/testing/shared.spec'; | ||
import {MatIconHarness} from './icon-harness'; | ||
|
||
describe('Non-MDC-based MatIconHarness', () => { | ||
runHarnessTests(MatIconModule, MatIconRegistry, MatIconHarness); | ||
}); |
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,68 @@ | ||
/** | ||
* @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 {IconHarnessFilters, IconType} from './icon-harness-filters'; | ||
|
||
|
||
/** Harness for interacting with a standard mat-icon in tests. */ | ||
export class MatIconHarness extends ComponentHarness { | ||
/** The selector for the host element of a `MatIcon` instance. */ | ||
static hostSelector = '.mat-icon'; | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for a `MatIconHarness` that meets | ||
* certain criteria. | ||
* @param options Options for filtering which icon instances are considered a match. | ||
* @return a `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with(options: IconHarnessFilters = {}): HarnessPredicate<MatIconHarness> { | ||
return new HarnessPredicate(MatIconHarness, options) | ||
.addOption('type', options.type, | ||
async (harness, type) => (await harness.getType()) === type) | ||
.addOption('name', options.name, | ||
(harness, text) => HarnessPredicate.stringMatches(harness.getName(), text)) | ||
.addOption('namespace', options.namespace, | ||
(harness, text) => HarnessPredicate.stringMatches(harness.getNamespace(), text)); | ||
} | ||
|
||
/** Gets the type of the icon. */ | ||
async getType(): Promise<IconType> { | ||
const type = await (await this.host()).getAttribute('data-mat-icon-type'); | ||
return type === 'svg' ? IconType.SVG : IconType.FONT; | ||
} | ||
|
||
/** Gets the name of the icon. */ | ||
async getName(): Promise<string | null> { | ||
const host = await this.host(); | ||
const nameFromDom = await host.getAttribute('data-mat-icon-name'); | ||
|
||
// If we managed to figure out the name from the attribute, use it. | ||
if (nameFromDom) { | ||
return nameFromDom; | ||
} | ||
|
||
// Some icons support defining the icon as a ligature. | ||
// As a fallback, try to extract it from the DOM text. | ||
if (await this.getType() === IconType.FONT) { | ||
return host.text(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** Gets the namespace of the icon. */ | ||
async getNamespace(): Promise<string | null> { | ||
return (await this.host()).getAttribute('data-mat-icon-namespace'); | ||
} | ||
|
||
/** Gets whether the icon is inline. */ | ||
async isInline(): Promise<boolean> { | ||
return (await this.host()).hasClass('mat-icon-inline'); | ||
} | ||
} |
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,106 @@ | ||
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 {MatIconModule, MatIconRegistry} from '@angular/material/icon'; | ||
import {MatIconHarness} from '@angular/material/icon/testing/icon-harness'; | ||
import {DomSanitizer} from '@angular/platform-browser'; | ||
import {IconType} from './icon-harness-filters'; | ||
|
||
/** Shared tests to run on both the original and MDC-based icons. */ | ||
export function runHarnessTests( | ||
iconModule: typeof MatIconModule, | ||
iconRegistry: typeof MatIconRegistry, | ||
iconHarness: typeof MatIconHarness) { | ||
let fixture: ComponentFixture<IconHarnessTest>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [iconModule], | ||
declarations: [IconHarnessTest], | ||
}).compileComponents(); | ||
|
||
const registry = TestBed.inject(iconRegistry); | ||
const sanitizer = TestBed.inject(DomSanitizer); | ||
|
||
registry.addSvgIconLiteralInNamespace('svgIcons', 'svgIcon', | ||
sanitizer.bypassSecurityTrustHtml('<svg></svg>')); | ||
fixture = TestBed.createComponent(IconHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
}); | ||
|
||
it('should load all icon harnesses', async () => { | ||
const icons = await loader.getAllHarnesses(iconHarness); | ||
expect(icons.length).toBe(3); | ||
}); | ||
|
||
it('should filter icon harnesses based on their type', async () => { | ||
const [svgIcons, fontIcons] = await Promise.all([ | ||
loader.getAllHarnesses(iconHarness.with({type: IconType.SVG})), | ||
loader.getAllHarnesses(iconHarness.with({type: IconType.FONT})) | ||
]); | ||
|
||
expect(svgIcons.length).toBe(1); | ||
expect(fontIcons.length).toBe(2); | ||
}); | ||
|
||
it('should filter icon harnesses based on their name', async () => { | ||
const [regexFilterResults, stringFilterResults] = await Promise.all([ | ||
loader.getAllHarnesses(iconHarness.with({name: /^font/})), | ||
loader.getAllHarnesses(iconHarness.with({name: 'fontIcon'})) | ||
]); | ||
|
||
expect(regexFilterResults.length).toBe(1); | ||
expect(stringFilterResults.length).toBe(1); | ||
}); | ||
|
||
it('should filter icon harnesses based on their namespace', async () => { | ||
const [regexFilterResults, stringFilterResults, nullFilterResults] = await Promise.all([ | ||
loader.getAllHarnesses(iconHarness.with({namespace: /^font/})), | ||
loader.getAllHarnesses(iconHarness.with({namespace: 'svgIcons'})), | ||
loader.getAllHarnesses(iconHarness.with({namespace: null})) | ||
]); | ||
|
||
expect(regexFilterResults.length).toBe(1); | ||
expect(stringFilterResults.length).toBe(1); | ||
expect(nullFilterResults.length).toBe(1); | ||
}); | ||
|
||
it('should get the type of each icon', async () => { | ||
const icons = await loader.getAllHarnesses(iconHarness); | ||
const types = await Promise.all(icons.map(icon => icon.getType())); | ||
expect(types).toEqual([IconType.FONT, IconType.SVG, IconType.FONT]); | ||
}); | ||
|
||
it('should get the name of an icon', async () => { | ||
const icons = await loader.getAllHarnesses(iconHarness); | ||
const names = await Promise.all(icons.map(icon => icon.getName())); | ||
expect(names).toEqual(['fontIcon', 'svgIcon', 'ligature_icon']); | ||
}); | ||
|
||
it('should get the namespace of an icon', async () => { | ||
const icons = await loader.getAllHarnesses(iconHarness); | ||
const namespaces = await Promise.all(icons.map(icon => icon.getNamespace())); | ||
expect(namespaces).toEqual(['fontIcons', 'svgIcons', null]); | ||
}); | ||
|
||
it('should get whether an icon is inline', async () => { | ||
const icons = await loader.getAllHarnesses(iconHarness); | ||
const inlineStates = await Promise.all(icons.map(icon => icon.isInline())); | ||
expect(inlineStates).toEqual([false, false, true]); | ||
}); | ||
|
||
} | ||
|
||
@Component({ | ||
template: ` | ||
<mat-icon fontSet="fontIcons" fontIcon="fontIcon"></mat-icon> | ||
<mat-icon svgIcon="svgIcons:svgIcon"></mat-icon> | ||
<mat-icon inline>ligature_icon</mat-icon> | ||
` | ||
}) | ||
class IconHarnessTest { | ||
} | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the naming of this and
getName
. Technically they correspond to two different inputs depending on whether it's an SVG icon (fontSet
/svgIcon
vsfontIcon
/svgIcon
), but they basically mean the same thing so I decided to combine them under the same methods. I'm open to suggestions and potentially separating them out so they mirror the inputs.