|
| 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 | + let overlayContainer: OverlayContainer; |
| 16 | + |
| 17 | + beforeAll(() => { |
| 18 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 19 | + }); |
| 20 | + |
| 21 | + beforeEach( |
| 22 | + waitForAsync(() => { |
| 23 | + TestBed.configureTestingModule({ |
| 24 | + imports: [MatBottomSheetModule, NoopAnimationsModule], |
| 25 | + declarations: [BottomSheetHarnessExample] |
| 26 | + }).compileComponents(); |
| 27 | + })); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + fixture = TestBed.createComponent(BottomSheetHarnessExample); |
| 31 | + fixture.detectChanges(); |
| 32 | + loader = TestbedHarnessEnvironment.documentRootLoader(fixture); |
| 33 | + inject([OverlayContainer], (oc: OverlayContainer) => { |
| 34 | + overlayContainer = oc; |
| 35 | + })(); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + // Dismiss the bottom sheet once the tests are done. This is necessary because the |
| 40 | + // "MatBottomSheet" service is not destroyed automatically by TestBed, and it could |
| 41 | + // mean that bottom sheets are left in the DOM. |
| 42 | + fixture.componentInstance.bottomSheet.dismiss(); |
| 43 | + fixture.detectChanges(); |
| 44 | + // Angular won't call this for us so we need to do it ourselves to avoid leaks. |
| 45 | + overlayContainer.ngOnDestroy(); |
| 46 | + overlayContainer = null!; |
| 47 | + }); |
| 48 | + |
| 49 | + it('should load harness for a bottom sheet', async () => { |
| 50 | + fixture.componentInstance.open(); |
| 51 | + const bottomSheets = await loader.getAllHarnesses(MatBottomSheetHarness); |
| 52 | + expect(bottomSheets.length).toBe(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should be able to get aria-label of the bottom sheet', async () => { |
| 56 | + fixture.componentInstance.open({ariaLabel: 'Confirm purchase.'}); |
| 57 | + const bottomSheet = await loader.getHarness(MatBottomSheetHarness); |
| 58 | + expect(await bottomSheet.getAriaLabel()).toBe('Confirm purchase.'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should be able to dismiss the bottom sheet', async () => { |
| 62 | + fixture.componentInstance.open(); |
| 63 | + let bottomSheets = await loader.getAllHarnesses(MatBottomSheetHarness); |
| 64 | + |
| 65 | + expect(bottomSheets.length).toBe(1); |
| 66 | + await bottomSheets[0].dismiss(); |
| 67 | + |
| 68 | + bottomSheets = await loader.getAllHarnesses(MatBottomSheetHarness); |
| 69 | + expect(bottomSheets.length).toBe(0); |
| 70 | + }); |
| 71 | +}); |
0 commit comments