From ba595259c166b259110bff3bf5a8daddacd7d355 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 23 Jun 2022 15:17:36 -0400 Subject: [PATCH] Add tests for fetchRelationships() action --- .../actions/__tests__/accounts.test.ts | 94 +++++++++++++++++++ app/soapbox/actions/accounts.ts | 13 ++- 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index ad7b55851..0a0a909e0 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -14,6 +14,7 @@ import { fetchAccountByUsername, fetchFollowers, fetchFollowing, + fetchRelationships, followAccount, muteAccount, removeFromFollowers, @@ -1308,3 +1309,96 @@ describe('expandFollowing()', () => { }); }); }); + +describe('fetchRelationships()', () => { + 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(fetchRelationships([id])); + const actions = store.getActions(); + + expect(actions).toEqual([]); + }); + }); + + describe('when logged in', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('me', '123'); + store = mockStore(state); + }); + + describe('without newAccountIds', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('relationships', ImmutableMap({ [id]: {} })) + .set('me', '123'); + store = mockStore(state); + }); + + it('should do nothing', async() => { + await store.dispatch(fetchRelationships([id])); + const actions = store.getActions(); + + expect(actions).toEqual([]); + }); + }); + + describe('with a successful API request', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('relationships', ImmutableMap({})) + .set('me', '123'); + store = mockStore(state); + + __stub((mock) => { + mock + .onGet(`/api/v1/accounts/relationships?${[id].map(id => `id[]=${id}`).join('&')}`) + .reply(200, []); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'RELATIONSHIPS_FETCH_REQUEST', ids: [id], skipLoading: true }, + { + type: 'RELATIONSHIPS_FETCH_SUCCESS', + relationships: [], + skipLoading: true, + }, + ]; + await store.dispatch(fetchRelationships([id])); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with an unsuccessful API request', () => { + beforeEach(() => { + __stub((mock) => { + mock + .onGet(`/api/v1/accounts/relationships?${[id].map(id => `id[]=${id}`).join('&')}`) + .networkError(); + }); + }); + + it('should dispatch the correct actions', async() => { + const expectedActions = [ + { type: 'RELATIONSHIPS_FETCH_REQUEST', ids: [id], skipLoading: true }, + { type: 'RELATIONSHIPS_FETCH_FAIL', skipLoading: true, error: new Error('Network Error') }, + ]; + await store.dispatch(fetchRelationships([id])); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.ts b/app/soapbox/actions/accounts.ts index ca6f3b1d5..74e95c5da 100644 --- a/app/soapbox/actions/accounts.ts +++ b/app/soapbox/actions/accounts.ts @@ -656,22 +656,21 @@ const expandFollowingFail = (id: string, error: AxiosError) => ({ const fetchRelationships = (accountIds: string[]) => (dispatch: AppDispatch, getState: () => RootState) => { - if (!isLoggedIn(getState)) return; + if (!isLoggedIn(getState)) return null; const loadedRelationships = getState().relationships; const newAccountIds = accountIds.filter(id => loadedRelationships.get(id, null) === null); if (newAccountIds.length === 0) { - return; + return null; } dispatch(fetchRelationshipsRequest(newAccountIds)); - api(getState).get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`).then(response => { - dispatch(fetchRelationshipsSuccess(response.data)); - }).catch(error => { - dispatch(fetchRelationshipsFail(error)); - }); + return api(getState) + .get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`) + .then(response => dispatch(fetchRelationshipsSuccess(response.data))) + .catch(error => dispatch(fetchRelationshipsFail(error))); }; const fetchRelationshipsRequest = (ids: string[]) => ({