samedi 21 janvier 2017

Angular2 whenStable() not working with observable?

I'm running an http call (using obervable) in the ngOnInit method from one Angular2 component:

ngOnInit() {
     this.descriptorService.generateDescriptors(new Server());
     this.fetchData();
}

fetchData () {
  console.log('Starting Http call');
  this.dataService.listAll(Server).subscribe(
     servers=>{console.log('Http Call done');this.listServers=servers},
     error=>console.log(error)
  );
}

with the following dataService.listAll():

listAll(zeClass:typeof Model): Observable<T[]> {
  console.log('Http call begin');
  return this.http.get(this.calculateUrl(zeClass))
    .map(this.extractData)
    .map((jsonArray)=>{
       let data:Array<ModelInterface>=new Array<ModelInterface>();
       if (isArray (jsonArray)) {
             console.log('Http call in');
         ...
       return data;
     })
    .catch(this.handleError);
 }

When I run the following unit test for this component (http is mockedup with InMemoryWebApiModule) :

it('should display the list of servers', async(() => {
  fixture.detectChanges();
  console.log('Before whenStable():'+fixture.isStable());
  fixture.whenStable().then(()=>{
    console.log('In whenStable()');
    fixture.detectChanges();
    let de:Array<DebugElement> = fixture.debugElement.queryAll(By.css('md-list-item'));

    expect(de.length).toBeGreaterThan(1);
  });
console.log('After whenStable():'+fixture.isStable());
}));

I then get the following log in the console:

 LOG: 'Starting Http call'
 LOG: 'Http call begin'
 LOG: 'Before whenStable():false'
 LOG: 'After whenStable():false'

You can see the test code inside whenStable() is not run at all... Why ?

Aucun commentaire:

Enregistrer un commentaire