Add tests for expandFollowers() action

This commit is contained in:
Justin 2022-06-23 14:59:37 -04:00
parent 761d524fdb
commit c2620b017b
2 changed files with 102 additions and 11 deletions

View file

@ -8,6 +8,7 @@ import { normalizeAccount } from '../../normalizers';
import {
blockAccount,
createAccount,
expandFollowers,
fetchAccount,
fetchAccountByUsername,
fetchFollowers,
@ -1045,4 +1046,91 @@ describe('fetchFollowers()', () => {
});
});
});
});
});
describe('expandFollowers()', () => {
const id = '1';
describe('when logged in', () => {
beforeEach(() => {
const state = rootReducer(undefined, {})
.set('user_lists', ImmutableMap({
followers: ImmutableMap({
[id]: 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({
followers: ImmutableMap({
[id]: ImmutableMap({
next: null,
}),
}),
}))
.set('me', '123');
store = mockStore(state);
});
it('should do nothing', async() => {
await store.dispatch(expandFollowers(id));
const actions = store.getActions();
expect(actions).toEqual([]);
});
});
describe('with a successful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('next_url').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_EXPAND_REQUEST', id },
{ type: 'ACCOUNTS_IMPORT', accounts: [] },
{
type: 'FOLLOWERS_EXPAND_SUCCESS',
id,
accounts: [],
next: null,
},
];
await store.dispatch(expandFollowers(id));
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: 'FOLLOWERS_EXPAND_REQUEST', id },
{ type: 'FOLLOWERS_EXPAND_FAIL', id, error: new Error('Network Error') },
];
await store.dispatch(expandFollowers(id));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});
});

View file

@ -532,25 +532,28 @@ const fetchFollowersFail = (id: string, error: AxiosError) => ({
const expandFollowers = (id: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return;
if (!isLoggedIn(getState)) return null;
const url = getState().user_lists.getIn(['followers', id, 'next']);
if (url === null) {
return;
return null;
}
dispatch(expandFollowersRequest(id));
api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
return api(getState)
.get(url)
.then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedAccounts(response.data));
dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
}).catch(error => {
dispatch(expandFollowersFail(id, error));
});
dispatch(importFetchedAccounts(response.data));
dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
})
.catch(error => {
dispatch(expandFollowersFail(id, error));
});
};
const expandFollowersRequest = (id: string) => ({