From 86d6c519f02b10d2e57d210b5d2fbc90b75e8e65 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 23 Jun 2022 15:37:21 -0400 Subject: [PATCH] Add tests for authorizeFollowRequest() action --- .../actions/__tests__/accounts.test.ts | 64 +++++++++++++++++++ app/soapbox/actions/accounts.ts | 4 +- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index c0af695cbc..9e5903f3bf 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 a1544a27f9..e1a4217e56 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)));