diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index b1db9d9164..1b63739a8e 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -12,6 +12,7 @@ import { fetchAccount, fetchAccountByUsername, fetchFollowers, + fetchFollowing, followAccount, muteAccount, removeFromFollowers, @@ -996,7 +997,7 @@ describe('fetchFollowers()', () => { describe('when logged in', () => { beforeEach(() => { - const state = rootReducer(undefined, {}); + const state = rootReducer(undefined, {}).set('me', '123'); store = mockStore(state); }); @@ -1134,3 +1135,60 @@ describe('expandFollowers()', () => { }); }); }); + +describe('fetchFollowing()', () => { + const id = '1'; + + 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.onGet(`/api/v1/accounts/${id}/following`).reply(200, [], { + link: `; rel='prev'`, + }); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOWING_FETCH_REQUEST', id }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'FOLLOWING_FETCH_SUCCESS', + id, + accounts: [], + next: null, + }, + ]; + await store.dispatch(fetchFollowing(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet(`/api/v1/accounts/${id}/following`).networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOWING_FETCH_REQUEST', id }, + { type: 'FOLLOWING_FETCH_FAIL', id, error: new Error('Network Error') }, + ]; + await store.dispatch(fetchFollowing(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index f1e3b277cc..becf822d6c 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -578,15 +578,18 @@ const fetchFollowing = (id: string) => (dispatch: AppDispatch, getState: () => RootState) => { dispatch(fetchFollowingRequest(id)); - api(getState).get(`/api/v1/accounts/${id}/following`).then(response => { - const next = getLinks(response).refs.find(link => link.rel === 'next'); + return api(getState) + .get(`/api/v1/accounts/${id}/following`) + .then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); - dispatch(importFetchedAccounts(response.data)); - dispatch(fetchFollowingSuccess(id, response.data, next ? next.uri : null)); - dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); - }).catch(error => { - dispatch(fetchFollowingFail(id, error)); - }); + dispatch(importFetchedAccounts(response.data)); + dispatch(fetchFollowingSuccess(id, response.data, next ? next.uri : null)); + dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); + }) + .catch(error => { + dispatch(fetchFollowingFail(id, error)); + }); }; const fetchFollowingRequest = (id: string) => ({