Skip to content

Commit b04af89

Browse files
authored
docs(material/bottom-sheet): add harness example for bottom sheet (#21455)
1 parent 714f70f commit b04af89

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

src/components-examples/material/bottom-sheet/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/bottom-sheet",
1313
deps = [
14+
"//src/cdk/overlay",
15+
"//src/cdk/testing",
16+
"//src/cdk/testing/testbed",
1417
"//src/material/bottom-sheet",
18+
"//src/material/bottom-sheet/testing",
1519
"//src/material/button",
1620
"//src/material/list",
21+
"@npm//@angular/platform-browser",
22+
"@npm//@angular/platform-browser-dynamic",
23+
"@npm//@types/jasmine",
1724
],
1825
)
1926

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ng-template>
2+
Hello from the bottom sheet!
3+
</ng-template>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {TestBed, ComponentFixture, waitForAsync, inject} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatBottomSheetHarness} from '@angular/material/bottom-sheet/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatBottomSheetModule} from '@angular/material/bottom-sheet';
8+
import {BottomSheetHarnessExample} from './bottom-sheet-harness-example';
9+
import {OverlayContainer} from '@angular/cdk/overlay';
10+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
11+
12+
describe('BottomSheetHarnessExample', () => {
13+
let fixture: ComponentFixture<BottomSheetHarnessExample>;
14+
let loader: HarnessLoader;
15+
16+
beforeAll(() => {
17+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
18+
});
19+
20+
beforeEach(
21+
waitForAsync(() => {
22+
TestBed.configureTestingModule({
23+
imports: [MatBottomSheetModule, NoopAnimationsModule],
24+
declarations: [BottomSheetHarnessExample]
25+
}).compileComponents();
26+
}));
27+
28+
beforeEach(() => {
29+
fixture = TestBed.createComponent(BottomSheetHarnessExample);
30+
fixture.detectChanges();
31+
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
32+
inject([OverlayContainer], () => {})();
33+
});
34+
35+
it('should load harness for a bottom sheet', async () => {
36+
fixture.componentInstance.open();
37+
const bottomSheets = await loader.getAllHarnesses(MatBottomSheetHarness);
38+
expect(bottomSheets.length).toBe(1);
39+
});
40+
41+
it('should be able to get aria-label of the bottom sheet', async () => {
42+
fixture.componentInstance.open({ariaLabel: 'Confirm purchase.'});
43+
const bottomSheet = await loader.getHarness(MatBottomSheetHarness);
44+
expect(await bottomSheet.getAriaLabel()).toBe('Confirm purchase.');
45+
});
46+
47+
it('should be able to dismiss the bottom sheet', async () => {
48+
fixture.componentInstance.open();
49+
let bottomSheets = await loader.getAllHarnesses(MatBottomSheetHarness);
50+
51+
expect(bottomSheets.length).toBe(1);
52+
await bottomSheets[0].dismiss();
53+
54+
bottomSheets = await loader.getAllHarnesses(MatBottomSheetHarness);
55+
expect(bottomSheets.length).toBe(0);
56+
});
57+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Component, TemplateRef, ViewChild} from '@angular/core';
2+
import {MatBottomSheet, MatBottomSheetConfig} from '@angular/material/bottom-sheet';
3+
4+
/**
5+
* @title Testing with MatBottomSheetHarness
6+
*/
7+
@Component({
8+
selector: 'bottom-sheet-harness-example',
9+
templateUrl: 'bottom-sheet-harness-example.html',
10+
})
11+
export class BottomSheetHarnessExample {
12+
@ViewChild(TemplateRef) template: TemplateRef<any>;
13+
14+
constructor(readonly bottomSheet: MatBottomSheet) {}
15+
16+
open(config?: MatBottomSheetConfig) {
17+
return this.bottomSheet.open(this.template, config);
18+
}
19+
}

src/components-examples/material/bottom-sheet/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
BottomSheetOverviewExample,
77
BottomSheetOverviewExampleSheet
88
} from './bottom-sheet-overview/bottom-sheet-overview-example';
9+
import {BottomSheetHarnessExample} from './bottom-sheet-harness/bottom-sheet-harness-example';
910

1011
export {
12+
BottomSheetHarnessExample,
1113
BottomSheetOverviewExample,
1214
BottomSheetOverviewExampleSheet,
1315
};
1416

1517
const EXAMPLES = [
18+
BottomSheetHarnessExample,
1619
BottomSheetOverviewExample,
1720
BottomSheetOverviewExampleSheet,
1821
];

0 commit comments

Comments
 (0)