Skip to content

Commit ee98bf9

Browse files
committed
Babel module resolver added to simplify module paths in unit tests. Unit test added for presence.
1 parent 7b75fa4 commit ee98bf9

18 files changed

+151
-87
lines changed

.babelrc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818
],
1919
"env": {
2020
"es": {
21-
"comments": false,
22-
"plugins": [
23-
]
21+
"comments": false
2422
},
2523
"commonjs": {
26-
"comments": false,
27-
"plugins": [
28-
]
24+
"comments": false
2925
},
3026
"test": {
3127
"plugins": [
3228
"transform-runtime",
33-
"transform-async-to-generator"
29+
"transform-async-to-generator",
30+
["module-resolver", {
31+
"root": ["./src"]
32+
}]
3433
]
3534
}
3635
}

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"babel-loader": "^7.1.2",
7272
"babel-plugin-add-module-exports": "^0.2.1",
7373
"babel-plugin-lodash": "^3.2.11",
74+
"babel-plugin-module-resolver": "^3.1.1",
7475
"babel-plugin-transform-async-to-generator": "^6.24.1",
7576
"babel-plugin-transform-class-properties": "^6.24.0",
7677
"babel-plugin-transform-export-extensions": "^6.22.0",

src/actions/auth.js

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { populate } from '../helpers'
44
import {
55
getLoginMethodAndParams,
66
updateProfileOnRTDB,
7-
updateProfileOnFirestore
7+
updateProfileOnFirestore,
8+
setupPresence
89
} from '../utils/auth'
910
import { promisesForPopulate, getPopulateObjs } from '../utils/populate'
1011

@@ -293,68 +294,6 @@ export const createUserProfile = (dispatch, firebase, userData, profile) => {
293294
})
294295
}
295296

296-
/**
297-
* @description Start presence management for a specificed user uid.
298-
* Presence collection contains a list of users that are online currently.
299-
* Sessions collection contains a record of all user sessions.
300-
* This function is called within login functions if enablePresence: true.
301-
* @param {Function} dispatch - Action dispatch function
302-
* @param {Object} firebase - Internal firebase object
303-
* @return {Promise}
304-
* @private
305-
*/
306-
const setupPresence = (dispatch, firebase) => {
307-
// exit if database does not exist on firebase instance
308-
if (!firebase.database || !firebase.database.ServerValue) {
309-
return
310-
}
311-
const ref = firebase.database().ref()
312-
const { config: { presence, sessions }, authUid } = firebase._
313-
const amOnline = ref.child('.info/connected')
314-
const onlineRef = ref
315-
.child(
316-
isFunction(presence)
317-
? presence(firebase.auth().currentUser, firebase)
318-
: presence
319-
)
320-
.child(authUid)
321-
let sessionsRef = isFunction(sessions)
322-
? sessions(firebase.auth().currentUser, firebase)
323-
: sessions
324-
if (sessionsRef) {
325-
sessionsRef = ref.child(sessions)
326-
}
327-
amOnline.on('value', snapShot => {
328-
if (!snapShot.val()) return
329-
// user is online
330-
if (sessionsRef) {
331-
// add session and set disconnect
332-
dispatch({ type: actionTypes.SESSION_START, payload: authUid })
333-
// add new session to sessions collection
334-
const session = sessionsRef.push({
335-
startedAt: firebase.database.ServerValue.TIMESTAMP,
336-
user: authUid
337-
})
338-
// Support versions of react-native-firebase that do not have setPriority
339-
// on firebase.database.ThenableReference
340-
if (isFunction(session.setPriority)) {
341-
// set authUid as priority for easy sorting
342-
session.setPriority(authUid)
343-
}
344-
session
345-
.child('endedAt')
346-
.onDisconnect()
347-
.set(firebase.database.ServerValue.TIMESTAMP, () => {
348-
dispatch({ type: actionTypes.SESSION_END })
349-
})
350-
}
351-
// add correct session id to user
352-
// remove from presence list
353-
onlineRef.set(true)
354-
onlineRef.onDisconnect().remove()
355-
})
356-
}
357-
358297
/**
359298
* Auth state change handler. Handles response from firebase's onAuthStateChanged
360299
* @param {Function} dispatch - Action dispatch function

src/utils/auth.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { capitalize, isArray, isString, isFunction } from 'lodash'
2-
import { supportedAuthProviders } from '../constants'
2+
import { supportedAuthProviders, actionTypes } from '../constants'
33

44
/**
55
* @description Get correct login method and params order based on provided credentials
@@ -219,3 +219,65 @@ export const updateProfileOnFirestore = (
219219
: profileRef.update(profileUpdate)
220220
return profileUpdatePromise.then(() => profileRef.get())
221221
}
222+
223+
/**
224+
* @description Start presence management for a specificed user uid.
225+
* Presence collection contains a list of users that are online currently.
226+
* Sessions collection contains a record of all user sessions.
227+
* This function is called within login functions if enablePresence: true.
228+
* @param {Function} dispatch - Action dispatch function
229+
* @param {Object} firebase - Internal firebase object
230+
* @return {Promise}
231+
* @private
232+
*/
233+
export function setupPresence(dispatch, firebase) {
234+
// exit if database does not exist on firebase instance
235+
if (!firebase.database || !firebase.database.ServerValue) {
236+
return
237+
}
238+
const ref = firebase.database().ref()
239+
const { config: { presence, sessions }, authUid } = firebase._
240+
const amOnline = ref.child('.info/connected')
241+
const onlineRef = ref
242+
.child(
243+
isFunction(presence)
244+
? presence(firebase.auth().currentUser, firebase)
245+
: presence
246+
)
247+
.child(authUid)
248+
let sessionsRef = isFunction(sessions)
249+
? sessions(firebase.auth().currentUser, firebase)
250+
: sessions
251+
if (sessionsRef) {
252+
sessionsRef = ref.child(sessions)
253+
}
254+
amOnline.on('value', snapShot => {
255+
if (!snapShot.val()) return
256+
// user is online
257+
if (sessionsRef) {
258+
// add session and set disconnect
259+
dispatch({ type: actionTypes.SESSION_START, payload: authUid })
260+
// add new session to sessions collection
261+
const session = sessionsRef.push({
262+
startedAt: firebase.database.ServerValue.TIMESTAMP,
263+
user: authUid
264+
})
265+
// Support versions of react-native-firebase that do not have setPriority
266+
// on firebase.database.ThenableReference
267+
if (isFunction(session.setPriority)) {
268+
// set authUid as priority for easy sorting
269+
session.setPriority(authUid)
270+
}
271+
session
272+
.child('endedAt')
273+
.onDisconnect()
274+
.set(firebase.database.ServerValue.TIMESTAMP, () => {
275+
dispatch({ type: actionTypes.SESSION_END })
276+
})
277+
}
278+
// add correct session id to user
279+
// remove from presence list
280+
onlineRef.set(true)
281+
onlineRef.onDisconnect().remove()
282+
})
283+
}

