-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(material-experimental/mdc-snack-bar): add harness support for get…
…ting label and actions (#23585) * fix(material-experimental/mdc-snack-bar): add harness support for getting label and actions of mdc-snack-bar with custom content * fixup! fix(material-experimental/mdc-snack-bar): add harness support for getting label and actions
- Loading branch information
Showing
8 changed files
with
167 additions
and
46 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
83 changes: 82 additions & 1 deletion
83
src/material-experimental/mdc-snack-bar/testing/snack-bar-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 |
---|---|---|
@@ -1,7 +1,88 @@ | ||
import {MatSnackBarModule, MatSnackBar} from '@angular/material-experimental/mdc-snack-bar'; | ||
import { | ||
MatSnackBar, | ||
MatSnackBarConfig, | ||
MatSnackBarModule | ||
} from '@angular/material-experimental/mdc-snack-bar'; | ||
import {runHarnessTests} from '@angular/material/snack-bar/testing/shared.spec'; | ||
import {MatSnackBarHarness} from './snack-bar-harness'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {HarnessLoader} from '@angular/cdk/testing'; | ||
import {NoopAnimationsModule} from '@angular/platform-browser/animations'; | ||
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; | ||
import {Component, TemplateRef, ViewChild} from '@angular/core'; | ||
|
||
describe('MDC-based MatSnackBarHarness', () => { | ||
runHarnessTests(MatSnackBarModule, MatSnackBar, MatSnackBarHarness as any); | ||
}); | ||
|
||
describe('MDC-based MatSnackBarHarness (MDC only behavior)', () => { | ||
let fixture: ComponentFixture<SnackbarHarnessTest>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MatSnackBarModule, NoopAnimationsModule], | ||
declarations: [SnackbarHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(SnackbarHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.documentRootLoader(fixture); | ||
}); | ||
|
||
it('should be able to get message of a snack-bar with custom content', async () => { | ||
fixture.componentInstance.openCustom(); | ||
let snackBar = await loader.getHarness(MatSnackBarHarness); | ||
expect(await snackBar.getMessage()).toBe('My custom snack-bar.'); | ||
|
||
fixture.componentInstance.openCustomWithAction(); | ||
snackBar = await loader.getHarness(MatSnackBarHarness); | ||
expect(await snackBar.getMessage()).toBe('My custom snack-bar with action.'); | ||
}); | ||
|
||
it('should fail to get action description of a snack-bar with no action', async () => { | ||
fixture.componentInstance.openCustom(); | ||
const snackBar = await loader.getHarness(MatSnackBarHarness); | ||
await expectAsync(snackBar.getActionDescription()).toBeRejectedWithError(/without an action/); | ||
}); | ||
|
||
it('should be able to get action description of a snack-bar with an action', async () => { | ||
fixture.componentInstance.openCustomWithAction(); | ||
const snackBar = await loader.getHarness(MatSnackBarHarness); | ||
expect(await snackBar.getActionDescription()).toBe('Ok'); | ||
}); | ||
|
||
it('should be able to check whether a snack-bar with custom content has an action', async () => { | ||
fixture.componentInstance.openCustom(); | ||
let snackBar = await loader.getHarness(MatSnackBarHarness); | ||
expect(await snackBar.hasAction()).toBe(false); | ||
|
||
fixture.componentInstance.openCustomWithAction(); | ||
snackBar = await loader.getHarness(MatSnackBarHarness); | ||
expect(await snackBar.hasAction()).toBe(true); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
template: ` | ||
<ng-template #custom>My custom snack-bar.</ng-template> | ||
<ng-template #customWithAction> | ||
<span matSnackBarLabel>My custom snack-bar with action.</span> | ||
<div matSnackBarActions><button matSnackBarAction>Ok</button></div> | ||
</ng-template> | ||
` | ||
}) | ||
class SnackbarHarnessTest { | ||
@ViewChild('custom') customTmpl: TemplateRef<any>; | ||
@ViewChild('customWithAction') customWithActionTmpl: TemplateRef<any>; | ||
|
||
constructor(public snackBar: MatSnackBar) {} | ||
|
||
openCustom(config?: MatSnackBarConfig) { | ||
return this.snackBar.openFromTemplate(this.customTmpl, config); | ||
} | ||
|
||
openCustomWithAction(config?: MatSnackBarConfig) { | ||
return this.snackBar.openFromTemplate(this.customWithActionTmpl, config); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,7 +1,59 @@ | ||
import {MatSnackBarModule, MatSnackBar} from '@angular/material/snack-bar'; | ||
import {MatSnackBar, MatSnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar'; | ||
import {runHarnessTests} from '@angular/material/snack-bar/testing/shared.spec'; | ||
import {MatSnackBarHarness} from './snack-bar-harness'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {HarnessLoader} from '@angular/cdk/testing'; | ||
import {NoopAnimationsModule} from '@angular/platform-browser/animations'; | ||
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; | ||
import {Component, TemplateRef, ViewChild} from '@angular/core'; | ||
|
||
describe('Non-MDC-based MatSnackBarHarness', () => { | ||
runHarnessTests(MatSnackBarModule, MatSnackBar, MatSnackBarHarness); | ||
}); | ||
|
||
describe('Non-MDC-based MatSnackBarHarness (non-MDC only behavior)', () => { | ||
let fixture: ComponentFixture<SnackbarHarnessTest>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MatSnackBarModule, NoopAnimationsModule], | ||
declarations: [SnackbarHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(SnackbarHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.documentRootLoader(fixture); | ||
}); | ||
|
||
it('should fail to get message of a snack-bar with custom content', async () => { | ||
fixture.componentInstance.openCustom(); | ||
const snackBar = await loader.getHarness(MatSnackBarHarness); | ||
await expectAsync(snackBar.getMessage()).toBeRejectedWithError(/custom content/); | ||
}); | ||
|
||
it('should fail to get action description of a snack-bar with custom content', async () => { | ||
fixture.componentInstance.openCustom(); | ||
const snackBar = await loader.getHarness(MatSnackBarHarness); | ||
await expectAsync(snackBar.getActionDescription()).toBeRejectedWithError(/custom content/); | ||
}); | ||
|
||
it('should fail to check whether a snack-bar with custom content has an action', async () => { | ||
fixture.componentInstance.openCustom(); | ||
const snackBar = await loader.getHarness(MatSnackBarHarness); | ||
await expectAsync(snackBar.hasAction()).toBeRejectedWithError(/custom content/); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
template: `<ng-template>My custom snack-bar.</ng-template>` | ||
}) | ||
class SnackbarHarnessTest { | ||
@ViewChild(TemplateRef) customTmpl: TemplateRef<any>; | ||
|
||
constructor(public snackBar: MatSnackBar) {} | ||
|
||
openCustom(config?: MatSnackBarConfig) { | ||
return this.snackBar.openFromTemplate(this.customTmpl, config); | ||
} | ||
} |
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