I have a project on Angular 5 and I encountered with following issue. I have a component ComponentWithSnackBar which triggers displaying of a snack-bar:
showSnackBar() {
this.snackBar.open('Message text', 'Close', {
duration: 5000,
verticalPosition: 'top',
});
}
It works as expected. But I have no ideas how I can test it. I try to write test:
describe('ComponentWithSnackBar', () => {
let snackBar: MatSnackBar;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
function createComponent<T>(component: Type<T>, providers: Provider[] = [], declarations: any[] = []): ComponentFixture<T> {
TestBed.configureTestingModule({
imports: [AppModule, RouterTestingModule, NoopAnimationsModule],
declarations: declarations,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers,
}).compileComponents();
inject([MatSnackBar, OverlayContainer], (sb: MatSnackBar, oc: OverlayContainer) => {
snackBar = sb;
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
})();
return TestBed.createComponent<T>(component);
}
it(`Should display snack-bar`, fakeAsync(() => {
const fixture = createComponent(ComponentWithSnackBar);
const component: ComponentWithSnackBar = fixture.debugElement.componentInstance;
fixture.detectChanges();
const button = fixture.debugElement.query(By.css('button')); //button which triggers method showSnackBar
button.triggerEventHandler('click', null);
fixture.detectChanges();
flush();
const messageElement = overlayContainerElement.querySelector('snack-bar-container');
expect(messageElement.textContent).toContain('Message text');
}));
});
As a result I get error
TypeError: Cannot read property 'textContent' of null
What I do wrong? Thanks for advance!