Skip to content

Commit b386490

Browse files
committed
Merge pull request #3 from unfold/tests
Add tests
2 parents ca11501 + f9acee7 commit b386490

File tree

4 files changed

+128
-4
lines changed

4 files changed

+128
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
coverage/
2+
node_modules/

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@
3939
"eslint-plugin-react": "4.2.3",
4040
"express": "4.13.4",
4141
"express-urlrewrite": "1.2.0",
42+
"isparta": "4.0.0",
43+
"jsdom": "8.1.0",
44+
"jsdom-global": "1.7.0",
45+
"react-addons-test-utils": "0.14.7",
4246
"react-dom": "0.14.7",
47+
"tape": "4.5.1",
4348
"webpack": "1.12.14",
4449
"webpack-dev-middleware": "1.5.1"
4550
},

src/components/connect.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default (mapPropsToSubscriptions, mapFirebaseToProps, mergeProps, options
2121
const mapFirebase = mapFirebaseToProps || defaultMapFirebaseToProps;
2222

2323
const finalMergeProps = mergeProps || defaultMergeProps;
24-
const { pure = true, keepalive = 2500 } = options;
24+
const { pure = true, keepalive = 0 } = options;
2525

2626
const computeSubscriptions = props => {
2727
const subscriptions = mapSubscriptions(props);
@@ -142,9 +142,13 @@ export default (mapPropsToSubscriptions, mapFirebaseToProps, mergeProps, options
142142
const subscriptionKeys = keys(subscriptions);
143143

144144
this.listeners = reduce(subscriptionKeys, (listeners, key) => {
145-
const listener = listeners[key];
145+
const unsubscribeKey = () => listeners[key].unsubscribe();
146146

147-
setTimeout(() => listener.unsubscribe(), keepalive);
147+
if (keepalive) {
148+
setTimeout(unsubscribeKey, keepalive);
149+
} else {
150+
unsubscribeKey();
151+
}
148152

149153
return omit(listeners, key);
150154
}, this.listeners);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import 'jsdom-global/register';
2+
import React from 'react';
3+
import test from 'tape';
4+
import { findDOMNode, unmountComponentAtNode } from 'react-dom';
5+
import { renderIntoDocument } from 'react-addons-test-utils';
6+
7+
import connect from '../connect';
8+
9+
test('Should throw if no firebase instance was found in either props or context', assert => {
10+
assert.throws(() => {
11+
const WrappedComponent = connect()('div');
12+
13+
renderIntoDocument(<WrappedComponent />);
14+
}, /Could not find "firebase"/);
15+
16+
assert.end();
17+
});
18+
19+
test('Should subscribe to a single path', assert => {
20+
const mockFirebase = {
21+
child: path => {
22+
assert.equal(path, 'foo');
23+
24+
return mockFirebase;
25+
},
26+
on: (event, callback) => {
27+
assert.equal(event, 'value');
28+
29+
const mockSnapshot = {
30+
val: () => 'foo changed',
31+
};
32+
33+
callback(mockSnapshot);
34+
},
35+
};
36+
37+
const mapPropsToSubscriptions = () => ({ foo: 'foo' });
38+
const WrappedComponent = connect(mapPropsToSubscriptions)('div');
39+
const container = renderIntoDocument(<WrappedComponent firebase={mockFirebase} />);
40+
41+
assert.deepEqual(container.state.subscriptionsState, { foo: 'foo changed' });
42+
assert.end();
43+
});
44+
45+
test('Should subscribe to a query', assert => {
46+
const mockFirebase = {
47+
child: () => {
48+
assert.fail();
49+
50+
return mockFirebase;
51+
},
52+
startAt: priority => {
53+
assert.equal(priority, 1);
54+
55+
return mockFirebase;
56+
},
57+
on: (event, callback) => {
58+
assert.equal(event, 'value');
59+
60+
const mockSnapshot = {
61+
val: () => 'bar changed',
62+
};
63+
64+
callback(mockSnapshot);
65+
},
66+
};
67+
68+
const mapPropsToSubscriptions = () => ({ bar: firebase => firebase.startAt(1) });
69+
const WrappedComponent = connect(mapPropsToSubscriptions)('div');
70+
const container = renderIntoDocument(<WrappedComponent firebase={mockFirebase} />);
71+
72+
assert.deepEqual(container.state.subscriptionsState, { bar: 'bar changed' });
73+
assert.end();
74+
});
75+
76+
test('Should unsubscribe when component unmounts', assert => {
77+
const mockFirebase = {
78+
child: path => {
79+
assert.equal(path, 'baz');
80+
81+
return mockFirebase;
82+
},
83+
on: (event, callback) => {
84+
assert.equal(event, 'value');
85+
86+
const mockSnapshot = {
87+
val: () => 'baz changed',
88+
};
89+
90+
callback(mockSnapshot);
91+
},
92+
off: event => {
93+
assert.equal(event, 'value');
94+
},
95+
};
96+
97+
const mapPropsToSubscriptions = () => ({ baz: 'baz' });
98+
const WrappedComponent = connect(mapPropsToSubscriptions)('div');
99+
const container = renderIntoDocument(<WrappedComponent firebase={mockFirebase} />);
100+
101+
assert.deepEqual(container.state.subscriptionsState, { baz: 'baz changed' });
102+
103+
unmountComponentAtNode(findDOMNode(container).parentNode);
104+
105+
assert.end();
106+
});
107+
108+
test('Should subscribe to nested paths');
109+
test('Should map firebase to props');
110+
test('Should keep connection to Firebase alive when specified and ignore later updates');
111+
test('Should update subscriptions when props change');
112+
test('Should not re-render if options.pure is true');
113+
test('Should re-render if options.pure is false');
114+
test('Should merge using mergeProps function');

0 commit comments

Comments
 (0)