Add tests for expandFollowers() action
This commit is contained in:
parent
761d524fdb
commit
c2620b017b
|
@ -8,6 +8,7 @@ import { normalizeAccount } from '../../normalizers';
|
||||||
import {
|
import {
|
||||||
blockAccount,
|
blockAccount,
|
||||||
createAccount,
|
createAccount,
|
||||||
|
expandFollowers,
|
||||||
fetchAccount,
|
fetchAccount,
|
||||||
fetchAccountByUsername,
|
fetchAccountByUsername,
|
||||||
fetchFollowers,
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -532,25 +532,28 @@ const fetchFollowersFail = (id: string, error: AxiosError) => ({
|
||||||
|
|
||||||
const expandFollowers = (id: string) =>
|
const expandFollowers = (id: string) =>
|
||||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
if (!isLoggedIn(getState)) return;
|
if (!isLoggedIn(getState)) return null;
|
||||||
|
|
||||||
const url = getState().user_lists.getIn(['followers', id, 'next']);
|
const url = getState().user_lists.getIn(['followers', id, 'next']);
|
||||||
|
|
||||||
if (url === null) {
|
if (url === null) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(expandFollowersRequest(id));
|
dispatch(expandFollowersRequest(id));
|
||||||
|
|
||||||
api(getState).get(url).then(response => {
|
return api(getState)
|
||||||
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
.get(url)
|
||||||
|
.then(response => {
|
||||||
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
||||||
|
|
||||||
dispatch(importFetchedAccounts(response.data));
|
dispatch(importFetchedAccounts(response.data));
|
||||||
dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
|
dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
|
||||||
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
|
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
|
||||||
}).catch(error => {
|
})
|
||||||
dispatch(expandFollowersFail(id, error));
|
.catch(error => {
|
||||||
});
|
dispatch(expandFollowersFail(id, error));
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const expandFollowersRequest = (id: string) => ({
|
const expandFollowersRequest = (id: string) => ({
|
||||||
|
|
Loading…
Reference in New Issue