-
-
Save defrian/fd6dd7f072754e833b60c7bd6ed45159 to your computer and use it in GitHub Desktop.
Authentication Redux Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const firebase = jest.genMockFromModule('firebase'); | |
const ref = jest.fn(() => ({ | |
child: jest.fn(() => ref), | |
update: jest.fn(() => { | |
console.log('Called update'); | |
return Promise.resolve(); | |
}), | |
})); | |
firebase.initializeApp = jest.fn(); | |
firebase.database = jest.fn(() => ({ | |
ref, | |
})); | |
firebase.auth = jest.fn().mockReturnValue({ | |
currentUser: true, | |
signOut() { | |
return Promise.resolve(); | |
}, | |
signInWithEmailAndPassword(email, password) { | |
return new Promise((resolve, reject) => { | |
if (password !== 'signIn') { | |
reject(Error('signIn error ')); | |
} | |
resolve({ name: 'signedUser' }); | |
}); | |
}, | |
}); | |
module.exports = firebase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import firebase from 'firebase'; | |
import * as Keychain from 'react-native-keychain'; | |
import NavigationService from '../config/NavigationService'; | |
import { | |
LOGIN_USER, | |
LOGIN_USER_SUCCESS, | |
LOGIN_USER_FAIL, | |
LOGIN_EMAIL_CHANGED, | |
LOGIN_PASSWORD_CHANGED, | |
LOGOUT_USER_SUCCESS, | |
LOGOUT_USER_FAIL, | |
} from './types'; | |
const saveCredentials = async (email, password) => { | |
// Store the credentials | |
console.log('Store Credentials'); | |
await Keychain.setGenericPassword(email, password); | |
try { | |
// Retreive the credentials | |
const credentials = await Keychain.getGenericPassword(); | |
if (credentials) { | |
console.log(`Credentials successfully loaded for user ${credentials.username}`); | |
console.log('Credentials successfully loaded: ', credentials); | |
} else { | |
console.log('No credentials stored'); | |
} | |
} catch (error) { | |
console.log("Keychain couldn't be accessed!", error); | |
} | |
}; | |
const resetCredentials = async () => { | |
console.log('creds reset'); | |
await Keychain.resetGenericPassword(); | |
}; | |
export const logOutUser = () => dispatch => | |
firebase | |
.auth() | |
.signOut() | |
.then(dispatch(() => resetCredentials()).then(dispatch({ | |
type: LOGOUT_USER_SUCCESS, | |
}))) | |
.catch(error => ({ | |
type: LOGOUT_USER_FAIL, | |
payload: error, | |
})); | |
export const loginUserSuccess = (dispatch, user) => { | |
dispatch({ | |
type: LOGIN_USER_SUCCESS, | |
payload: user, | |
}); | |
NavigationService.navigate('Home'); | |
}; | |
export const loginUserFail = error => ({ | |
type: LOGIN_USER_FAIL, | |
payload: error, | |
}); | |
export const loginUser = ({ email, password }) => (dispatch) => { | |
dispatch({ type: LOGIN_USER }); | |
return firebase | |
.auth() | |
.signInWithEmailAndPassword(email, password) | |
.then((user) => { | |
loginUserSuccess(dispatch, user); | |
}) | |
.catch((error) => { | |
console.log(`sign in fail ${error}`); | |
dispatch(loginUserFail(error.message)); | |
}); | |
}; | |
export const emailChanged = text => ({ | |
type: LOGIN_EMAIL_CHANGED, | |
payload: text, | |
}); | |
export const passwordChanged = text => ({ | |
type: LOGIN_PASSWORD_CHANGED, | |
payload: text, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import configureMockStore from 'redux-mock-store'; | |
import thunk from 'redux-thunk'; | |
import * as actions from '../../app/actions/AuthActions'; | |
import * as types from '../../app/actions/types'; | |
const middlewares = [thunk]; | |
const mockStore = configureMockStore(middlewares); | |
describe('login actions', () => { | |
let store; | |
beforeEach(() => { | |
store = mockStore({}); | |
}); | |
it('signIn should call firebase', () => { | |
const user = { | |
email: 'test@test.com', | |
password: 'signIn', | |
}; | |
const expected = [{ type: types.LOGIN_USER }]; | |
return store.dispatch(actions.loginUser(user)).then(() => { | |
expect(store.getActions()).toEqual(expected); | |
}); | |
}); | |
}); | |
it('creates an action that changes email', () => { | |
const expected = { type: types.LOGIN_EMAIL_CHANGED, payload: 'a' }; | |
const actual = actions.emailChanged('a'); | |
expect(actual).toEqual(expected); | |
}); | |
it('creates an action that changes password', () => { | |
const expected = { type: types.LOGIN_PASSWORD_CHANGED, payload: 'a' }; | |
const actual = actions.passwordChanged('a'); | |
expect(actual).toEqual(expected); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment