Add tests for fetchFollowRequests() action

environments/review-add-accoun-5o034k/deployments/361
Justin 2022-06-23 15:24:31 -04:00
rodzic ba595259c1
commit 6775beba93
2 zmienionych plików z 84 dodań i 6 usunięć

Wyświetl plik

@ -14,6 +14,7 @@ import {
fetchAccountByUsername,
fetchFollowers,
fetchFollowing,
fetchFollowRequests,
fetchRelationships,
followAccount,
muteAccount,
@ -1402,3 +1403,77 @@ describe('fetchRelationships()', () => {
});
});
});
describe('fetchFollowRequests()', () => {
describe('when logged out', () => {
beforeEach(() => {
const state = rootReducer(undefined, {}).set('me', null);
store = mockStore(state);
});
it('should do nothing', async() => {
await store.dispatch(fetchFollowRequests());
const actions = store.getActions();
expect(actions).toEqual([]);
});
});
describe('when logged in', () => {
beforeEach(() => {
const state = rootReducer(undefined, {})
.set('me', '123');
store = mockStore(state);
});
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/follow_requests').reply(200, [], {
link: '<https://example.com/api/v1/follow_requests?since_id=1>; rel=\'prev\'',
});
});
});
it('should dispatch the correct actions', async() => {
const expectedActions = [
{ type: 'FOLLOW_REQUESTS_FETCH_REQUEST' },
{ type: 'ACCOUNTS_IMPORT', accounts: [] },
{
type: 'FOLLOW_REQUESTS_FETCH_SUCCESS',
accounts: [],
next: null,
},
];
await store.dispatch(fetchFollowRequests());
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('with an unsuccessful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v1/follow_requests').networkError();
});
});
it('should dispatch the correct actions', async() => {
const expectedActions = [
{ type: 'FOLLOW_REQUESTS_FETCH_REQUEST' },
{ type: 'FOLLOW_REQUESTS_FETCH_FAIL', error: new Error('Network Error') },
];
await store.dispatch(fetchFollowRequests());
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});
});

Wyświetl plik

@ -693,15 +693,18 @@ const fetchRelationshipsFail = (error: AxiosError) => ({
const fetchFollowRequests = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return;
if (!isLoggedIn(getState)) return null;
dispatch(fetchFollowRequestsRequest());
api(getState).get('/api/v1/follow_requests').then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedAccounts(response.data));
dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null));
}).catch(error => dispatch(fetchFollowRequestsFail(error)));
return api(getState)
.get('/api/v1/follow_requests')
.then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedAccounts(response.data));
dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null));
})
.catch(error => dispatch(fetchFollowRequestsFail(error)));
};
const fetchFollowRequestsRequest = () => ({