Unit test case polling service in angular

 ngOnInit(): void {
    
    this.timeInterval = interval(30000).pipe(startWith(0), switchMap(() => this.deviceService.getDeviceSession())
    ).subscribe((success: any) => {
      this.rowData = success;
      console.log(this.rowData)
      
    }, (error: any) => {
      this.rowData = [];
      if (error.error && error.error.status !== 401) {
        this.toastService.error('Error in loading data');
      }
    }
    
Test Case    

it('should start checking for data after every interval', (done) => {
const dataService = TestBed.get(DeviceService);
// Mock the getStatus function
spyOn(dataService, 'getDeviceSession').and.returnValue(Observable.create().pipe(map(() => 'woo')));
// Should not be initialised yet
expect(component.rowData).toBeUndefined();

setTimeout(()=> {
      expect(component.rowData).toBe('woo');
      done();
},30000);

});

I am writing this unit test case to check this polling implementation, which is fetching data(array of objects) from service and updating it in the Dom. But the test case is getting failed. I am not sure where i am going wrong.
Getting error rowData.forEach is not a function. Please help how to solve it

100 thoughts on “Unit test case polling service in angular”

Leave a Comment