test/unit/actions/auth.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import {
1616
resetPassword,
1717
confirmPasswordReset,
1818
verifyPasswordResetCode
19-
} from '../../../src/actions/auth'
19+
} from 'actions/auth'
2020
import { cloneDeep } from 'lodash'
2121
import { actionTypes } from '../../../src/constants'
2222
import {
2323
fakeFirebase,
2424
onAuthStateChangedSpy,
2525
firebaseWithConfig
2626
} from '../../utils'
27-
// import { promisesForPopulate } from '../../../src/utils/populate'
27+
// import { promisesForPopulate } from 'utils/populate'
2828

2929
let functionSpy
3030
let dispatchSpy

test/unit/actions/query.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
watchEvents,
55
unWatchEvents,
66
remove
7-
} from '../../../src/actions/query'
7+
} from 'actions/query'
88
import { actionTypes } from '../../../src/constants'
99
let spy
1010
const dispatch = () => {}

test/unit/actions/storage.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
uploadFile,
44
uploadFiles,
55
deleteFile
6-
} from '../../../src/actions/storage'
6+
} from 'actions/storage'
77
import { fakeFirebase } from '../../utils'
88

99
let spy

test/unit/enhancer.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ describe('enhancer', () => {
214214

215215
describe('uploadFiles', () => {
216216
it('calls uploadFiles Firebase method', () => {
217-
expect(store.firebase.uploadFiles('[email protected]')).to.be.rejected
217+
expect(store.firebase.uploadFiles('test', [{ name: 'file1' }])).to.be
218+
.rejected
218219
})
219220
})
220221

test/unit/firestoreConnect.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import {
77
ProviderMock,
88
TestContainer
99
} from '../utils'
10-
import firestoreConnect, {
11-
createFirestoreConnect
12-
} from '../../src/firestoreConnect'
10+
import firestoreConnect, { createFirestoreConnect } from 'firestoreConnect'
1311

1412
const createContainer = () => {
1513
const store = storeWithFirestore()

0 commit comments

Comments
 (0)