Add tests for expandFollowRequests() action

This commit is contained in:
Justin 2022-06-23 15:31:32 -04:00
parent 6775beba93
commit 7fc43f524a
2 changed files with 105 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import {
createAccount, createAccount,
expandFollowers, expandFollowers,
expandFollowing, expandFollowing,
expandFollowRequests,
fetchAccount, fetchAccount,
fetchAccountByUsername, fetchAccountByUsername,
fetchFollowers, fetchFollowers,
@ -1477,3 +1478,97 @@ describe('fetchFollowRequests()', () => {
}); });
}); });
}); });
describe('expandFollowRequests()', () => {
describe('when logged out', () => {
beforeEach(() => {
const state = rootReducer(undefined, {}).set('me', null);
store = mockStore(state);
});
it('should do nothing', async() => {
await store.dispatch(expandFollowRequests());
const actions = store.getActions();
expect(actions).toEqual([]);
});
});
describe('when logged in', () => {
beforeEach(() => {
const state = rootReducer(undefined, {})
.set('user_lists', ImmutableMap({
follow_requests: ImmutableMap({
next: 'next_url',
}),
}))
.set('me', '123');
store = mockStore(state);
});
describe('when the url is null', () => {
beforeEach(() => {
const state = rootReducer(undefined, {})
.set('user_lists', ImmutableMap({
follow_requests: ImmutableMap({
next: null,
}),
}))
.set('me', '123');
store = mockStore(state);
});
it('should do nothing', async() => {
await store.dispatch(expandFollowRequests());
const actions = store.getActions();
expect(actions).toEqual([]);
});
});
describe('with a successful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('next_url').reply(200, [], {
link: '<next_url>; rel=\'prev\'',
});
});
});
it('should dispatch the correct actions', async() => {
const expectedActions = [
{ type: 'FOLLOW_REQUESTS_EXPAND_REQUEST' },
{ type: 'ACCOUNTS_IMPORT', accounts: [] },
{
type: 'FOLLOW_REQUESTS_EXPAND_SUCCESS',
accounts: [],
next: null,
},
];
await store.dispatch(expandFollowRequests());
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('with an unsuccessful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('next_url').networkError();
});
});
it('should dispatch the correct actions', async() => {
const expectedActions = [
{ type: 'FOLLOW_REQUESTS_EXPAND_REQUEST' },
{ type: 'FOLLOW_REQUESTS_EXPAND_FAIL', error: new Error('Network Error') },
];
await store.dispatch(expandFollowRequests());
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});
});

View File

@ -724,21 +724,24 @@ const fetchFollowRequestsFail = (error: AxiosError) => ({
const expandFollowRequests = () => const expandFollowRequests = () =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return null;
const url = getState().user_lists.getIn(['follow_requests', 'next']); const url = getState().user_lists.getIn(['follow_requests', 'next']);
if (url === null) { if (url === null) {
return; return null;
} }
dispatch(expandFollowRequestsRequest()); dispatch(expandFollowRequestsRequest());
api(getState).get(url).then(response => { return api(getState)
const next = getLinks(response).refs.find(link => link.rel === 'next'); .get(url)
dispatch(importFetchedAccounts(response.data)); .then(response => {
dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null)); const next = getLinks(response).refs.find(link => link.rel === 'next');
}).catch(error => dispatch(expandFollowRequestsFail(error))); dispatch(importFetchedAccounts(response.data));
dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null));
})
.catch(error => dispatch(expandFollowRequestsFail(error)));
}; };
const expandFollowRequestsRequest = () => ({ const expandFollowRequestsRequest = () => ({