diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index 90de6544a..b1db9d916 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -8,6 +8,7 @@ import { normalizeAccount } from '../../normalizers'; import { blockAccount, createAccount, + expandFollowers, fetchAccount, fetchAccountByUsername, fetchFollowers, @@ -1045,4 +1046,91 @@ describe('fetchFollowers()', () => { }); }); }); -}); \ No newline at end of file +}); + +describe('expandFollowers()', () => { + const id = '1'; + + describe('when logged in', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('user_lists', ImmutableMap({ + followers: ImmutableMap({ + [id]: 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({ + followers: ImmutableMap({ + [id]: ImmutableMap({ + next: null, + }), + }), + })) + .set('me', '123'); + store = mockStore(state); + }); + + it('should do nothing', async() => { + await store.dispatch(expandFollowers(id)); + 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: 'FOLLOWERS_EXPAND_REQUEST', id }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'FOLLOWERS_EXPAND_SUCCESS', + id, + accounts: [], + next: null, + }, + ]; + await store.dispatch(expandFollowers(id)); + 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: 'FOLLOWERS_EXPAND_REQUEST', id }, + { type: 'FOLLOWERS_EXPAND_FAIL', id, error: new Error('Network Error') }, + ]; + await store.dispatch(expandFollowers(id)); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index 75a2f41a9..f1e3b277c 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -532,25 +532,28 @@ const fetchFollowersFail = (id: string, error: AxiosError) => ({ const expandFollowers = (id: string) => (dispatch: AppDispatch, getState: () => RootState) => { - if (!isLoggedIn(getState)) return; + if (!isLoggedIn(getState)) return null; const url = getState().user_lists.getIn(['followers', id, 'next']); if (url === null) { - return; + return null; } dispatch(expandFollowersRequest(id)); - api(getState).get(url).then(response => { - const next = getLinks(response).refs.find(link => link.rel === 'next'); + return api(getState) + .get(url) + .then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); - dispatch(importFetchedAccounts(response.data)); - dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null)); - dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); - }).catch(error => { - dispatch(expandFollowersFail(id, error)); - }); + dispatch(importFetchedAccounts(response.data)); + dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null)); + dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); + }) + .catch(error => { + dispatch(expandFollowersFail(id, error)); + }); }; const expandFollowersRequest = (id: string) => ({