diff --git a/src/teams/teams.sagas.js b/src/teams/teams.sagas.js index 0798b24..f9a32fe 100644 --- a/src/teams/teams.sagas.js +++ b/src/teams/teams.sagas.js @@ -30,11 +30,12 @@ export function* getCurrentTeam() { return getSelectedTeam(teamsState); } +export function* onTeamSelect(team) { + yield call(fetchProfile, team.id, team.member_id); + yield call([browserHistory, browserHistory.push], `/clubs/joined`); +} + export function* handleSelectTeam() { - function* onTeamSelect(team) { - yield call(fetchProfile, team.id, team.member_id); - yield call([browserHistory, browserHistory.push], `/clubs/joined`); - } yield takeLatest(SELECT_TEAM, onTeamSelect); } @@ -71,15 +72,16 @@ export function* leaveTeam() { yield takeEvery(LEAVE_TEAM, leave); } +export function* onTeamCreate(action) { + // TODO First validate form data + yield call(authenticate); // TODO check if not authenticated within this generator itself + const team = yield call(createTeam, action); + yield call(fetchTeams); + yield call(fetchProfile, team.id, team.member_id); // TODO Should not get there if failed during any previous steps + yield call([browserHistory, browserHistory.push], '/match'); +} + export function* teamCreationFlow() { - function* onTeamCreate(action) { - // TODO First validate form data - yield call(authenticate); // TODO check if not authenticated within this generator itself - const team = yield call(createTeam, action); - yield call(fetchTeams); - yield call(fetchProfile, team.id, team.member_id); // TODO Should not get there if failed during any previous steps - yield call([browserHistory, browserHistory.push], '/match'); - } yield takeLatest(REQUEST_CREATE_TEAM, onTeamCreate); } @@ -139,40 +141,42 @@ export function* fetchPendingMembers() { } } -export function* memberAcceptance() { - while (true) { - const action = yield take(MEMBER_ACCEPTANCE); - const currentTeam = yield call(getCurrentTeam); - const url = api.urls.teamMemberEntity(currentTeam.id, action.id); - try { - if (action.shouldAccept) { - yield call(api.requests.patch, url, { is_accepted: true }, 'Cannot accept membership of this user.'); - } else { - yield call(api.requests['delete'], url, { is_accepted: true }, 'Cannot reject membership of this user'); - } - yield put(showInfo(`User membership ${action.shouldAccept ? 'confirmed' : 'rejected'} successfully.`)) - } catch (error) { - yield put(raiseError(error)); - } finally { - yield call(fetchPendingMembers); +export function* onMemberAccept(action) { + const currentTeam = yield call(getCurrentTeam); + const url = api.urls.teamMemberEntity(currentTeam.id, action.id); + try { + if (action.shouldAccept) { + yield call(api.requests.patch, url, { is_accepted: true }, 'Cannot accept membership of this user.'); + } else { + yield call(api.requests['delete'], url, { is_accepted: true }, 'Cannot reject membership of this user'); } + yield put(showInfo(`User membership ${action.shouldAccept ? 'confirmed' : 'rejected'} successfully.`)) + } catch (error) { + yield put(raiseError(error)); + } finally { + yield call(fetchPendingMembers); } } -export function* manageUser() { - function* onManageUser({updatedProfile: {id, username, is_team_admin, hidden}}) { - const error_msg = `Failed to manage ${username} settings.`; - const currentTeam = yield call(getCurrentTeam); - const url = api.urls.teamMemberEntity(currentTeam.id, id); - try { - const response = yield call(api.requests.patch, url, {is_team_admin, hidden}, error_msg); - yield put(showInfo(`Updated ${username} settings.`)); - yield put(profileUpdate(response)); - } - catch (error) { - yield put(raiseError(error)); - } +export function* memberAcceptance() { + yield takeLatest(MEMBER_ACCEPTANCE, onMemberAccept); +} + +export function* onManageUser({updatedProfile: {id, username, is_team_admin, hidden}}) { + const error_msg = `Failed to manage ${username} settings.`; + const currentTeam = yield call(getCurrentTeam); + const url = api.urls.teamMemberEntity(currentTeam.id, id); + try { + const response = yield call(api.requests.patch, url, {is_team_admin, hidden}, error_msg); + yield put(showInfo(`Updated ${username} settings.`)); + yield put(profileUpdate(response)); + } + catch (error) { + yield put(raiseError(error)); } +} + +export function* manageUser() { yield takeLatest(MANAGE_USER, onManageUser); } diff --git a/src/test/profile.components.test.js b/src/test/profile.components.test.js deleted file mode 100644 index 6b3bbb0..0000000 --- a/src/test/profile.components.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import React from 'react'; -import { shallow } from 'enzyme'; -import { Panel } from 'react-bootstrap'; -import ProfileTeams from '../profile/components/ProfileTeams'; - - -describe('Profile teams page', () => { - describe('ProfileTeams component', () => { - xit('should render properly and consist of a Panel', () => { - const component = shallow(); - expect(component.find(Panel)).toHaveLength(1); - }); - }); -}); diff --git a/src/test/settings.sagas.test.js b/src/test/settings.sagas.test.js index 30d9ec4..a9d0744 100644 --- a/src/test/settings.sagas.test.js +++ b/src/test/settings.sagas.test.js @@ -1,6 +1,6 @@ -import { call, put, take, takeLatest } from 'redux-saga/effects'; +import { call, put, takeLatest } from 'redux-saga/effects'; import api from '../api'; -import { raiseError, showInfo } from '../shared/notifier.actions'; +import { showInfo } from '../shared/notifier.actions'; import { saveSettings, onRequestSaveSettings, validateMember } from '../settings/settings.sagas'; @@ -28,33 +28,14 @@ describe('onRequestSaveSettings saga', () => { }); describe('Save settings saga', () => { - const memberSettings = { username: 'ABC123', }; - const profileSettings = { first_name: 'ABC', last_name: '123', }; + const settings = { first_name: 'ABC', last_name: '123', username: 'ABC123', hidden: true}; const currentTeam = { id: 1, member_id: 15, }; - const successMsg = 'Profile settings were saved'; - const errorMsg = 'Failed to save profile settings'; + const successMsg = 'Your settings were saved'; + const errorMsg = 'Failed to save settings'; - describe('Scenario 1: Should save profile only', () => { - const action = requestSaveSettings({}, profileSettings); - const iterator = saveSettings(action); - const profileUrl = api.urls.profile(); - - it('should call API with PATCH request to save profile', () => { - const iter = iterator.next(action).value; - expect(iter).toEqual(call(api.requests.patch, profileUrl, action.values, errorMsg)) - }); - - it('should show info about success', () => { - expect(iterator.next().value).toEqual(put(showInfo(successMsg))); - }); - - it('should dispatch action SETTINGS_SAVED', () => { - expect(iterator.next().value).toEqual(put(settingsSaved(action.values))); - }); - }); - describe('Scenario 2: Should save member only', () => { - const action = requestSaveSettings({}, memberSettings); + describe('Scenario 1: Should save settings', () => { + const action = requestSaveSettings({}, settings); const iterator = saveSettings(action); const memberUrl = api.urls.teamMemberEntity(currentTeam.id, currentTeam.member_id); @@ -80,42 +61,6 @@ describe('Save settings saga', () => { expect(iterator.next().value).toEqual(expected); }); }); - - describe('Scenario 3: Should both profile and member', () => { - const action = requestSaveSettings({}, { ...memberSettings, ...profileSettings }); - const iterator = saveSettings(action); - const profileUrl = api.urls.profile(); - const memberUrl = api.urls.teamMemberEntity(currentTeam.id, currentTeam.member_id); - - it('should call API with PATCH request to save profile', () => { - const iter = iterator.next().value; - expect(iter).toEqual(call(api.requests.patch, profileUrl, profileSettings, errorMsg)); - }); - - it('should get current team', () => { - expect(iterator.next().value).toEqual(call(getCurrentTeam)); - }); - - it('should call API with PATCH request to save profile', () => { - const iter = iterator.next(currentTeam).value; - expect(iter).toEqual(call(api.requests.patch, memberUrl, memberSettings, errorMsg)); - }); - - it('should show info about success', () => { - expect(iterator.next().value).toEqual(put(showInfo(successMsg))); - }); - - it('should dispatch action SETTINGS_SAVED', () => { - expect(iterator.next().value).toEqual(put(settingsSaved(action.values))); - }); - - it('should redir to new profile url', () => { - const expected = call([browserHistory, browserHistory.push], '/profile/ABC123/settings'); - expect(iterator.next().value).toEqual(expected); - }); - }); - - }); describe('Validate member profile data', () => { diff --git a/src/test/teams.sagas.test.js b/src/test/teams.sagas.test.js index 2330856..4d51768 100644 --- a/src/test/teams.sagas.test.js +++ b/src/test/teams.sagas.test.js @@ -10,7 +10,7 @@ import { initTeam, handleJoinTeam, fetchPendingMembers, - getCurrentTeam + getCurrentTeam, onTeamCreate, onTeamSelect } from '../teams/teams.sagas'; import { authenticate, fetchProfile } from '../shared/auth/auth.sagas'; import { requestJoinTeam } from '../teams/teams.actions'; @@ -60,16 +60,12 @@ describe('StateTeamsSelector', () => { describe('TeamCreationFlow saga', () => { describe('Scenario 1: Typical [Success]', () => { - const iterator = teamCreationFlow(); const action = requestCreateTeam('Team', 'Username'); + const iterator = onTeamCreate(action); const team = { id: 1, name: 'Team', member_id: 7 }; - it('should wait for REQUEST_CREATE_TEAM', () => { - expect(iterator.next().value).toEqual(take(action.type)); - }); - it('should attempt authenticating user', () => { - const iter = iterator.next(action).value; + const iter = iterator.next().value; expect(iter).toEqual(call(authenticate)); }); @@ -149,14 +145,14 @@ describe('CreateTeam saga - success scenario', () => { }); describe('HandleSelectTeam saga', () => { - const iterator = handleSelectTeam(); const team = { id: 7, member_id: 15, username: 'Axis' }; + const iterator = onTeamSelect(team); - it('should wait to take SELECT_TEAM', () => { + xit('should wait to take SELECT_TEAM', () => { const iter = iterator.next().value; expect(iter).toEqual(take(SELECT_TEAM)); }); @@ -171,7 +167,7 @@ describe('HandleSelectTeam saga', () => { expect(iter).toEqual(call([browserHistory, browserHistory.push], `/clubs/joined`)); }); - it('should not return from saga', () => { + xit('should not return from saga', () => { const iter = iterator.next(); expect(iter.done).toEqual(false); expect(iter.value).toEqual(take(SELECT_TEAM));