Problem
I'm using redux-saga, jest, redux-mock-store and react-testing-library, I try to test a saga that makes polling to the server until the status of the response is resolved. In the saga i'm using the redux-saga delay function to create an interval of the polling.
const INTEVAL_TIME = 3000;
function* saga(action) {
try {
yield delay(INTEVAL_TIME);
while (true) {
const { data } = yield call(apiCall);
if (data.status === 'success') {
yield put(success());
break;
}
if (data.status === 'failure') {
yield put(failure());
break;
}
yield delay(INTEVAL_TIME );
}
} catch (error) {
yield put(failure());
}
}
how can i in the test it self mock the delay to not wait 3 sec (maybe setting the delay to 0 or resolve immediately). for example, this is my test
it('some test', async () => {
const { getByTestId } = render(<MyComponent />);
userEvent.click(getByTestId('button');
await waitFor(() => {
expect(store.getActions()).toEqual(succesPayloadMock);
});
});