Skip to content

Commit a804e2d

Browse files
simenbrekkenEinar Löve
authored andcommitted
Improve test coverage, closes #15 (#17)
* Improve test coverage, closes #15 * Fix invalid argument name in description * Clarify test description
1 parent c8d6d4b commit a804e2d

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

src/tests/Provider-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
import 'jsdom-global/register'
3+
import React, { Component } from 'react'
4+
import test from 'tape'
5+
import { findRenderedComponentWithType, renderIntoDocument } from 'react-addons-test-utils'
6+
import connect from '../connect'
7+
import Provider from '../Provider'
8+
import { createMockApp } from './helpers'
9+
10+
test('Should use firebaseApp from context if provided', assert => {
11+
class Passthrough extends Component { // eslint-disable-line react/prefer-stateless-function
12+
render() {
13+
return <div />
14+
}
15+
}
16+
17+
const firebaseApp = createMockApp()
18+
const WrappedComponent = connect()(Passthrough)
19+
const container = renderIntoDocument(
20+
<Provider firebaseApp={firebaseApp}>
21+
<WrappedComponent />
22+
</Provider>
23+
)
24+
25+
const stub = findRenderedComponentWithType(container, WrappedComponent)
26+
27+
assert.equal(stub.firebaseApp, firebaseApp)
28+
assert.end()
29+
})

src/tests/connect-test.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,20 @@
22

33
import 'jsdom-global/register'
44
import React, { Component } from 'react'
5+
import firebase from 'firebase/app'
56
import test from 'tape'
67
import { findDOMNode, unmountComponentAtNode } from 'react-dom'
78
import { findRenderedComponentWithType, renderIntoDocument } from 'react-addons-test-utils'
8-
99
import 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 = /No Firebase App/
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' }

src/tests/helpers.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const createMockSnapshot = (val, ...otherProps) => ({
2+
...otherProps,
3+
val: () => val,
4+
})
5+
6+
const defaultDatabaseProps = {
7+
ref: path => ({
8+
on: (event, callback) => (
9+
callback(createMockSnapshot(`${path} value`))
10+
),
11+
}),
12+
}
13+
14+
export const createMockApp = (dataBaseProps = defaultDatabaseProps, ...otherProps) => ({
15+
...otherProps,
16+
database: () => dataBaseProps,
17+
})

0 commit comments

Comments
 (0)