I am little new to writing unit tests for Angular and not able to figure out how I can write unit tests for a component with a @Input directive.
Here is my
== file.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-bugs-table',
templateUrl: './bugs-table.component.html',
styleUrls: ['./bugs-table.component.scss']
})
export class BugsTableComponent implements OnInit {
// priorityData: any = [];
@Input() bugsDataService;
@Input() gridItemComponent;
constructor(
) { }
ngOnInit() {
}
ngAfterViewInit() {
this.bugsDataService.subscribe(response => {
this.bugsData = response;
})
} }
Here is my file.component.spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule, } from '@angular/forms';
import { HttpModule, BrowserXhr } from "@angular/http";
fdescribe('BugsTableComponent', () => {
let component: BugsTableComponent;
let fixture: ComponentFixture<BugsTableComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BugsTableComponent ],
imports: []
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BugsTableComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create', () => {
expect(component).toBeTruthy();
});
});
But it fails with error:
× should create HeadlessChrome 69.0.3497 (Windows 10.0.0) TypeError: Cannot read property 'subscribe' of undefined
I know this is because bugsDataService (which is a observable) is undefined. Can any please help fixing this. Please let me know if you need more info.
component.bugsDataService?