function polling(tries) { return async (dispatch, getState) => { const result = await Promise.all([ loadIntegrations(), // calling fetch() getAllEnabledCalendarEvents(), // calling fetch() ]); if (Array.isArray(result)) { if ( result[0].every((calendar) => calendar.name && calendar.id) && areDifferentBy(getState().time.integratedCalendars, result[0]) ) { dispatch(setPolling(false)); dispatch(setPollingInterval(null)); dispatch( getIntegratedCalendarEvents( (result[1] || []).map(formatIntegrationEvent) ) ); dispatch(getIntegratedCalendars(result[0])); } } if (tries >= 100) { // There seems to be no way in Jest to mock the 100 tries // and see these `dispatch()` calls in the `store.getActions();` // return value. dispatch(setPolling(false)); dispatch(setPollingInterval(null)); const msgKey = 'wfhomecalendar.outlook.synctimeout'; const msgs = await Stores.WF.getMessageSet([msgKey]); addErrorToast(msgs['msgKey']); } }; } export function pollForNewIntegration() { return (dispatch) => { dispatch(setPolling(true)); let tries = 0; dispatch( setPollingInterval( setInterval( async () => await dispatch(polling(tries++)), SECOND_IN_MS * 3 ) ) ); }; }