Angular 2. My component contains the following piece of code:
export class SectorComponent implements OnInit {
let myList = [];
let loading = false;
refresh(){
this.loading = true;
const that : any = this;
this.dataService.products().subscribe(products=>{
that.myList= products;
console.log(that.myList);
that.loading = false;
});
}
}
the refresh() method is called from a button. Now I want to unit test it. the dataService is mocked to return a set of products. In my test, I want to see the corre
it("should get products", () => {
component.refresh();
console.log('total found', component.myList.length)
expect(component.myList.length).toEqual(1);
});
the test fails because myList is always 1 while console.log(that.myList) returns 1. I am almost sure it is related to the expect() happening before observable is complete. How do I go handle it please?