I have created a polling service which recursively calls an api and on success of the api if certain conditions are met, keeps polling again.
/**
* start a timer with the interval specified by the user || default interval
* we are using setTimeout and not setinterval because a slow back end server might take more time than our interval time and that would lead to
* a queue of ajax requests with no response at all.
* -----------------------------------------
* This function would call the api first time and only on the success response of the api we would poll again after the interval
*/
runPolling() {
const { url, onSuccess, onFailure, interval } = this.config;
const _this = this;
this.poll = setTimeout(() => {
/* onSuccess would be handled by the user of service which would either return true or false
* true - This means we need to continue polling
* false - This means we need to stop polling
*/
api
.request(url)
.then(response => {
console.log('I was called', response);
onSuccess(response);
})
.then(continuePolling => {
_this.isPolling && continuePolling ? _this.runPolling() : _this.stopPolling();
})
.catch(error => {
if (_this.config.shouldRetry && _this.config.retryCount > 0) {
onFailure && onFailure(error);
_this.config.retryCount--;
_this.runPolling();
} else {
onFailure && onFailure(error);
_this.stopPolling();
}
});
}, interval);
}
While trying to write the test cases for it, I am not very sure as to how can simulate fake timers and the axios api response.
This is what I have so far
import PollingService from '../PollingService';
import { statusAwaitingProduct } from '@src/__mock_data__/getSessionStatus';
import mockAxios from 'axios';
describe('timer events for runPoll', () => {
let PollingObject,
pollingInterval = 3000,
url = '/session/status',
onSuccess = jest.fn(() => {
return false;
});
beforeAll(() => {
PollingObject = new PollingService({
url: url,
interval: pollingInterval,
onSuccess: onSuccess
});
});
beforeEach(() => {
jest.useFakeTimers();
});
test('runPolling should be called recursively when onSuccess returns true', async () => {
expect.assertions(1);
const mockedRunPolling = jest.spyOn(PollingObject, 'runPolling');
const mockedOnSuccess = jest.spyOn(PollingObject.config, 'onSuccess');
mockAxios.request.mockImplementation(
() =>
new Promise(resolve => {
resolve(statusAwaitingProduct);
})
);
PollingObject.startPolling();
expect(mockedRunPolling).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(mockAxios.request).toHaveBeenCalledTimes(0);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), pollingInterval);
jest.runAllTimers();
expect(mockAxios.request).toHaveBeenCalledTimes(1);
expect(mockedOnSuccess).toHaveBeenCalledTimes(1);
expect(PollingObject.isPolling).toBeTruthy();
expect(mockedRunPolling).toHaveBeenCalledTimes(2);
});
});
});
Here even though mockedOnsuccess is called but jest expect call fails saying it was called 0 times instead being called 1times.
Can someone please help? Thanks