From 7fc43f524a12f77bea79297286ba5db427488aa8 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 23 Jun 2022 15:31:32 -0400 Subject: [PATCH] Add tests for expandFollowRequests() action --- .../actions/__tests__/accounts.test.ts | 95 +++++++++++++++++++ app/soapbox/actions/accounts.ts | 17 ++-- 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index 4e26a677f..c0af695cb 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -10,6 +10,7 @@ import { createAccount, expandFollowers, expandFollowing, + expandFollowRequests, fetchAccount, fetchAccountByUsername, fetchFollowers, @@ -1477,3 +1478,97 @@ describe('fetchFollowRequests()', () => { }); }); }); + +describe('expandFollowRequests()', () => { + describe('when logged out', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}).set('me', null); + store = mockStore(state); + }); + + it('should do nothing', async() => { + await store.dispatch(expandFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual([]); + }); + }); + + describe('when logged in', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('user_lists', ImmutableMap({ + follow_requests: ImmutableMap({ + next: 'next_url', + }), + })) + .set('me', '123'); + store = mockStore(state); + }); + + describe('when the url is null', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('user_lists', ImmutableMap({ + follow_requests: ImmutableMap({ + next: null, + }), + })) + .set('me', '123'); + store = mockStore(state); + }); + + it('should do nothing', async() => { + await store.dispatch(expandFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual([]); + }); + }); + + describe('with a successful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet('next_url').reply(200, [], { + link: '; rel=\'prev\'', + }); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOW_REQUESTS_EXPAND_REQUEST' }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'FOLLOW_REQUESTS_EXPAND_SUCCESS', + accounts: [], + next: null, + }, + ]; + await store.dispatch(expandFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet('next_url').networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOW_REQUESTS_EXPAND_REQUEST' }, + { type: 'FOLLOW_REQUESTS_EXPAND_FAIL', error: new Error('Network Error') }, + ]; + await store.dispatch(expandFollowRequests()); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index ee6790171..a1544a27f 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -724,21 +724,24 @@ const fetchFollowRequestsFail = (error: AxiosError) => ({ const expandFollowRequests = () => (dispatch: AppDispatch, getState: () => RootState) => { - if (!isLoggedIn(getState)) return; + if (!isLoggedIn(getState)) return null; const url = getState().user_lists.getIn(['follow_requests', 'next']); if (url === null) { - return; + return null; } dispatch(expandFollowRequestsRequest()); - api(getState).get(url).then(response => { - const next = getLinks(response).refs.find(link => link.rel === 'next'); - dispatch(importFetchedAccounts(response.data)); - dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null)); - }).catch(error => dispatch(expandFollowRequestsFail(error))); + return api(getState) + .get(url) + .then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); + dispatch(importFetchedAccounts(response.data)); + dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null)); + }) + .catch(error => dispatch(expandFollowRequestsFail(error))); }; const expandFollowRequestsRequest = () => ({