import { inject, ComponentFixture, TestBed, fakeAsync, tick, flush, } from '@angular/core/testing'; import { NgModule, Component, Directive, ViewChild, ViewContainerRef, Inject, TemplateRef, } from '@angular/core'; import {CommonModule} from '@angular/common'; import {NoopAnimationsModule} from '@angular/platform-browser/animations'; import {OverlayContainer} from '@angular/cdk/overlay'; import {LiveAnnouncer} from '@angular/cdk/a11y'; import { MatSnackBarModule, MatSnackBar, MatSnackBarConfig, MatSnackBarRef, SimpleSnackBar, MAT_SNACK_BAR_DATA, MAT_SNACK_BAR_DEFAULT_OPTIONS, } from './index'; describe('MatSnackBar', () => { let snackBar: MatSnackBar; let liveAnnouncer: LiveAnnouncer; let overlayContainer: OverlayContainer; let overlayContainerElement: HTMLElement; let testViewContainerRef: ViewContainerRef; let viewContainerFixture: ComponentFixture; let simpleMessage = 'Burritos are here!'; let simpleActionLabel = 'pickup'; beforeEach(fakeAsync(() => { TestBed.configureTestingModule({ imports: [MatSnackBarModule, SnackBarTestModule, NoopAnimationsModule], }).compileComponents(); })); beforeEach(inject([MatSnackBar, LiveAnnouncer, OverlayContainer], (sb: MatSnackBar, la: LiveAnnouncer, oc: OverlayContainer) => { snackBar = sb; liveAnnouncer = la; overlayContainer = oc; overlayContainerElement = oc.getContainerElement(); })); afterEach(() => { overlayContainer.ngOnDestroy(); liveAnnouncer.ngOnDestroy(); }); beforeEach(() => { viewContainerFixture = TestBed.createComponent(ComponentWithChildViewContainer); viewContainerFixture.detectChanges(); testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer; }); it('should have the role of `alert` with an `assertive` politeness if no announcement message ' + 'is provided', () => { snackBar.openFromComponent(BurritosNotification, {announcementMessage: '', politeness: 'assertive'}); viewContainerFixture.detectChanges(); const containerElement = overlayContainerElement.querySelector('snack-bar-container')!; expect(containerElement.getAttribute('role')) .toBe('alert', 'Expected snack bar container to have role="alert"'); }); it('should have the role of `status` with an `assertive` politeness if an announcement message ' + 'is provided', () => { snackBar.openFromComponent(BurritosNotification, {announcementMessage: 'Yay Burritos', politeness: 'assertive'}); viewContainerFixture.detectChanges(); const containerElement = overlayContainerElement.querySelector('snack-bar-container')!; expect(containerElement.getAttribute('role')) .toBe('status', 'Expected snack bar container to have role="status"'); }); it('should have the role of `status` with a `polite` politeness', () => { snackBar.openFromComponent(BurritosNotification, {politeness: 'polite'}); viewContainerFixture.detectChanges(); const containerElement = overlayContainerElement.querySelector('snack-bar-container')!; expect(containerElement.getAttribute('role')) .toBe('status', 'Expected snack bar container to have role="status"'); }); it('should remove the role if the politeness is turned off', () => { snackBar.openFromComponent(BurritosNotification, {politeness: 'off'}); viewContainerFixture.detectChanges(); const containerElement = overlayContainerElement.querySelector('snack-bar-container')!; expect(containerElement.getAttribute('role')).toBeFalsy('Expected role to be removed'); }); it('should open and close a snackbar without a ViewContainerRef', fakeAsync(() => { let snackBarRef = snackBar.open('Snack time!', 'Chew'); viewContainerFixture.detectChanges(); let messageElement = overlayContainerElement.querySelector('snack-bar-container')!; expect(messageElement.textContent).toContain('Snack time!', 'Expected snack bar to show a message without a ViewContainerRef'); snackBarRef.dismiss(); viewContainerFixture.detectChanges(); flush(); expect(overlayContainerElement.childNodes.length) .toBe(0, 'Expected snack bar to be dismissed without a ViewContainerRef'); })); it('should open a simple message with a button', () => { let config: MatSnackBarConfig = {viewContainerRef: testViewContainerRef}; let snackBarRef = snackBar.open(simpleMessage, simpleActionLabel, config); viewContainerFixture.detectChanges(); expect(snackBarRef.instance instanceof SimpleSnackBar) .toBe(true, 'Expected the snack bar content component to be SimpleSnackBar'); expect(snackBarRef.instance.snackBarRef) .toBe(snackBarRef, 'Expected the snack bar reference to be placed in the component instance'); let messageElement = overlayContainerElement.querySelector('snack-bar-container')!; expect(messageElement.textContent) .toContain(simpleMessage, `Expected the snack bar message to be '${simpleMessage}'`); let buttonElement = overlayContainerElement.querySelector('button.mat-button')!; expect(buttonElement.tagName) .toBe('BUTTON', 'Expected snack bar action label to be a