|
| 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 | +} |
0 commit comments