From bb36408a73d57f6ee29b64c8c35bf1d0242d93fb Mon Sep 17 00:00:00 2001 From: prescottprue Date: Tue, 6 Dec 2016 03:21:31 -0800 Subject: [PATCH] Config validation was moved to util. --- src/compose.js | 18 +++++++----------- src/utils/index.js | 36 +++++++++++++++++++++++++++++------- test/unit/compose.spec.js | 7 ++++--- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/compose.js b/src/compose.js index 867c48e52..3f1d0c305 100644 --- a/src/compose.js +++ b/src/compose.js @@ -1,5 +1,6 @@ import Firebase from 'firebase' import { defaultConfig } from './constants' +import { validateConfig } from './utils' import { authActions, queryActions, storageActions } from './actions' let firebaseInstance @@ -43,25 +44,20 @@ let firebaseInstance * // Use Function later to create store * const store = createStoreWithFirebase(rootReducer, initialState) */ -export default (config, otherConfig) => next => +export default (fbConfig, otherConfig) => next => (reducer, initialState, middleware) => { const store = next(reducer, initialState, middleware) const { dispatch } = store - const { apiKey, authDomain, databaseURL } = config - - // Throw for missing Firebase Data - if (!databaseURL) throw new Error('Firebase databaseURL is required') - if (!authDomain) throw new Error('Firebase authDomain is required') - if (!apiKey) throw new Error('Firebase apiKey is required') - // Combine all configs - const configs = Object.assign({}, defaultConfig, config, otherConfig) + const configs = Object.assign({}, defaultConfig, fbConfig, otherConfig) + + validateConfig(configs) // Initialize Firebase try { - Firebase.initializeApp(config) - } catch (err) {} + Firebase.initializeApp(fbConfig) + } catch (err) {} // silence reinitialize warning (hot-reloading) // Enable Logging based on config if (configs.enableLogging) { diff --git a/src/utils/index.js b/src/utils/index.js index eb0c1c3d0..95760febd 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,17 +1,39 @@ import { isFunction } from 'lodash' -import { getEventsFromInput } from './events' +export { getEventsFromInput } from './events' /** + * @private * @description Create a function if not already one * @param {Function|Object|Array|String} Callable function or value of return for new function */ export const createCallable = f => isFunction(f) ? f : () => f -export { - getEventsFromInput -} +/** + * @private + * @description Validate config input + * @param {Object} Config object containing all combined configs + */ +export const validateConfig = (config) => { + // require needed Firebase config + const requiredProps = [ + 'databaseURL', + 'authDomain', + 'apiKey' + ] + requiredProps.forEach((p) => { + if (!config[p]) { + throw new Error(`${p} is a required config parameter for react-redux-firebase.`) + } + }) -export default { - createCallable, - getEventsFromInput + // Check that some certain config are functions if they exist + const functionProps = [ + 'fileMetadataFactory', + 'profileDecorator' + ] + functionProps.forEach((p) => { + if (!!config[p] && !isFunction(config[p])) { + throw new Error(`${p} parameter in react-redux-firebase config must be a function. check your compose function.`) + } + }) } diff --git a/test/unit/compose.spec.js b/test/unit/compose.spec.js index 736156386..516699a7c 100644 --- a/test/unit/compose.spec.js +++ b/test/unit/compose.spec.js @@ -104,17 +104,18 @@ describe('Compose', () => { }) describe('throws for missing fbConfig parameters', () => { + const errorSuffix = 'is a required config parameter for react-redux-firebase.' it('databaseURL', () => { expect(() => generateCreateStore('databaseURL')(reducer)) - .to.throw('Firebase databaseURL is required') + .to.throw(`databaseURL ${errorSuffix}`) }) it('authDomain', () => { expect(() => generateCreateStore('authDomain')(reducer)) - .to.throw('Firebase authDomain is required') + .to.throw(`authDomain ${errorSuffix}`) }) it('apiKey', () => { expect(() => generateCreateStore('apiKey')(reducer)) - .to.throw('Firebase apiKey is required') + .to.throw(`apiKey ${errorSuffix}`) }) })