I have angular 4 application and i am writing test cases using jasmin + Karma
My first test case working properly as it creates component and service instance,
But second test cases is failing as it is not able to fetch mock data for 'getorders' method, when i log its value is undefined.
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ OrderListComponent ],
providers: [
OrderFormFetchService,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
RouterModule.forRoot([]),
NglModule.forRoot({svgPath: 'assets/icons'}),
LayoutModule,
NgbModule.forRoot()
],
})
.compileComponents();
}));
beforeEach(() => {
orderFormFetchService = TestBed.get(OrderFormFetchService);
fixture = TestBed.createComponent(OrderListComponent);
component = fixture.componentInstance;
orderListStub = [{
'id': '5c18e74dd5ab9332cc473c94',
'createdAt': '2018-12-18T12:25:49.507Z',
'updatedAt': '2018-12-18T12:25:49.507Z',
'name': 'apple',
'website': 'www.apple.com',
'secsId': ''
}];
spyOn(orderFormFetchService, 'getorders').and.returnValue(Observable.of(orderListStub));
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeDefined();
expect(component).toBeTruthy();
expect(fixture).toBeDefined();
expect(orderFormFetchService).toBeDefined();
});
it('check order list ', () => {
console.log(orderFormFetchService.getorders()); // -> undefined
expect(component.orderList).toBeDefined();
// expect(component.orderList).toEqual(orderListStub);
});
Code in OrderListComponent is as follows,
export class OrderListComponent extends BaseComponent implements OnInit {
defaultQuery = {website: null, name: null};
orderList: any;
query = _.clone(this.defaultQuery);
date: Date;
constructor(private orderFormFetchService: OrderFormFetchService) { super(); }
ngOnInit() {
this.date = new Date();
this.getOrderList();
}
getOrderList(): void {
this.orderFormFetchService.getOrders()
.subscribe((res: Response) => {
if (_.isArray(res)) {
this.orderList = _.orderBy(res, [order => order.name.toLowerCase()], ['asc']);
} else {
this.orderList = [res];
}
});
}
}
Please help