Skip to content
Prev Previous commit
Next Next commit
dispatchOnUnsetListener spelling fixed in method and docs. Depreciati…
…on warning added for distpatchOnUnsetListener including a matching test.
  • Loading branch information
Scott Prue committed May 22, 2017
commit c4fa410e4604b38279dc545c42b6fbd7d7337776
6 changes: 3 additions & 3 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ export const updateProfile = (dispatch, firebase, profileUpdate) => {
return database()
.ref(`${config.userProfile}/${authUid}`)
.update(profileUpdate)
.then((payload) => {
.then((snap) => {
dispatch({
type: actionTypes.PROFILE_UPDATE_SUCCESS,
payload
payload: snap.val()
})
})
.catch((payload) => {
Expand Down Expand Up @@ -558,7 +558,7 @@ export const updateAuth = (dispatch, firebase, authUpdate, updateInProfile) => {
.then((payload) => {
dispatch({
type: actionTypes.AUTH_UPDATE_SUCCESS,
payload: authUpdate
payload: firebase.auth().currentUser
})
if (updateInProfile) {
return updateProfile(dispatch, firebase, authUpdate)
Expand Down
4 changes: 2 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const actionTypes = {
* the data path. For example: role paramter on profile populated from 'roles'
* root. True will call SET_PROFILE as well as a SET action with the role that
* is loaded (places it in data/roles).
* @property {Boolean} distpatchOnUnsetListener - `false` Whether or not to
* @property {Boolean} dispatchOnUnsetListener - `false` Whether or not to
* dispatch UNSET_LISTENER when disabling listeners for a specific path. USE WITH CAUTION
* Setting this to true allows an action to be called that removes data
* from redux (which might not always be expected).
Expand All @@ -105,7 +105,7 @@ export const defaultConfig = {
enableRedirectHandling: true,
autoPopulateProfile: true,
setProfilePopulateResults: false,
distpatchOnUnsetListener: false
dispatchOnUnsetListener: false
}

/** @constant
Expand Down
2 changes: 1 addition & 1 deletion src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default (state = initialState, action = {}) => {
return state.setIn(['authError'], action.authError)

case AUTH_UPDATE_SUCCESS:
return state.updateIn(['auth'], action.payload)
return state.setIn(['auth'], fromJS(action.payload))

default:
return state
Expand Down
16 changes: 11 additions & 5 deletions src/utils/query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { actionTypes } from '../constants'
import { isFunction } from 'lodash'

const { UNSET_LISTENER } = actionTypes

Expand Down Expand Up @@ -95,16 +96,21 @@ export const getWatcherCount = (firebase, event, path, queryId = undefined) => {
export const unsetWatcher = (firebase, dispatch, event, path, queryId = undefined) => {
let id = queryId || getQueryIdFromPath(path, event) || getWatchPath(event, path)
path = path.split('#')[0]
if (firebase._.watchers[id] <= 1) {
delete firebase._.watchers[id]
const { watchers, config } = firebase._
if (watchers[id] <= 1) {
delete watchers[id]
if (event !== 'first_child' && event !== 'once') {
firebase.database().ref().child(path).off(event)
if (firebase._.config.distpatchOnUnsetListener) {
// TODO: Remove config.distpatchOnUnsetListener
if (config.dispatchOnUnsetListener || config.distpatchOnUnsetListener) {
if (config.distpatchOnUnsetListener && isFunction(console.warn)) { // eslint-disable-line no-console
console.warn('config.distpatchOnUnsetListener is Depreceated and will be removed in future versions. Please use config.dispatchOnUnsetListener (dispatch spelled correctly).') // eslint-disable-line no-console
}
dispatch({ type: UNSET_LISTENER, path })
}
}
} else if (firebase._.watchers[id]) {
firebase._.watchers[id]--
} else if (watchers[id]) {
watchers[id]--
}
}

Expand Down
70 changes: 69 additions & 1 deletion tests/unit/reducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,58 @@ describe('reducer', () => {
)
).to.equal(exampleState)
})

it('sets state', () => {
const path = 'test'
const pathArray = path.split(/\//).filter(p => !!p)
console.log('path:', { type: actionTypes.SET, path, data: {} })
expect(
JSON.stringify(firebaseStateReducer(
exampleState,
{ type: actionTypes.SET, path, data: {} }
))
).to.equal(JSON.stringify(exampleState.setIn(['data', ...pathArray], fromJS({}))))
})

it('handles already existing parent that is null', () => {
const childPath = 123
const path = `test/${childPath}`
const pathArray = path.split(/\//).filter(p => !!p)
const newData = { some: 'val' }
console.log('----- pathArray:', pathArray)
expect(
JSON.stringify(firebaseStateReducer(
fromJS({ data: { test: null } }),
{ type: actionTypes.SET, path, data: newData }
).toJS())
).to.equal(
JSON.stringify(
exampleState.setIn(
['data', ...pathArray],
fromJS(newData)
).toJS()
)
)
})

it('handles already existing value of null', () => {
const path = 'test/123'
const pathArray = path.split(/\//).filter(p => !!p)
const newData = { some: 'val' }
expect(
JSON.stringify(firebaseStateReducer(
fromJS({ data: { test: { '123': null } } }),
{ type: actionTypes.SET, path, data: newData }
))
).to.equal(
JSON.stringify(
exampleState.setIn(
['data', ...pathArray],
fromJS(newData)
).toJS()
)
)
})

})

describe('NO_VALUE action', () => {
Expand All @@ -76,6 +117,17 @@ describe('reducer', () => {
})
})

describe('UNSET_LISTENER action', () => {
it('sets state', () => {
expect(
JSON.stringify(firebaseStateReducer(
exampleState,
{ type: actionTypes.UNSET_LISTENER, path: 'asdfasdf' }
).toJS())
).to.equal(JSON.stringify({}))
})
})

describe('SET_PROFILE action', () => {
it('sets state', () => {
const profile = { email: '[email protected]' }
Expand Down Expand Up @@ -183,4 +235,20 @@ describe('reducer', () => {
)
})
})

describe('AUTH_UPDATE_SUCCESS action', () => {
it('sets state', () => {
const authUpdate = { email: 'newEmail' }
expect(
JSON.stringify(firebaseStateReducer(
exampleState,
{ type: actionTypes.AUTH_UPDATE_SUCCESS, payload: authUpdate }
).toJS())
).to.equal(
JSON.stringify(
exampleState.setIn(['auth'], authUpdate).toJS()
)
)
})
})
})
30 changes: 30 additions & 0 deletions tests/unit/utils/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,30 @@ describe('Utils: Query', () => {
'value:/todos': 1,
'value:/todo': 2
}
spy = sinon.spy(console, 'warn')

})
afterEach(() => {
console.warn.restore()
})
it('removes single watcher', () => {
unsetWatcher(Firebase, dispatch, 'value', '/todos')
expect(Firebase._.watchers['value:/todos']).to.be.undefined
})
it('handes dispatch on unset listener config', () => {
Firebase._.config.dispatchOnUnsetListener = true
unsetWatcher(Firebase, dispatch, 'value', '/todos')
expect(Firebase._.watchers['value:/todos']).to.be.undefined
})

it('warns for deprecated method name', () => {
Firebase._.config.distpatchOnUnsetListener = true
// TODO: confirm that console.warn is called with correct message
unsetWatcher(Firebase, dispatch, 'value', '/todos')
expect(Firebase._.watchers['value:/todos']).to.be.undefined
expect(spy).to.have.been.calledWith('config.distpatchOnUnsetListener is Depreceated and will be removed in future versions. Please use config.dispatchOnUnsetListener (dispatch spelled correctly).')
})

it('decrements existings watcher count', () => {
unsetWatcher(Firebase, dispatch, 'value', '/todo')
expect(Firebase._.watchers['value:/todos']).to.equal(1)
Expand Down Expand Up @@ -198,6 +217,17 @@ describe('Utils: Query', () => {
.to
.equal(equalTo)
})
it('does not parse if notParsed parameter passed', () => {
const child = 'emailAddress'
const equalTo = '123'
const queryParams = createQueryFromParams([`orderByChild=${child}`, 'notParsed', `equalTo=${equalTo}`])
expect(queryParams.child)
.to
.equal(child)
expect(queryParams.equalTo)
.to
.equal(equalTo)
})
})

})
Expand Down