From 931f2e16d84d8250d144dc6d4ff5c68fdd46b686 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 23 Jun 2022 14:37:55 -0400 Subject: [PATCH] Add tests for removeFromFollowers() action --- .../actions/__tests__/accounts.test.ts | 67 +++++++++++++++++++ app/soapbox/actions/accounts.ts | 13 ++-- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index 2ea60bd80..6a8d6ddd5 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -12,6 +12,7 @@ import { fetchAccountByUsername, followAccount, muteAccount, + removeFromFollowers, subscribeAccount, unblockAccount, unfollowAccount, @@ -921,3 +922,69 @@ describe('unsubscribeAccount()', () => { }); }); }); + +describe('removeFromFollowers()', () => { + 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(removeFromFollowers(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/accounts/${id}/remove_from_followers`).reply(200, {}); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST', id }, + { + type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS', + relationship: {}, + }, + ]; + await store.dispatch(removeFromFollowers(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onPost(`/api/v1/accounts/${id}/remove_from_followers`).networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST', id }, + { type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_FAIL', id, error: new Error('Network Error') }, + ]; + await store.dispatch(removeFromFollowers(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); \ No newline at end of file diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index e5a92fae9..0249ac4f5 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -468,15 +468,14 @@ const unsubscribeAccountFail = (error: AxiosError) => ({ const removeFromFollowers = (id: string) => (dispatch: AppDispatch, getState: () => RootState) => { - if (!isLoggedIn(getState)) return; + if (!isLoggedIn(getState)) return null; - dispatch(muteAccountRequest(id)); + dispatch(removeFromFollowersRequest(id)); - api(getState).post(`/api/v1/accounts/${id}/remove_from_followers`).then(response => { - dispatch(removeFromFollowersSuccess(response.data)); - }).catch(error => { - dispatch(removeFromFollowersFail(id, error)); - }); + return api(getState) + .post(`/api/v1/accounts/${id}/remove_from_followers`) + .then(response => dispatch(removeFromFollowersSuccess(response.data))) + .catch(error => dispatch(removeFromFollowersFail(id, error))); }; const removeFromFollowersRequest = (id: string) => ({