Skip to content

Commit a7c3ab0

Browse files
crisbetojelbourn
authored andcommitted
feat(bottom-sheet): add test harness (#17618)
1 parent 924494d commit a7c3ab0

File tree

9 files changed

+220
-0
lines changed

9 files changed

+220
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
4+
5+
ts_library(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material/bottom-sheet/testing",
12+
deps = [
13+
"//src/cdk/testing",
14+
"//src/material/bottom-sheet",
15+
],
16+
)
17+
18+
filegroup(
19+
name = "source-files",
20+
srcs = glob(["**/*.ts"]),
21+
)
22+
23+
ng_test_library(
24+
name = "harness_tests_lib",
25+
srcs = ["shared.spec.ts"],
26+
deps = [
27+
":testing",
28+
"//src/cdk/overlay",
29+
"//src/cdk/testing",
30+
"//src/cdk/testing/testbed",
31+
"//src/material/bottom-sheet",
32+
"@npm//@angular/platform-browser",
33+
],
34+
)
35+
36+
ng_test_library(
37+
name = "unit_tests_lib",
38+
srcs = glob(
39+
["**/*.spec.ts"],
40+
exclude = ["shared.spec.ts"],
41+
),
42+
deps = [
43+
":harness_tests_lib",
44+
":testing",
45+
"//src/material/bottom-sheet",
46+
],
47+
)
48+
49+
ng_web_test_suite(
50+
name = "unit_tests",
51+
deps = [":unit_tests_lib"],
52+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {BaseHarnessFilters} from '@angular/cdk/testing';
10+
11+
export interface BottomSheetHarnessFilters extends BaseHarnessFilters {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {MatBottomSheetModule} from '@angular/material/bottom-sheet';
2+
import {runHarnessTests} from '@angular/material/bottom-sheet/testing/shared.spec';
3+
import {MatBottomSheetHarness} from './bottom-sheet-harness';
4+
5+
describe('Non-MDC-based MatBottomSheetHarness', () => {
6+
runHarnessTests(MatBottomSheetModule, MatBottomSheetHarness);
7+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ComponentHarness, HarnessPredicate, TestKey} from '@angular/cdk/testing';
10+
import {BottomSheetHarnessFilters} from './bottom-sheet-harness-filters';
11+
12+
/**
13+
* Harness for interacting with a standard MatBottomSheet in tests.
14+
* @dynamic
15+
*/
16+
export class MatBottomSheetHarness extends ComponentHarness {
17+
// Developers can provide a custom component or template for the
18+
// bottom sheet. The canonical parent is the ".mat-bottom-sheet-container".
19+
static hostSelector = '.mat-bottom-sheet-container';
20+
21+
/**
22+
* Gets a `HarnessPredicate` that can be used to search for a bottom sheet with
23+
* specific attributes.
24+
* @param options Options for narrowing the search.
25+
* @return a `HarnessPredicate` configured with the given options.
26+
*/
27+
static with(options: BottomSheetHarnessFilters = {}): HarnessPredicate<MatBottomSheetHarness> {
28+
return new HarnessPredicate(MatBottomSheetHarness, options);
29+
}
30+
31+
/** Gets the value of the bottom sheet's "aria-label" attribute. */
32+
async getAriaLabel(): Promise<string|null> {
33+
return (await this.host()).getAttribute('aria-label');
34+
}
35+
36+
/**
37+
* Dismisses the bottom sheet by pressing escape. Note that this method cannot
38+
* be used if "disableClose" has been set to true via the config.
39+
*/
40+
async dismiss(): Promise<void> {
41+
await (await this.host()).sendKeys(TestKey.ESCAPE);
42+
}
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './public-api';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './bottom-sheet-harness';
10+
export * from './bottom-sheet-harness-filters';
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {HarnessLoader} from '@angular/cdk/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {Component, TemplateRef, ViewChild} from '@angular/core';
4+
import {ComponentFixture, TestBed, inject} from '@angular/core/testing';
5+
import {
6+
MatBottomSheet,
7+
MatBottomSheetConfig,
8+
MatBottomSheetModule,
9+
} from '@angular/material/bottom-sheet';
10+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
11+
import {OverlayContainer} from '@angular/cdk/overlay';
12+
import {MatBottomSheetHarness} from './bottom-sheet-harness';
13+
14+
/** Shared tests to run on both the original and MDC-based bottom sheets. */
15+
export function runHarnessTests(
16+
bottomSheetModule: typeof MatBottomSheetModule, harness: typeof MatBottomSheetHarness) {
17+
let fixture: ComponentFixture<BottomSheetHarnessTest>;
18+
let loader: HarnessLoader;
19+
let overlayContainer: OverlayContainer;
20+
21+
beforeEach(async () => {
22+
await TestBed.configureTestingModule({
23+
imports: [bottomSheetModule, NoopAnimationsModule],
24+
declarations: [BottomSheetHarnessTest],
25+
}).compileComponents();
26+
27+
fixture = TestBed.createComponent(BottomSheetHarnessTest);
28+
fixture.detectChanges();
29+
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
30+
inject([OverlayContainer], (oc: OverlayContainer) => {
31+
overlayContainer = oc;
32+
})();
33+
});
34+
35+
afterEach(() => {
36+
// Dismiss the bottom sheet once the tests are done. This is necessary because the
37+
// "MatBottomSheet" service is not destroyed automatically by TestBed, and it could
38+
// mean that bottom sheets are left in the DOM.
39+
fixture.componentInstance.bottomSheet.dismiss();
40+
fixture.detectChanges();
41+
42+
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
43+
overlayContainer.ngOnDestroy();
44+
});
45+
46+
it('should load harness for a bottom sheet', async () => {
47+
fixture.componentInstance.open();
48+
const bottomSheets = await loader.getAllHarnesses(harness);
49+
expect(bottomSheets.length).toBe(1);
50+
});
51+
52+
it('should be able to get aria-label of the bottom sheet', async () => {
53+
fixture.componentInstance.open({ariaLabel: 'Confirm purchase.'});
54+
const bottomSheet = await loader.getHarness(harness);
55+
expect(await bottomSheet.getAriaLabel()).toBe('Confirm purchase.');
56+
});
57+
58+
it('should be able to dismiss the bottom sheet', async () => {
59+
fixture.componentInstance.open();
60+
let bottomSheets = await loader.getAllHarnesses(harness);
61+
62+
expect(bottomSheets.length).toBe(1);
63+
await bottomSheets[0].dismiss();
64+
65+
bottomSheets = await loader.getAllHarnesses(harness);
66+
expect(bottomSheets.length).toBe(0);
67+
});
68+
}
69+
70+
@Component({
71+
template: `
72+
<ng-template>
73+
Hello from the bottom sheet!
74+
</ng-template>
75+
`
76+
})
77+
class BottomSheetHarnessTest {
78+
@ViewChild(TemplateRef) template: TemplateRef<any>;
79+
80+
constructor(readonly bottomSheet: MatBottomSheet) {}
81+
82+
open(config?: MatBottomSheetConfig) {
83+
return this.bottomSheet.open(this.template, config);
84+
}
85+
}

src/material/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ entryPoints = [
44
"badge",
55
"badge/testing",
66
"bottom-sheet",
7+
"bottom-sheet/testing",
78
"button",
89
"button/testing",
910
"button-toggle",

test/karma-system-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ System.config({
104104
'@angular/material/badge/testing': 'dist/packages/material/badge/testing/index.js',
105105
'@angular/material/badge/testing/shared.spec': 'dist/packages/material/badge/testing/shared.spec.js',
106106
'@angular/material/bottom-sheet': 'dist/packages/material/bottom-sheet/index.js',
107+
'@angular/material/bottom-sheet/testing': 'dist/packages/material/bottom-sheet/testing/index.js',
108+
'@angular/material/bottom-sheet/testing/shared.spec': 'dist/packages/material/bottom-sheet/testing/shared.spec.js',
107109
'@angular/material/button': 'dist/packages/material/button/index.js',
108110
'@angular/material/button/testing': 'dist/packages/material/button/testing/index.js',
109111
'@angular/material/button/testing/shared.spec': 'dist/packages/material/button/testing/shared.spec.js',

0 commit comments

Comments
 (0)