22
33import 'jsdom-global/register'
44import React , { Component } from 'react'
5+ import firebase from 'firebase/app'
56import test from 'tape'
67import { findDOMNode , unmountComponentAtNode } from 'react-dom'
78import { findRenderedComponentWithType , renderIntoDocument } from 'react-addons-test-utils'
8-
99import connect from '../connect'
10+ import { createMockApp , createMockSnapshot } from './helpers'
1011
11- class Passthrough extends Component { // eslint-disable-line react/prefer-stateless-function
12- render ( ) {
13- return < div />
12+ const renderStub = ( mapFirebaseToProps , firebaseApp , props ) => {
13+ class Passthrough extends Component { // eslint-disable-line react/prefer-stateless-function
14+ render ( ) {
15+ return < div />
16+ }
1417 }
15- }
1618
17- const createMockSnapshot = ( val , ...otherProps ) => ( {
18- ...otherProps ,
19- val : ( ) => val ,
20- } )
21-
22- const defaultDatabaseProps = {
23- ref : path => ( {
24- on : ( event , callback ) => (
25- callback ( createMockSnapshot ( `${ path } value` ) )
26- ) ,
27- } ) ,
28- }
29-
30- const createMockApp = ( dataBaseProps = defaultDatabaseProps , ...otherProps ) => ( {
31- ...otherProps ,
32- database : ( ) => dataBaseProps ,
33- } )
34-
35- const renderStub = ( mapFirebaseToProps , firebaseApp , props ) => {
3619 const WrappedComponent = connect ( mapFirebaseToProps ) ( Passthrough )
3720 const container = renderIntoDocument ( < WrappedComponent { ...props } firebaseApp = { firebaseApp } /> )
3821 const stub = findRenderedComponentWithType ( container , Passthrough )
@@ -44,15 +27,22 @@ const renderStub = (mapFirebaseToProps, firebaseApp, props) => {
4427 }
4528}
4629
47- test ( 'Should throw if no Firebase app instance was found either globally, in props or context ' , assert => {
30+ test ( 'Should throw if no initialized Firebase app instance was found' , assert => {
4831 const errorPattern = / N o F i r e b a s e A p p /
4932
50- assert . throws ( ( ) => {
33+ // Default app instance
34+ assert . doesNotThrow ( ( ) => {
35+ const defaultApp = firebase . initializeApp ( { } )
5136 const WrappedComponent = connect ( ) ( 'div' )
37+ const container = renderIntoDocument ( < WrappedComponent /> )
38+ const stub = findRenderedComponentWithType ( container , WrappedComponent )
39+
40+ assert . equal ( stub . firebaseApp , defaultApp )
5241
53- renderIntoDocument ( < WrappedComponent /> )
42+ defaultApp . delete ( )
5443 } , errorPattern )
5544
45+ // Props
5646 assert . doesNotThrow ( ( ) => {
5747 const firebaseApp = createMockApp ( )
5848 const WrappedComponent = connect ( ) ( props => {
@@ -169,10 +159,10 @@ test('Should unsubscribe when component unmounts', assert => {
169159 assert . end ( )
170160} )
171161
172- test ( 'Should pass props, ref and firebase to mapFirebaseToProps' , assert => {
173- const mapFirebaseToProps = ( props , ref , firebase ) => {
162+ test ( 'Should pass props, ref and firebaseApp to mapFirebaseToProps' , assert => {
163+ const mapFirebaseToProps = ( props , ref , firebaseApp ) => {
174164 assert . deepEqual ( props . foo , 'foo prop' )
175- assert . equal ( typeof firebase . database , 'function' )
165+ assert . equal ( typeof firebaseApp . database , 'function' )
176166 assert . equal ( typeof ref , 'function' )
177167
178168 return { foo : 'foo' }
0 commit comments