0

I have a component with the following ngOnInit function which polls a service method for status updates:

ngOnInit() {
this.waitingForMachineToStart = interval(2000)
  .pipe(
    distinctUntilChanged(),
    switchMap(() => this.machineService.getMachineStatus(this.machineId)),
  )
  .subscribe(res => {
    this.machineStatus = res.status;
    if (this.machineStatus === 'STARTED') {
      this.machineStarted = res
      this.notify()
    }
  });
}

I am trying to test that the updates are actually happening using the following code:

beforeEach(() => {
  fixture = TestBed.createComponent(UploadComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
});


it('should start checking for status updates', fakeAsync(() => {
  const machineService = TestBed.get(MachineService);
  spyOn(machineService, 'getMachineStatus').and.returnValue(Observable.create().pipe(map(() => {status :'STARTED'})));
  // Should not be initialised yet
  expect(component.machineStarted).toBeUndefined();
  tick(2000);
  //FAILING TEST
  expect(component.machineStarted).toBe({status :'STARTED'});
}));

However, the test still fails.

Thanks in advance

testeurFou
  • 71
  • 3
  • 11
  • What is the error which is shown on the console? – suraj13k Feb 06 '20 at 20:13
  • Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. Expected '' to be 'STARTED'. **I have added the following in my beforeEach : jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; – testeurFou Feb 10 '20 at 09:25

0 Answers0