From 761d524fdb9fc6e3e5cbf6142ecaa80650c689dd Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 23 Jun 2022 14:47:43 -0400 Subject: [PATCH] Add tests for fetchFollowers() action --- .../actions/__tests__/accounts.test.ts | 58 +++++++++++++++++++ app/soapbox/actions/accounts.ts | 19 +++--- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index 6a8d6ddd5..90de6544a 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -10,6 +10,7 @@ import { createAccount, fetchAccount, fetchAccountByUsername, + fetchFollowers, followAccount, muteAccount, removeFromFollowers, @@ -983,6 +984,63 @@ describe('removeFromFollowers()', () => { await store.dispatch(removeFromFollowers(id)); const actions = store.getActions(); + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); + +describe('fetchFollowers()', () => { + const id = '1'; + + describe('when logged in', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}); + store = mockStore(state); + }); + + describe('with a successful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet(`/api/v1/accounts/${id}/followers`).reply(200, [], { + link: `; rel='prev'`, + }); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOWERS_FETCH_REQUEST', id }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'FOLLOWERS_FETCH_SUCCESS', + id, + accounts: [], + next: null, + }, + ]; + await store.dispatch(fetchFollowers(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet(`/api/v1/accounts/${id}/followers`).networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'FOLLOWERS_FETCH_REQUEST', id }, + { type: 'FOLLOWERS_FETCH_FAIL', id, error: new Error('Network Error') }, + ]; + await store.dispatch(fetchFollowers(id)); + const actions = store.getActions(); + expect(actions).toEqual(expectedActions); }); }); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index 0249ac4f5..75a2f41a9 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -498,15 +498,18 @@ const fetchFollowers = (id: string) => (dispatch: AppDispatch, getState: () => RootState) => { dispatch(fetchFollowersRequest(id)); - api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => { - const next = getLinks(response).refs.find(link => link.rel === 'next'); + return api(getState) + .get(`/api/v1/accounts/${id}/followers`) + .then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); - dispatch(importFetchedAccounts(response.data)); - dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null)); - dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); - }).catch(error => { - dispatch(fetchFollowersFail(id, error)); - }); + dispatch(importFetchedAccounts(response.data)); + dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null)); + dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); + }) + .catch(error => { + dispatch(fetchFollowersFail(id, error)); + }); }; const fetchFollowersRequest = (id: string) => ({