2
2
3
3
import 'jsdom-global/register'
4
4
import React , { Component } from 'react'
5
+ import firebase from 'firebase/app'
5
6
import test from 'tape'
6
7
import { findDOMNode , unmountComponentAtNode } from 'react-dom'
7
8
import { findRenderedComponentWithType , renderIntoDocument } from 'react-addons-test-utils'
8
-
9
9
import connect from '../connect'
10
+ import { createMockApp , createMockSnapshot } from './helpers'
10
11
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
+ }
14
17
}
15
- }
16
18
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 ) => {
36
19
const WrappedComponent = connect ( mapFirebaseToProps ) ( Passthrough )
37
20
const container = renderIntoDocument ( < WrappedComponent { ...props } firebaseApp = { firebaseApp } /> )
38
21
const stub = findRenderedComponentWithType ( container , Passthrough )
@@ -44,15 +27,22 @@ const renderStub = (mapFirebaseToProps, firebaseApp, props) => {
44
27
}
45
28
}
46
29
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 => {
48
31
const errorPattern = / N o F i r e b a s e A p p /
49
32
50
- assert . throws ( ( ) => {
33
+ // Default app instance
34
+ assert . doesNotThrow ( ( ) => {
35
+ const defaultApp = firebase . initializeApp ( { } )
51
36
const WrappedComponent = connect ( ) ( 'div' )
37
+ const container = renderIntoDocument ( < WrappedComponent /> )
38
+ const stub = findRenderedComponentWithType ( container , WrappedComponent )
39
+
40
+ assert . equal ( stub . firebaseApp , defaultApp )
52
41
53
- renderIntoDocument ( < WrappedComponent /> )
42
+ defaultApp . delete ( )
54
43
} , errorPattern )
55
44
45
+ // Props
56
46
assert . doesNotThrow ( ( ) => {
57
47
const firebaseApp = createMockApp ( )
58
48
const WrappedComponent = connect ( ) ( props => {
@@ -169,10 +159,10 @@ test('Should unsubscribe when component unmounts', assert => {
169
159
assert . end ( )
170
160
} )
171
161
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 ) => {
174
164
assert . deepEqual ( props . foo , 'foo prop' )
175
- assert . equal ( typeof firebase . database , 'function' )
165
+ assert . equal ( typeof firebaseApp . database , 'function' )
176
166
assert . equal ( typeof ref , 'function' )
177
167
178
168
return { foo : 'foo' }
0 commit comments