Add tests for fetchFollowers() action

This commit is contained in:
Justin 2022-06-23 14:47:43 -04:00
parent 931f2e16d8
commit 761d524fdb
2 changed files with 69 additions and 8 deletions

View file

@ -10,6 +10,7 @@ import {
createAccount, createAccount,
fetchAccount, fetchAccount,
fetchAccountByUsername, fetchAccountByUsername,
fetchFollowers,
followAccount, followAccount,
muteAccount, muteAccount,
removeFromFollowers, removeFromFollowers,
@ -988,3 +989,60 @@ describe('removeFromFollowers()', () => {
}); });
}); });
}); });
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: `<https://example.com/api/v1/accounts/${id}/followers?since_id=1>; 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);
});
});
});
});

View file

@ -498,13 +498,16 @@ const fetchFollowers = (id: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch(fetchFollowersRequest(id)); dispatch(fetchFollowersRequest(id));
api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => { return api(getState)
.get(`/api/v1/accounts/${id}/followers`)
.then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next'); const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedAccounts(response.data)); dispatch(importFetchedAccounts(response.data));
dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null)); dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id))); dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
}).catch(error => { })
.catch(error => {
dispatch(fetchFollowersFail(id, error)); dispatch(fetchFollowersFail(id, error));
}); });
}; };