Add tests for fetchFollowing() action

This commit is contained in:
Justin 2022-06-23 15:03:48 -04:00
parent c2620b017b
commit 04a2d6be99
2 changed files with 70 additions and 9 deletions

View file

@ -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: `<https://example.com/api/v1/accounts/${id}/following?since_id=1>; 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);
});
});
});
});

View file

@ -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) => ({