Add tests for expandFollowing() action
This commit is contained in:
parent
04a2d6be99
commit
2cc38554fa
2 changed files with 129 additions and 10 deletions
|
@ -9,6 +9,7 @@ import {
|
||||||
blockAccount,
|
blockAccount,
|
||||||
createAccount,
|
createAccount,
|
||||||
expandFollowers,
|
expandFollowers,
|
||||||
|
expandFollowing,
|
||||||
fetchAccount,
|
fetchAccount,
|
||||||
fetchAccountByUsername,
|
fetchAccountByUsername,
|
||||||
fetchFollowers,
|
fetchFollowers,
|
||||||
|
@ -1052,6 +1053,20 @@ describe('fetchFollowers()', () => {
|
||||||
describe('expandFollowers()', () => {
|
describe('expandFollowers()', () => {
|
||||||
const id = '1';
|
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(expandFollowers(id));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('when logged in', () => {
|
describe('when logged in', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const state = rootReducer(undefined, {})
|
const state = rootReducer(undefined, {})
|
||||||
|
@ -1192,3 +1207,104 @@ describe('fetchFollowing()', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('expandFollowing()', () => {
|
||||||
|
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(expandFollowing(id));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when logged in', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const state = rootReducer(undefined, {})
|
||||||
|
.set('user_lists', ImmutableMap({
|
||||||
|
following: 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({
|
||||||
|
following: ImmutableMap({
|
||||||
|
[id]: ImmutableMap({
|
||||||
|
next: null,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
}))
|
||||||
|
.set('me', '123');
|
||||||
|
store = mockStore(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should do nothing', async() => {
|
||||||
|
await store.dispatch(expandFollowing(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}/following?since_id=1>; rel='prev'`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should dispatch the correct actions', async() => {
|
||||||
|
const expectedActions = [
|
||||||
|
{ type: 'FOLLOWING_EXPAND_REQUEST', id },
|
||||||
|
{ type: 'ACCOUNTS_IMPORT', accounts: [] },
|
||||||
|
{
|
||||||
|
type: 'FOLLOWING_EXPAND_SUCCESS',
|
||||||
|
id,
|
||||||
|
accounts: [],
|
||||||
|
next: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
await store.dispatch(expandFollowing(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: 'FOLLOWING_EXPAND_REQUEST', id },
|
||||||
|
{ type: 'FOLLOWING_EXPAND_FAIL', id, error: new Error('Network Error') },
|
||||||
|
];
|
||||||
|
await store.dispatch(expandFollowing(id));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual(expectedActions);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -612,25 +612,28 @@ const fetchFollowingFail = (id: string, error: AxiosError) => ({
|
||||||
|
|
||||||
const expandFollowing = (id: string) =>
|
const expandFollowing = (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(['following', id, 'next']);
|
const url = getState().user_lists.getIn(['following', id, 'next']);
|
||||||
|
|
||||||
if (url === null) {
|
if (url === null) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(expandFollowingRequest(id));
|
dispatch(expandFollowingRequest(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(expandFollowingSuccess(id, response.data, next ? next.uri : null));
|
dispatch(expandFollowingSuccess(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(expandFollowingFail(id, error));
|
.catch(error => {
|
||||||
});
|
dispatch(expandFollowingFail(id, error));
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const expandFollowingRequest = (id: string) => ({
|
const expandFollowingRequest = (id: string) => ({
|
||||||
|
|
Loading…
Reference in a new issue