Add tests for fetchRelationships() action
This commit is contained in:
parent
2cc38554fa
commit
ba595259c1
2 changed files with 100 additions and 7 deletions
|
@ -14,6 +14,7 @@ import {
|
||||||
fetchAccountByUsername,
|
fetchAccountByUsername,
|
||||||
fetchFollowers,
|
fetchFollowers,
|
||||||
fetchFollowing,
|
fetchFollowing,
|
||||||
|
fetchRelationships,
|
||||||
followAccount,
|
followAccount,
|
||||||
muteAccount,
|
muteAccount,
|
||||||
removeFromFollowers,
|
removeFromFollowers,
|
||||||
|
@ -1308,3 +1309,96 @@ describe('expandFollowing()', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('fetchRelationships()', () => {
|
||||||
|
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(fetchRelationships([id]));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when logged in', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const state = rootReducer(undefined, {})
|
||||||
|
.set('me', '123');
|
||||||
|
store = mockStore(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('without newAccountIds', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const state = rootReducer(undefined, {})
|
||||||
|
.set('relationships', ImmutableMap({ [id]: {} }))
|
||||||
|
.set('me', '123');
|
||||||
|
store = mockStore(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should do nothing', async() => {
|
||||||
|
await store.dispatch(fetchRelationships([id]));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with a successful API request', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const state = rootReducer(undefined, {})
|
||||||
|
.set('relationships', ImmutableMap({}))
|
||||||
|
.set('me', '123');
|
||||||
|
store = mockStore(state);
|
||||||
|
|
||||||
|
__stub((mock) => {
|
||||||
|
mock
|
||||||
|
.onGet(`/api/v1/accounts/relationships?${[id].map(id => `id[]=${id}`).join('&')}`)
|
||||||
|
.reply(200, []);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should dispatch the correct actions', async() => {
|
||||||
|
const expectedActions = [
|
||||||
|
{ type: 'RELATIONSHIPS_FETCH_REQUEST', ids: [id], skipLoading: true },
|
||||||
|
{
|
||||||
|
type: 'RELATIONSHIPS_FETCH_SUCCESS',
|
||||||
|
relationships: [],
|
||||||
|
skipLoading: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
await store.dispatch(fetchRelationships([id]));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual(expectedActions);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with an unsuccessful API request', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
__stub((mock) => {
|
||||||
|
mock
|
||||||
|
.onGet(`/api/v1/accounts/relationships?${[id].map(id => `id[]=${id}`).join('&')}`)
|
||||||
|
.networkError();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should dispatch the correct actions', async() => {
|
||||||
|
const expectedActions = [
|
||||||
|
{ type: 'RELATIONSHIPS_FETCH_REQUEST', ids: [id], skipLoading: true },
|
||||||
|
{ type: 'RELATIONSHIPS_FETCH_FAIL', skipLoading: true, error: new Error('Network Error') },
|
||||||
|
];
|
||||||
|
await store.dispatch(fetchRelationships([id]));
|
||||||
|
const actions = store.getActions();
|
||||||
|
|
||||||
|
expect(actions).toEqual(expectedActions);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -656,22 +656,21 @@ const expandFollowingFail = (id: string, error: AxiosError) => ({
|
||||||
|
|
||||||
const fetchRelationships = (accountIds: string[]) =>
|
const fetchRelationships = (accountIds: string[]) =>
|
||||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
if (!isLoggedIn(getState)) return;
|
if (!isLoggedIn(getState)) return null;
|
||||||
|
|
||||||
const loadedRelationships = getState().relationships;
|
const loadedRelationships = getState().relationships;
|
||||||
const newAccountIds = accountIds.filter(id => loadedRelationships.get(id, null) === null);
|
const newAccountIds = accountIds.filter(id => loadedRelationships.get(id, null) === null);
|
||||||
|
|
||||||
if (newAccountIds.length === 0) {
|
if (newAccountIds.length === 0) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(fetchRelationshipsRequest(newAccountIds));
|
dispatch(fetchRelationshipsRequest(newAccountIds));
|
||||||
|
|
||||||
api(getState).get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`).then(response => {
|
return api(getState)
|
||||||
dispatch(fetchRelationshipsSuccess(response.data));
|
.get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`)
|
||||||
}).catch(error => {
|
.then(response => dispatch(fetchRelationshipsSuccess(response.data)))
|
||||||
dispatch(fetchRelationshipsFail(error));
|
.catch(error => dispatch(fetchRelationshipsFail(error)));
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchRelationshipsRequest = (ids: string[]) => ({
|
const fetchRelationshipsRequest = (ids: string[]) => ({
|
||||||
|
|
Loading…
Reference in a new issue