Add tests for fetchFollowers() action

environments/review-add-accoun-5o034k/deployments/361
Justin 2022-06-23 14:47:43 -04:00
rodzic 931f2e16d8
commit 761d524fdb
2 zmienionych plików z 69 dodań i 8 usunięć

Wyświetl plik

@ -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: `<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);
});
});

Wyświetl plik

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