diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index c0af695cb..9e5903f3b 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -6,6 +6,7 @@ import rootReducer from 'soapbox/reducers'; import { normalizeAccount } from '../../normalizers'; import { + authorizeFollowRequest, blockAccount, createAccount, expandFollowers, @@ -1572,3 +1573,66 @@ describe('expandFollowRequests()', () => { }); }); }); + +describe('authorizeFollowRequest()', () => { + const id = '1'; + + describe('when logged out', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}).set('me', null); + store = mockStore(state); + }); + + it('should do nothing', async() => { + await store.dispatch(authorizeFollowRequest(id)); + 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(() => { + __stub((mock) => { + mock.onPost(`/api/v1/follow_requests/${id}/authorize`).reply(200); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOW_REQUEST_AUTHORIZE_REQUEST', id }, + { type: 'FOLLOW_REQUEST_AUTHORIZE_SUCCESS', id }, + ]; + await store.dispatch(authorizeFollowRequest(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onPost(`/api/v1/follow_requests/${id}/authorize`).networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOW_REQUEST_AUTHORIZE_REQUEST', id }, + { type: 'FOLLOW_REQUEST_AUTHORIZE_FAIL', id, error: new Error('Network Error') }, + ]; + await store.dispatch(authorizeFollowRequest(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index a1544a27f..e1a4217e5 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -761,11 +761,11 @@ const expandFollowRequestsFail = (error: AxiosError) => ({ const authorizeFollowRequest = (id: string) => (dispatch: AppDispatch, getState: () => RootState) => { - if (!isLoggedIn(getState)) return; + if (!isLoggedIn(getState)) return null; dispatch(authorizeFollowRequestRequest(id)); - api(getState) + return api(getState) .post(`/api/v1/follow_requests/${id}/authorize`) .then(() => dispatch(authorizeFollowRequestSuccess(id))) .catch(error => dispatch(authorizeFollowRequestFail(id, error)));