sforkowany z mirror/soapbox
Add tests for expandFollowing() action
rodzic
04a2d6be99
commit
2cc38554fa
|
@ -9,6 +9,7 @@ import {
|
|||
blockAccount,
|
||||
createAccount,
|
||||
expandFollowers,
|
||||
expandFollowing,
|
||||
fetchAccount,
|
||||
fetchAccountByUsername,
|
||||
fetchFollowers,
|
||||
|
@ -1052,6 +1053,20 @@ describe('fetchFollowers()', () => {
|
|||
describe('expandFollowers()', () => {
|
||||
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', () => {
|
||||
beforeEach(() => {
|
||||
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) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
if (!isLoggedIn(getState)) return;
|
||||
if (!isLoggedIn(getState)) return null;
|
||||
|
||||
const url = getState().user_lists.getIn(['following', id, 'next']);
|
||||
|
||||
if (url === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
dispatch(expandFollowingRequest(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(expandFollowingSuccess(id, response.data, next ? next.uri : null));
|
||||
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
|
||||
}).catch(error => {
|
||||
dispatch(expandFollowingFail(id, error));
|
||||
});
|
||||
dispatch(importFetchedAccounts(response.data));
|
||||
dispatch(expandFollowingSuccess(id, response.data, next ? next.uri : null));
|
||||
dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.id)));
|
||||
})
|
||||
.catch(error => {
|
||||
dispatch(expandFollowingFail(id, error));
|
||||
});
|
||||
};
|
||||
|
||||
const expandFollowingRequest = (id: string) => ({
|
||||
|
|
Ładowanie…
Reference in New Issue