diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index 0a0a909e0..4e26a677f 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -14,6 +14,7 @@ import { fetchAccountByUsername, fetchFollowers, fetchFollowing, + fetchFollowRequests, fetchRelationships, followAccount, muteAccount, @@ -1402,3 +1403,77 @@ describe('fetchRelationships()', () => { }); }); }); + +describe('fetchFollowRequests()', () => { + describe('when logged out', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}).set('me', null); + store = mockStore(state); + }); + + it('should do nothing', async() => { + await store.dispatch(fetchFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual([]); + }); + }); + + describe('when logged in', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('me', '123'); + store = mockStore(state); + }); + + describe('with a successful API request', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('relationships', ImmutableMap({})) + .set('me', '123'); + store = mockStore(state); + + __stub((mock) => { + mock.onGet('/api/v1/follow_requests').reply(200, [], { + link: '; rel=\'prev\'', + }); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOW_REQUESTS_FETCH_REQUEST' }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'FOLLOW_REQUESTS_FETCH_SUCCESS', + accounts: [], + next: null, + }, + ]; + await store.dispatch(fetchFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet('/api/v1/follow_requests').networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOW_REQUESTS_FETCH_REQUEST' }, + { type: 'FOLLOW_REQUESTS_FETCH_FAIL', error: new Error('Network Error') }, + ]; + await store.dispatch(fetchFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index 74e95c5da..ee6790171 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -693,15 +693,18 @@ const fetchRelationshipsFail = (error: AxiosError) => ({ const fetchFollowRequests = () => (dispatch: AppDispatch, getState: () => RootState) => { - if (!isLoggedIn(getState)) return; + if (!isLoggedIn(getState)) return null; dispatch(fetchFollowRequestsRequest()); - api(getState).get('/api/v1/follow_requests').then(response => { - const next = getLinks(response).refs.find(link => link.rel === 'next'); - dispatch(importFetchedAccounts(response.data)); - dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null)); - }).catch(error => dispatch(fetchFollowRequestsFail(error))); + return api(getState) + .get('/api/v1/follow_requests') + .then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); + dispatch(importFetchedAccounts(response.data)); + dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null)); + }) + .catch(error => dispatch(fetchFollowRequestsFail(error))); }; const fetchFollowRequestsRequest = () => ({