Skip to content
Prev Previous commit
Next Next commit
Unit tests added for updateEmail, updateProfile, and updateAuth helpers.
  • Loading branch information
Scott Prue committed May 22, 2017
commit 52636aa87f9d3370cea88eb388a1819e7e3cc3fc
59 changes: 44 additions & 15 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import {
} from 'lodash'
import jwtDecode from 'jwt-decode'
import { actionTypes, defaultJWTProps } from '../constants'
import { getLoginMethodAndParams } from '../utils/auth'
import {
promisesForPopulate,
getPopulateObjs,
getChildType
} from '../utils/populate'
import { getLoginMethodAndParams } from '../utils/auth'
import { wrapInDispatch } from '../utils/actions'

const {
SET,
Expand Down Expand Up @@ -546,14 +545,25 @@ export const updateAuth = (dispatch, firebase, authUpdate, updateInProfile) => {
type: actionTypes.AUTH_UPDATE_START,
payload: authUpdate
})
if (!firebase.auth().currentUser) {
const msg = 'User must be logged in to update auth.'
dispatch({
type: actionTypes.AUTH_UPDATE_ERROR,
payload: msg
})
return Promise.reject(msg)
}
return firebase.auth().currentUser
.updateProfile(authUpdate)
.then((payload) => {
dispatch({
type: actionTypes.AUTH_UPDATE_SUCCESS,
payload: authUpdate
})
return updateProfile(dispatch, firebase, authUpdate)
if (updateInProfile) {
return updateProfile(dispatch, firebase, authUpdate)
}
return payload
})
.catch((payload) => {
dispatch({
Expand All @@ -572,19 +582,38 @@ export const updateAuth = (dispatch, firebase, authUpdate, updateInProfile) => {
* @return {Promise}
* @private
*/
export const updateEmail = (dispatch, firebase, newEmail) =>
wrapInDispatch(dispatch, {
types: [
{
type: actionTypes.EMAIL_UPDATE_START,
payload: newEmail
},
actionTypes.EMAIL_UPDATE_SUCCESS,
actionTypes.EMAIL_UPDATE_ERROR
],
method: firebase.auth().currentUser.updateEmail,
args: [newEmail]
export const updateEmail = (dispatch, firebase, newEmail, updateInProfile) => {
dispatch({
type: actionTypes.EMAIL_UPDATE_START,
payload: newEmail
})
if (!firebase.auth().currentUser) {
const msg = 'User must be logged in to update email.'
dispatch({
type: actionTypes.EMAIL_UPDATE_ERROR,
payload: msg
})
return Promise.reject(msg)
}
return firebase.auth().currentUser
.updateEmail(newEmail)
.then((payload) => {
dispatch({
type: actionTypes.EMAIL_UPDATE_SUCCESS,
payload: newEmail
})
if (updateInProfile) {
return updateProfile(dispatch, firebase, { email: newEmail })
}
return payload
})
.catch((payload) => {
dispatch({
type: actionTypes.EMAIL_UPDATE_ERROR,
payload
})
})
}

export default {
dispatchLoginError,
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/compose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,44 @@ describe('Compose', () => {
}
})

describe('updateProfile', () => {
it('acccepts an object', () =>
expect(helpers.updateProfile({ displayName: 'test' })).to.eventually.become(undefined)
)
})

describe('updateAuth', () => {
it('rejects when not authenticated', () =>
expect(helpers.updateAuth()).to.be.rejectedWith('User must be logged in to update auth.')
)

// TODO: test that update auth when authenticated
it.skip('updates auth object if authenticated', () =>
expect(helpers.updateAuth()).to.eventually.become(undefined)
)

// TODO: test that updateProfile is called if updateInProfile is true
it.skip('calls update profile if updateInProfile is true', () =>
expect(helpers.updateAuth({}, true)).to.eventually.become(undefined)
)
})

describe('updateEmail', () => {
it('rejects when not authenticated', () =>
expect(helpers.updateEmail()).to.be.rejectedWith('User must be logged in to update email.')
)

// TODO: test that update auth when authenticated
it.skip('updates auth object if authenticated', () =>
expect(helpers.updateEmail()).to.eventually.become(undefined)
)

// TODO: test that updateProfile is called if updateInProfile is true
it.skip('calls update profile if updateInProfile is true', () =>
expect(helpers.updateEmail({}, true)).to.eventually.become(undefined)
)
})

describe('storage', () => {
try {
helpers.storage()
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/utils/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ describe('Utils: Auth', () => {
expect(createAuthProvider(firebase, 'google', 'email'))
.to.be.a.function
})
it('handles customAuthParameters config option', () => {
firebase._.config.customAuthParameters = { google: [{prompt: 'select_account'}] }
expect(createAuthProvider(firebase, 'google', 'email'))
.to.be.a.function
})
it('throws for invalid provider', () => {
const provider = 'asdf'
expect(() => createAuthProvider(firebase, provider, ['email']))
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/utils/populate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getPopulates,
getPopulateChild,
getPopulateObjs,
getChildType,
promisesForPopulate
} from '../../../src/utils/populate'

Expand All @@ -18,7 +19,22 @@ describe('Utils: Populate', () => {
})
})

describe('getPopulateObj', () => {
describe('getChildType', () => {
it('returns "string" for strings', () => {
expect(getChildType('some:value')).to.equal('string')
})
it('returns "object" for objects', () => {
expect(getChildType({ some: 'val' })).to.equal('object')
})
it('returns "array" for arrays', () => {
expect(getChildType([])).to.equal('array')
})
it('returns "other" for other types', () => {
expect(getChildType(1)).to.equal('other')
})
})

describe('getPopulateObjs', () => {
it('returns object with child and root', () => {
expect(getPopulateObjs(['some:value'])[0]).to.have.keys('child', 'root')
})
Expand Down