Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Completely deny array as parameter, add more cov test
  • Loading branch information
illuminist committed Apr 18, 2019
commit 47deac9bd11ee535f43e9ddff3650d8d7fa3fd58
9 changes: 2 additions & 7 deletions src/useFirebaseConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import useFirebase from './useFirebase'
* // create firebase connect that uses another redux store
* const useFirebaseConnect = createUseFirebaseConnect()
*/
export const createUseFirebaseConnect = () => (dataOrFn = []) => {
export const createUseFirebaseConnect = () => dataOrFn => {
const firebase = useFirebase()

const inputAsFunc = createCallable(dataOrFn)
Expand All @@ -31,12 +31,7 @@ export const createUseFirebaseConnect = () => (dataOrFn = []) => {
return null
}
if (isArray(data)) {
if (data.length > 1) {
throw new Error(
"Array of multiple paths isn't allowed inside useFirebaseConnect hook."
)
}
return getEventsFromInput(data)
throw new Error("Array isn't allowed inside useFirebaseConnect hook.")
}
return getEventsFromInput([data])
},
Expand Down
9 changes: 2 additions & 7 deletions src/useFirestoreConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import useFirestore from './useFirestore'
*
* export default useFirestoreConnect()
*/
export const createUseFirestoreConnect = () => (dataOrFn = []) => {
export const createUseFirestoreConnect = () => dataOrFn => {
const firestore = useFirestore()

const inputAsFunc = createCallable(dataOrFn)
Expand All @@ -32,12 +32,7 @@ export const createUseFirestoreConnect = () => (dataOrFn = []) => {
return null
}
if (isArray(data)) {
if (data.length > 1) {
throw new Error(
"Array of multiple query isn't allowed inside useFirestoreConnect hook."
)
}
return data
throw new Error("Array isn't allowed inside useFirestoreConnect hook.")
}
return [data]
},
Expand Down
37 changes: 30 additions & 7 deletions test/unit/useFirebaseConnect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import TestUtils from 'react-dom/test-utils'
import { some, isMatch, filter } from 'lodash'
import { storeWithFirebase, firebaseWithConfig, sleep } from '../utils'
import {
ErrorBoundary,
storeWithFirebase,
firebaseWithConfig,
sleep
} from '../utils'
import useFirebaseConnect, {
createUseFirebaseConnect
} from '../../src/useFirebaseConnect'
Expand All @@ -16,7 +21,11 @@ function TestComponent({ dynamicProps }) {
}
/* eslint-enable react/prop-types */

const createContainer = (additionalWrappedProps, listeners) => {
const createContainer = ({
additionalComponentProps,
listeners,
component = TestComponent
} = {}) => {
const firebase = firebaseWithConfig()
const store = storeWithFirebase()
sinon.spy(store, 'dispatch')
Expand All @@ -25,17 +34,20 @@ const createContainer = (additionalWrappedProps, listeners) => {
state = { test: 'testing', dynamic: 'start' }

render() {
const InnerComponent = component
return (
<ReactReduxFirebaseProvider
dispatch={store.dispatch}
firebase={firebase}
createFirestoreInstance={createFirestoreInstance}
config={{}}>
<TestComponent
dynamicProps={this.state.dynamic}
testProps={this.state.test}
{...additionalWrappedProps}
/>
<ErrorBoundary>
<InnerComponent
dynamicProps={this.state.dynamic}
testProps={this.state.test}
{...additionalComponentProps}
/>
</ErrorBoundary>
</ReactReduxFirebaseProvider>
)
}
Expand Down Expand Up @@ -131,6 +143,17 @@ describe('useFirebaseConnect', () => {
)
).to.have.lengthOf(1)
})

it('should not accept array', async () => {
const useFirebaseConnectSpy = sinon.spy(useFirebaseConnect)
const Component = () => {
useFirebaseConnectSpy(['test'])
return <div />
}
createContainer({ component: Component })
await sleep()
expect(useFirebaseConnectSpy.threw()).to.be.true
})
})

describe('createUseFirebaseConnect', () => {
Expand Down
37 changes: 30 additions & 7 deletions test/unit/useFirestoreConnect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import TestUtils from 'react-dom/test-utils'
import { some, isMatch, filter } from 'lodash'
import { storeWithFirestore, firebaseWithConfig, sleep } from '../utils'
import {
ErrorBoundary,
storeWithFirestore,
firebaseWithConfig,
sleep
} from '../utils'
import useFirestoreConnect, {
createUseFirestoreConnect
} from '../../src/useFirestoreConnect'
Expand All @@ -20,7 +25,11 @@ function TestComponent({ dynamicProps }) {
}
/* eslint-enable react/prop-types */

const createContainer = (additionalWrappedProps, listeners) => {
const createContainer = ({
additionalComponentProps,
listeners,
component = TestComponent
} = {}) => {
const firebase = firebaseWithConfig()
const store = storeWithFirestore()
sinon.spy(store, 'dispatch')
Expand All @@ -29,17 +38,20 @@ const createContainer = (additionalWrappedProps, listeners) => {
state = { test: 'testing', dynamic: '' }

render() {
const InnerComponent = component
return (
<ReactReduxFirebaseProvider
dispatch={store.dispatch}
firebase={firebase}
createFirestoreInstance={createFirestoreInstance}
config={{}}>
<TestComponent
dynamicProps={this.state.dynamic}
testProps={this.state.test}
{...additionalWrappedProps}
/>
<ErrorBoundary>
<InnerComponent
dynamicProps={this.state.dynamic}
testProps={this.state.test}
{...additionalComponentProps}
/>
</ErrorBoundary>
</ReactReduxFirebaseProvider>
)
}
Expand Down Expand Up @@ -139,6 +151,17 @@ describe('firestoreConnect', () => {
)
).to.have.lengthOf(1)
})

it('should not accept array', async () => {
const useFirestoreConnectSpy = sinon.spy(useFirestoreConnect)
const Component = () => {
useFirestoreConnectSpy(['test'])
return <div />
}
createContainer({ component: Component })
await sleep()
expect(useFirestoreConnectSpy.threw()).to.be.true
})
})

describe('createUseFirestoreConnect', () => {
Expand Down
25 changes: 25 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ export class Container extends Component {
}
}

export class ErrorBoundary extends Component {
constructor(props) {
super(props)
this.state = { hasError: false }
}

static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true, error }
}

render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>
}

return this.props.children
}
}

ErrorBoundary.propTypes = {
children: PropTypes.node.isRequired
}

export class ProviderMock extends Component {
getChildContext() {
return { store: this.props.store }
Expand Down