Skip to content

Commit

Permalink
test(*): Refactor sagas unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoTN committed Dec 28, 2017
1 parent 062477e commit e84289f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 127 deletions.
86 changes: 45 additions & 41 deletions src/teams/teams.sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 0 additions & 14 deletions src/test/profile.components.test.js

This file was deleted.

69 changes: 7 additions & 62 deletions src/test/settings.sagas.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);

Expand All @@ -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', () => {
Expand Down
16 changes: 6 additions & 10 deletions src/test/teams.sagas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
});

Expand Down Expand Up @@ -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));
});
Expand All @@ -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));
Expand Down

0 comments on commit e84289f

Please sign in to comment.