I have an angular component which uses an injection token to get data from an Angular-material dialog.
export class DialogComponent implements OnInit
{
public userEmail: string;
constructor(private dialogService: DialogService, @Inject(MAT_DIALOG_DATA) data)
{
this.userEmail = data.data.userEmail;
}
...
}
I am trying to run unit tests for this component but have been running into various different errors. I have already looked at these posts...
Angular Material and Jasmine : " No provider for InjectionToken MdDialogData! "
Angular Testing - Mocking An InjectionToken
Angular components injection issues
...but none have provided a working solution I am looking for to solve the error I'm seeing for my most basic unit test. I was experiencing the same error message as seen in the first post but the solution within fixed that error while providing me with my current error. The test, and the associated providers and beforeEach block, is...
providers: [
DialogService,
{ provide: MAT_DIALOG_DATA, useValue: {} },
{ provide: MatDialogRef, useValue: {} }
]
...
beforeEach(() =>
{
fixture = TestBed.createComponent(DialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
...
it('should create', () =>
{
expect(component).toBeTruthy();
});
...And then, the error I see is...
TypeError: Cannot read property 'userEmail' of undefined
There's something I'm not understanding here, and if anyone knows what, I'd appreciate it if you could explain to me what's going on.
EDIT: I forgot to add in the original post that upon debugging the testing code, the "component" variable is undefined as well.
Angular: v7.3.8 Angular-Material: v6.4.7