How to write a unit- test for this data attribute given value when a promise is resolved?

198 Views Asked by At

I have the following angular-component:

export class AppComponent implements OnInit {
    data: Currency[];
    ngOnInit() {
   d3.csv('../../assets/data.csv').then((data: any) => {
     this.data = data.map(item => new Currency(item.date, item.price));
   });
  }

}

I would like to write a unit-test for this component, and this is what I have so far:

it('should ', async(() => {
      const fixture = TestBed.createComponent(AppComponent);
      fixture.detectChanges();
      fixture.whenStable().then(() => {
          const app = fixture.debugElement.componentInstance;
          expect(app.data.length).toBe(10);
      });
    }));

I believe that the fixture.whenStable would wait until all promises are resolved (such as the one within ngOnInit). Could someone guide me in writing a correct unit test that waits until the promise is resolved and this.data has a value?
this seems to be the answer so far: Testing promise in Angular 2 ngOnInit

0

There are 0 best solutions below