Skip to content

Commit

Permalink
test(Auth): Fix UTs for Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoTN committed Feb 5, 2018
1 parent 84db946 commit 99216e5
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 136 deletions.
14 changes: 8 additions & 6 deletions src/matches/matches.sagas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import api from '../api/index';
import * as MatchActions from './match.actions';
import * as MatchTypes from './match.types';
import { raiseError, showInfo } from '../shared/notifier.actions';
import { publish, removeMatch, listMatches, stateTeamsSelectedSelector } from './matches.sagas';
import { publish, removeMatch, listMatches } from './matches.sagas';
import { fetchUpdateUsers } from '../users/users.sagas';
import { getSelectedTeamId } from "../teams/teams.reducer";


describe('Publish match saga', () => {
Expand Down Expand Up @@ -36,7 +37,8 @@ describe('Publish match saga', () => {
});

it('should select team id', () => {
expect(JSON.stringify(iterator.next(MatchActions.publish(matchData, callback)).value)).toEqual(JSON.stringify(select(() => 1)));
const iter = iterator.next(MatchActions.publish(matchData, callback)).value;
expect(iter).toEqual(select(getSelectedTeamId));
});

it('should call api to publish match', () => {
Expand Down Expand Up @@ -87,8 +89,8 @@ describe('Publish match saga', () => {
});

it('should select team id', () => {
const iter = JSON.stringify(iterator.next(MatchActions.publish(matchData, callback)).value);
expect(iter).toEqual(JSON.stringify(select(() => currentTeamId)));
const iter = iterator.next(MatchActions.publish(matchData, callback)).value;
expect(iter).toEqual(select(getSelectedTeamId));
});

it('should call api to publish match', () => {
Expand Down Expand Up @@ -175,7 +177,7 @@ describe('ListMatches saga', () => {
const iterator = listMatches(params);

it('should select team selected from store', () => {
expect(iterator.next(currentTeamId).value).toEqual(select(stateTeamsSelectedSelector));
expect(iterator.next(currentTeamId).value).toEqual(select(getSelectedTeamId));
});
it('should call fetch data from API', () => {
expect(iterator.next(currentTeamId).value).toEqual(call(api.requests.get, url, params, errorMsg));
Expand All @@ -192,7 +194,7 @@ describe('ListMatches saga', () => {
const iterator = listMatches(params);

it('should select team selected from store', () => {
expect(iterator.next(currentTeamId).value).toEqual(select(stateTeamsSelectedSelector));
expect(iterator.next(currentTeamId).value).toEqual(select(getSelectedTeamId));
});
it('should call fetch data from API', () => {
expect(iterator.next(currentTeamId).value).toEqual(call(api.requests.get, url, params, errorMsg));
Expand Down
31 changes: 6 additions & 25 deletions src/settings/settings.sagas.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import { call, put, takeLatest, select } from 'redux-saga/effects';
import { call, put, select } from 'redux-saga/effects';
import api from '../api/index';
import { showInfo } from '../shared/notifier.actions';
import {
saveSettings, onRequestSaveSettings, validateMember
} from './settings.sagas';
import { showInfo } from '../shared/notifier.actions';
import { saveSettings, validateMember } from './settings.sagas';
import { settingsSaved } from './settings.actions';
import { browserHistory } from 'react-router'

import {
REQUEST_SAVE_SETTINGS,
requestSaveSettings,
} from './settings.actions';
import { requestSaveSettings } from './settings.actions';
import { getSelectedTeam } from "../teams/teams.reducer";



describe('onRequestSaveSettings saga', () => {
const iterator = onRequestSaveSettings();

it('should take latest REQUEST_SAVE_SETTINGS', () => {
expect(iterator.next().value).toEqual(takeLatest(REQUEST_SAVE_SETTINGS, saveSettings));
});

it('should return from the saga', () => {
expect(iterator.next().done).toBe(true);
});
});

describe('Save settings saga', () => {
const settings = { first_name: 'ABC', last_name: '123', username: 'ABC123', hidden: true};
const currentTeam = { id: 1, member_id: 15, };
const settings = {first_name: 'ABC', last_name: '123', username: 'ABC123', hidden: true};
const currentTeam = {id: 1, member_id: 15,};
const successMsg = 'Your settings were saved';
const errorMsg = 'Failed to save settings';

Expand Down
112 changes: 7 additions & 105 deletions src/shared/auth/auth.sagas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { prepareWindow } from '../../api/oauth';
import api from '../../api/index';
import * as AuthActions from './auth.actions';
import { clean, raiseError } from '../notifier.actions';
import { authenticate, loginFlow, signIn, fetchProfile } from './auth.sagas';
import { authenticate, onSignIn, fetchProfile } from './auth.sagas';
import { getOAuthErrorMsg } from './auth.utils';
import { fetchTeams, initTeam } from '../../teams/teams.sagas';
import { removeState } from '../../persistence';
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('Authenticate saga', () => {

it('should return token and complete', () => {
const iter = iterator.next(fixture.token).value;
expect(iter).toEqual(fixture);
expect(iter).toEqual(fixture.token);
expect(iterator.next().done).toEqual(true);
});
});
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('Authenticate saga', () => {

it('should complete', () => {
const iter = iterator.next('some-token').value;
expect(iter).toEqual({});
expect(iter).toEqual('');
expect(iterator.next().done).toEqual(true);
});
});
Expand All @@ -87,22 +87,18 @@ describe('Authenticate saga', () => {

describe('SignIn saga', () => {
describe('Scenario 1: Typical [Success]', () => {
const iterator = signIn();
const iterator = onSignIn();
const fixtureTeam = {
id: 1,
member_id: 7,
};

it('should wait for action SIGN_IN', () => {
expect(iterator.next(AuthActions.signIn()).value).toEqual(take(AuthActions.signIn().type));
});

it('should call Authenticate saga', () => {
expect(iterator.next().value).toEqual(call(authenticate));
});

it('should call FetchTeams saga', () => {
expect(iterator.next().value).toEqual(call(fetchTeams));
expect(iterator.next('token').value).toEqual(call(fetchTeams));
});

it('should call InitTeam saga', () => {
Expand All @@ -120,17 +116,14 @@ describe('SignIn saga', () => {
});

describe('Scenario 2: User not assigned to any team', () => {
const iterator = signIn();
it('should wait for action SIGN_IN', () => {
expect(iterator.next(AuthActions.signIn()).value).toEqual(take(AuthActions.signIn().type));
});
const iterator = onSignIn();

it('should call Authenticate saga', () => {
expect(iterator.next().value).toEqual(call(authenticate));
});

it('should call FetchTeams saga', () => {
expect(iterator.next().value).toEqual(call(fetchTeams));
expect(iterator.next('token').value).toEqual(call(fetchTeams));
});

it('should call InitTeam saga', () => {
Expand All @@ -144,97 +137,6 @@ describe('SignIn saga', () => {
});


describe('LoginFlow saga', () => {
describe('Scenario 1: Typical success', () => {
const iterator = loginFlow();
let signInSaga;
it('should fork SignIn saga', () => {
signInSaga = fork(signIn);
expect(iterator.next().value).toEqual(signInSaga);
});

it('should wait for SIGN_OUT action', () => {
expect(iterator.next(createMockTask()).value).toEqual(take(AuthActions.signOut().type));
});

it('should cancel SignIn saga', () => {
expect(JSON.stringify(iterator.next().value)).toEqual(JSON.stringify(cancel(createMockTask())));
});

it('should call API sign out', () => {
const logout_url = api.urls.logout();
const expected = call(api.requests.get, logout_url, null, 'Failed to sign out. Please try again.');
expect(iterator.next().value).toEqual(expected)
});

it('should dispatch SIGNED_OUT action', () => {
expect(iterator.next().value).toEqual(put(AuthActions.signedOut()));
});

it('should clean notifications', () => {
expect(iterator.next().value).toEqual(put(clean()));
});

it('should clean localStorage', () => {
expect(iterator.next().value).toEqual(call(removeState));
});

it('should redirect to home page', () => {
expect(iterator.next().value).toEqual(call([browserHistory, browserHistory.push], '/'));
});

it('should not complete the saga', () => {
expect(iterator.next().done).toEqual(false); // Fork signIn again
});
});

describe('Scenario 2: Failed to sign out', () => {
const iterator = loginFlow();
const error_msg = 'Failed to sign out. Please try again.';
let signInSaga;
it('should fork SignIn saga', () => {
signInSaga = fork(signIn);
expect(iterator.next().value).toEqual(signInSaga);
});

it('should wait for SIGN_OUT action', () => {
expect(iterator.next(createMockTask()).value).toEqual(take(AuthActions.signOut().type));
});

it('should cancel SignIn saga', () => {
expect(JSON.stringify(iterator.next().value)).toEqual(JSON.stringify(cancel(createMockTask())));
});

it('should call API sign out', () => {
const logout_url = api.urls.logout();
const expected = call(api.requests.get, logout_url, null, error_msg);
expect(iterator.next().value).toEqual(expected)
});

it('should dispatch SIGNED_OUT action', () => {
expect(iterator.next().value).toEqual(put(AuthActions.signedOut()));
});

it('should clean notifications', () => {
expect(iterator.next().value).toEqual(put(clean()));
});

it('should clean localStorage', () => {
expect(iterator.next().value).toEqual(call(removeState));
});

it('should redirect to home page', () => {
expect(iterator.next().value).toEqual(call([browserHistory, browserHistory.push], '/'));
});

it('should restart the saga', () => {
const iter = iterator.next();
expect(iter.done).toEqual(false);
expect(iter.value).toEqual(fork(signIn));
})
})
});

describe('Fetch profile saga', () => {
const team = {id: 1, member_id: 7};
const iterator = fetchProfile(team.id, team.member_id);
Expand Down
Loading

0 comments on commit 99216e5

Please sign in to comment.