8000 Tests added for PROFILE_UPDATE_SUCCESS action and preserve parameter.… · srsedev/react-redux-firebase@d25c026 · GitHub
[go: up one dir, main page]

Skip to content

Commit d25c026

Browse files
committed
Tests added for PROFILE_UPDATE_SUCCESS action and preserve parameter. Lint removed
1 parent bc74f57 commit d25c026

File tree

3 files changed

+148
-7
lines changed

3 files changed

+148
-7
lines changed

src/actions/auth.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const dispatchLoginError = (dispatch, authError) =>
2929
export const unWatchUserProfile = firebase => {
3030
const {
3131
authUid,
32-
config: {userProfile, useFirestoreForProfile}
32+
config: { userProfile, useFirestoreForProfile }
3333
} = firebase._
3434
if (firebase._.profileWatch) {
3535
if (useFirestoreForProfile && firebase.firestore) {
@@ -212,8 +212,12 @@ export const createUserProfile = (dispatch, firebase, userData, profile) => {
212212
.doc(userData.uid)
213213
.get()
214214
.then(profileSnap => {
215-
let newProfile = {}
215+
// Return if config for updating profile is not enabled and profile exists
216+
if (!config.updateProfileOnLogin && profileSnap.exists) {
217+
return profileSnap.data()
218+
}
216219

220+
let newProfile = {}
217221
// If the user did supply a profileFactory, we should use the result of it for the new Profile
218222
if (isFunction(config.profileFactory)) {
219223
newProfile = profile
@@ -229,10 +233,6 @@ export const createUserProfile = (dispatch, firebase, userData, profile) => {
229233
}
230234
}
231235

232-
// Return if config for updating profile is not enabled and profile exists
233-
if (!config.updateProfileOnLogin && profileSnap.exists) {
234-
return profileSnap.data()
235-
}
236236
// Create/Update the profile
237237
return profileSnap.ref
238238
.set(newProfile, { merge: true })

test/unit/helpers.spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ describe('Helpers:', () => {
269269
exampleData.data[rootName].ABC.displayName
270270
)
271271
})
272+
273+
it('supports childParam', () => {
274+
populates = [
275+
{
276+
child: 'collaborators',
277+
root: rootName,
278+
childParam: 'displayName'
279+
}
280+
]
281+
// Non matching collaborator
282+
expect(
283+
helpers.populate(exampleData, path, populates)
284+
).to.have.deep.property(`${valName}.collaborators.abc`, true)
285+
// Matching collaborator
286+
expect(
287+
helpers.populate(exampleData, path, populates)
288+
).to.have.deep.property(
289+
`${valName}.collaborators.ABC`,
290+
exampleData.data[rootName].ABC.displayName
291+
)
292+
})
272293
})
273294

274295
describe('config as function', () => {
@@ -354,7 +375,6 @@ describe('Helpers:', () => {
354375
)
355376
})
356377

357-
// Skipped since this is not currently supported
358378
it('from same root', () => {
359379
populates = [
360380
{ child: 'owner', root: rootName },
@@ -408,4 +428,19 @@ describe('Helpers:', () => {
408428
expect(helpers.isEmpty([{}])).to.be.false
409429
})
410430
})
431+
432+
describe('fixPath', () => {
433+
it('exists', () => {
434+
expect(helpers).to.respondTo('fixPath')
435+
})
436+
437+
it('returns original path if no fix is required', () => {
438+
const originalPath = '/asdf'
439+
expect(helpers.fixPath(originalPath)).to.equal(originalPath)
440+
})
441+
442+
it('adds / to beginning of path', () => {
443+
expect(helpers.fixPath('asdf')).to.equal('/asdf')
444+
})
445+
})
411446
})

test/unit/reducer.spec.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ describe('reducer', () => {
527527
})
528528
})
529529

530+
describe('PROFILE_UPDATE_SUCCESS action -', () => {
531+
it('sets auth state to isLoaded: true, isEmpty: false', () => {
532+
const payload = { some: 'value' }
533+
action = { type: actionTypes.PROFILE_UPDATE_SUCCESS, payload }
534+
const currentState = firebaseReducer({}, action)
535+
expect(currentState).to.have.deep.property('profile.some', payload.some)
536+
})
537+
})
538+
530539
describe('LOGIN action -', () => {
531540
it('sets auth state to isLoaded: true, isEmpty: false', () => {
532541
const auth = { some: 'value' }
@@ -552,6 +561,103 @@ describe('reducer', () => {
552561
true
553562
)
554563
})
564+
565+
describe('preserve parameter', () => {
566+
describe('profile state is preserved when provided', () => {
567+
it('an array of keys', () => {
568+
const payload = { some: 'other' }
569+
const initialState = { profile: { some: 'value' } }
570+
action = {
571+
type: actionTypes.LOGIN,
572+
preserve: { profile: ['some'] },
573+
payload
574+
}
575+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
576+
'profile.some',
577+
'value'
578+
)
579+
})
580+
581+
it('a boolean (true)', () => {
582+
const payload = { some: 'other' }
583+
const initialState = { profile: { some: 'value' } }
584+
action = {
585+
type: actionTypes.LOGIN,
586+
preserve: { profile: true },
587+
payload
588+
}
589+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
590+
'profile.some',
591+
'value'
592+
)
593+
})
594+
})
595+
596+
describe('profile state is updated when is already exists and is provided', () => {
597+
it('a boolean (true)', () => {
598+
const payload = { some: 'other' }
599+
const initialState = { profile: { some: 'value' } }
600+
action = {
601+
type: actionTypes.LOGIN,
602+
preserve: { profile: true },
603+
payload
604+
}
605+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
606+
'profile.some',
607+
'value'
608+
)
609+
})
610+
})
611+
612+
describe('auth state is preserved when provided', () => {
613+
it('an array of keys', () => {
614+
const payload = { some: 'other' }
615+
const initialState = { auth: { some: 'value' } }
616+
action = {
617+
type: actionTypes.LOGIN,
618+
preserve: { auth: ['some'] },
619+
auth: payload
620+
}
621+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
622+
'auth.some',
623+
'value'
624+
)
625+
})
626+
627+
it('a boolean (true)', () => {
628+
const initialState = { auth: { some: 'value' } }
629+
action = {
630+
type: actionTypes.LOGIN,
631+
preserve: { auth: true },
632+
auth: {}
633+
}
634+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
635+
'auth.some',
636+
'value'
637+
)
638+
})
639+
})
640+
641+
describe('auth state is updated when is already exists and is provided', () => {
642+
it('a boolean (true)', () => {
643+
const payload = { some: 'other', another: 'thing' }
644+
const initialState = { auth: { some: 'value' } }
645+
action = {
646+
type: actionTypes.LOGIN,
647+
preserve: { auth: true },
648+
auth: payload
649+
}
650+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
651+
'auth.some',
652+
payload.some
653+
)
654+
expect(firebaseReducer(initialState, action)).to.have.deep.property(
655+
'auth.another',
656+
payload.another
657+
)
658+
})
659+
})
660+
})
555661
})
556662

557663
describe('REMOVE action -', () => {

0 commit comments

Comments
 (0)
0