Add tests for removeFromFollowers() action

This commit is contained in:
Justin 2022-06-23 14:37:55 -04:00
parent 0a9e1c808b
commit 931f2e16d8
2 changed files with 73 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import {
fetchAccountByUsername,
followAccount,
muteAccount,
removeFromFollowers,
subscribeAccount,
unblockAccount,
unfollowAccount,
@ -921,3 +922,69 @@ describe('unsubscribeAccount()', () => {
});
});
});
describe('removeFromFollowers()', () => {
const id = '1';
describe('when logged out', () => {
beforeEach(() => {
const state = rootReducer(undefined, {}).set('me', null);
store = mockStore(state);
});
it('should do nothing', async() => {
await store.dispatch(removeFromFollowers(id));
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(() => {
__stub((mock) => {
mock.onPost(`/api/v1/accounts/${id}/remove_from_followers`).reply(200, {});
});
});
it('should dispatch the correct actions', async() => {
const expectedActions = [
{ type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST', id },
{
type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS',
relationship: {},
},
];
await store.dispatch(removeFromFollowers(id));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('with an unsuccessful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onPost(`/api/v1/accounts/${id}/remove_from_followers`).networkError();
});
});
it('should dispatch the correct actions', async() => {
const expectedActions = [
{ type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST', id },
{ type: 'ACCOUNT_REMOVE_FROM_FOLLOWERS_FAIL', id, error: new Error('Network Error') },
];
await store.dispatch(removeFromFollowers(id));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});
});

View File

@ -468,15 +468,14 @@ const unsubscribeAccountFail = (error: AxiosError) => ({
const removeFromFollowers = (id: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return;
if (!isLoggedIn(getState)) return null;
dispatch(muteAccountRequest(id));
dispatch(removeFromFollowersRequest(id));
api(getState).post(`/api/v1/accounts/${id}/remove_from_followers`).then(response => {
dispatch(removeFromFollowersSuccess(response.data));
}).catch(error => {
dispatch(removeFromFollowersFail(id, error));
});
return api(getState)
.post(`/api/v1/accounts/${id}/remove_from_followers`)
.then(response => dispatch(removeFromFollowersSuccess(response.data)))
.catch(error => dispatch(removeFromFollowersFail(id, error)));
};
const removeFromFollowersRequest = (id: string) => ({