From f3479f2e8331be53775b74a4813e16251399438f Mon Sep 17 00:00:00 2001 From: Smirnov Evgenii Date: Sun, 3 Sep 2017 20:43:10 +0300 Subject: [PATCH] hw-2 Add signIn saga, test auth sagas and clear person form --- src/components/routes/AuthPage.js | 6 +- src/ducks/auth.js | 31 ++++++++++ src/ducks/auth.test.js | 96 +++++++++++++++++++++++++++++++ src/ducks/people.js | 3 + 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 src/ducks/auth.test.js diff --git a/src/components/routes/AuthPage.js b/src/components/routes/AuthPage.js index cc9a55f..99d16fd 100644 --- a/src/components/routes/AuthPage.js +++ b/src/components/routes/AuthPage.js @@ -3,7 +3,7 @@ import SignInForm from '../auth/SignInForm' import SignUpForm from '../auth/SignUpForm' import {Route, NavLink} from 'react-router-dom' import {connect} from 'react-redux' -import {signUp, moduleName} from '../../ducks/auth' +import {signUp, moduleName, signIn} from '../../ducks/auth' import Loader from '../common/Loader' class AuthPage extends Component { @@ -25,10 +25,10 @@ class AuthPage extends Component { ) } - handleSignIn = (values) => console.log('---', values) + handleSignIn = ({email, password}) => this.props.signIn(email, password) handleSignUp = ({email, password}) => this.props.signUp(email, password) } export default connect(state => ({ loading: state[moduleName].loading -}), {signUp})(AuthPage) \ No newline at end of file +}), {signUp, signIn})(AuthPage) \ No newline at end of file diff --git a/src/ducks/auth.js b/src/ducks/auth.js index 6bae0d9..3ebd7bf 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -15,7 +15,9 @@ export const moduleName = 'auth' export const SIGN_UP_REQUEST = `${appName}/${moduleName}/SIGN_UP_REQUEST` export const SIGN_UP_SUCCESS = `${appName}/${moduleName}/SIGN_UP_SUCCESS` export const SIGN_UP_ERROR = `${appName}/${moduleName}/SIGN_UP_ERROR` +export const SIGN_IN_REQUEST = `${appName}/${moduleName}/SIGN_IN_REQUEST` export const SIGN_IN_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS` +export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR` export const SIGN_OUT_REQUEST = `${appName}/${moduleName}/SIGN_OUT_REQUEST` export const SIGN_OUT_SUCCESS = `${appName}/${moduleName}/SIGN_OUT_SUCCESS` @@ -53,6 +55,13 @@ export function signUp(email, password) { } } +export function signIn(email, password) { + return { + type: SIGN_IN_REQUEST, + payload: {email, password} + } +} + export function signOut() { return { type: SIGN_OUT_REQUEST @@ -103,6 +112,27 @@ export function signUp(email, password) { } */ + +export const signInSaga = function * (action) { + const auth = firebase.auth() + + try { + const user = yield call( + [auth, auth.signInWithEmailAndPassword], + action.payload.email, action.payload.password + ) + yield put({ + type: SIGN_IN_SUCCESS, + payload: {user} + }) + } catch (error) { + yield put({ + type: SIGN_IN_ERROR, + error + }) + } +} + export const watchStatusChange = function * () { const auth = firebase.auth() try { @@ -144,6 +174,7 @@ export const saga = function * () { yield all([ signUpSaga(), watchStatusChange(), + takeEvery(SIGN_IN_REQUEST, signInSaga), takeEvery(SIGN_OUT_REQUEST, signOutSaga) ]) } \ No newline at end of file diff --git a/src/ducks/auth.test.js b/src/ducks/auth.test.js new file mode 100644 index 0000000..041cfdd --- /dev/null +++ b/src/ducks/auth.test.js @@ -0,0 +1,96 @@ +import firebase from 'firebase' +import {push} from 'react-router-redux' +import {all, cps, call, put, take, takeEvery} from 'redux-saga/effects' +import { + signUpSaga, signOutSaga, watchStatusChange, signInSaga, saga as authSaga, + SIGN_UP_REQUEST, SIGN_UP_SUCCESS, SIGN_UP_ERROR, + SIGN_IN_REQUEST, SIGN_IN_SUCCESS, SIGN_IN_ERROR, + SIGN_OUT_REQUEST, SIGN_OUT_SUCCESS, +} from './auth'; + +const auth = firebase.auth() +const user = { + email: 'a@a.aa', + password: 'aaaaaaaaaa', +} + +describe('test auth sagas', () => { + it('should sign up', () => { + const saga = signUpSaga(); + + expect(saga.next().value).toEqual(take(SIGN_UP_REQUEST)) + + const action = { + type: SIGN_UP_REQUEST, + payload: { ...user } + } + + expect(saga.next(action).value).toEqual( + call([auth, auth.createUserWithEmailAndPassword], user.email, user.password) + ) + + expect(saga.next(user).value).toEqual(put({ + type: SIGN_UP_SUCCESS, + payload: { user } + })) + + expect(saga.throw('test').value).toEqual(put({ + type: SIGN_UP_ERROR, + error: 'test', + })) + }) + + it('should sign in', () => { + const action = { + type: SIGN_IN_REQUEST, + payload: { ...user } + } + + const saga = signInSaga(action); + + expect(saga.next().value).toEqual( + call([auth, auth.signInWithEmailAndPassword], user.email, user.password) + ) + + expect(saga.next(user).value).toEqual(put({ + type: SIGN_IN_SUCCESS, + payload: { user } + })) + + expect(saga.throw('test').value).toEqual(put({ + type: SIGN_IN_ERROR, + error: 'test', + })) + }) + + it('should sign out', () => { + const action = { + type: SIGN_OUT_REQUEST, + } + + const saga = signOutSaga(action); + + expect(saga.next().value).toEqual( + call([auth, auth.signOut]) + ) + + expect(saga.next().value).toEqual(put({ + type: SIGN_OUT_SUCCESS + })) + + expect(saga.next().value).toEqual(put(push('/auth/signin'))) + }) + + it('should watch auth status', () => { + const saga = watchStatusChange(); + + expect(saga.next().value).toEqual( + cps([auth, auth.onAuthStateChanged]) + ) + + expect(saga.throw(user).value).toEqual(put({ + type: SIGN_IN_SUCCESS, + payload: { user }, + })) + }) +}) diff --git a/src/ducks/people.js b/src/ducks/people.js index 8e80b5d..32d1f47 100644 --- a/src/ducks/people.js +++ b/src/ducks/people.js @@ -1,3 +1,4 @@ +import {reset} from 'redux-form' import {appName} from '../config' import {Record, List} from 'immutable' import {put, call, takeEvery} from 'redux-saga/effects' @@ -46,6 +47,8 @@ export const addPersonSaga = function * (action) { type: ADD_PERSON, payload: {...action.payload, id} }) + + yield put(reset('person')) } /*