-
Notifications
You must be signed in to change notification settings - Fork 93
Open
Description
Hi. While trying to set up firebase-mock in my project, I fail to 'connect the dots'. I have the following setup.
firebaseSetup.js
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'
if (!firebase.apps.length) {
var config = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "..."
}
firebase.initializeApp(config)
}
const firebaseAuth = firebase.auth()
const firestore = firebase.firestore()
firestore.settings({timestampsInSnapshots: true})
export default firebase
export {
firebaseAuth,
firestore
}
AppBody.js
import React from 'react'
import PropTypes from 'prop-types'
import firebase, {firestore} from './firebaseSetup'
const propTypes = {
userID: PropTypes.string
}
class AppBody extends React.Component {
state = {
data: undefined
}
componentWillMount = () => {
const userRef = firestore.collection('users').doc(this.props.userID)
this.dbData = userRef.collection('data')
this.dbDataUnsubscribe = this.dbData.onSnapshot(data => this.setState({data}))
}
componentWillUnmount = () => {
this.dbDataUnsubscribe()
}
render() {...}
}
Now, the tricky part - AppBody.test.js
import React from 'react'
import proxyquire from 'proxyquire'
import {render} from 'react-testing-library'
const AppBody = proxyquire('./AppBody.js', {
firebase: mocksdk
})
jest.mock('./firebaseSetup.js', () => {
const firebasemock = require('firebase-mock')
const mockauth = new firebasemock.MockAuthentication()
const mockfirestore = new firebasemock.MockFirestore()
const mocksdk = new firebasemock.MockFirebaseSdk(null, () => mockauth, () => mockfirestore)
const firebase = mocksdk.initializeApp()
const firestore = firebase.firestore()
const firebaseAuth = firebase.auth()
return firebase, {firebaseAuth, firestore}
})
describe('AppBody', () => {
it('renders', () => {
const {getByText} = render(
<AppBody
userID={'aaa'}
/>
)
expect(getByText('Today')).toBeInTheDocument()
})
})
Now, obviously, my test file is incorrect. For one, mocksdk is undefined in line 6. So, my question is, how do I setup firebase-mock correctly so that my AppBody component at least renders and retrieves initial data?
Thank you very much in advance!
balazsorban44, maxvektor, froddd, cdock1029, GabLeRoux and 4 more
Metadata
Metadata
Assignees
Labels
No labels