Skip to content

Commit e3f0d10

Browse files
authored
Correctly order subscription values if orderByChild was passed to query (#34)
1 parent 66ffc5f commit e3f0d10

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

src/connect.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, PropTypes, createElement } from 'react'
22
import invariant from 'invariant'
33
import firebase from 'firebase/app'
44
import 'firebase/database'
5-
import { createQueryRef, getDisplayName, mapValues, pickBy } from './utils'
5+
import { createQueryRef, getDisplayName, mapValues, mapSnapshotToValue, pickBy } from './utils'
66

77
const defaultMergeProps = (ownProps, firebaseProps) => ({
88
...ownProps,
@@ -80,13 +80,16 @@ export default (mapFirebaseToProps = defaultMapFirebaseToProps, mergeProps = def
8080

8181
const queries = mapSubscriptionsToQueries(subscriptions)
8282
const nextListeners = mapValues(queries, ({ path, ...query }, key) => {
83+
const containsOrderBy = Object.keys(query).some(queryKey => queryKey.startsWith('orderBy'))
8384
const subscriptionRef = createQueryRef(this.ref(path), query)
8485
const update = snapshot => {
8586
if (this.mounted) {
87+
const value = containsOrderBy ? mapSnapshotToValue(snapshot) : snapshot.val()
88+
8689
this.setState(prevState => ({
8790
subscriptionsState: {
8891
...prevState.subscriptionsState,
89-
[key]: snapshot.val(),
92+
[key]: value,
9093
},
9194
}))
9295
}

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ export const createQueryRef = (ref, query) => (
3434
export const getDisplayName = Component => (
3535
Component.displayName || Component.name || 'Component'
3636
)
37+
38+
export const mapSnapshotToValue = snapshot => {
39+
const result = {}
40+
41+
snapshot.forEach(child => {
42+
result[child.key] = child.val()
43+
})
44+
45+
return result
46+
}

tests/connect-test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,54 @@ test('Should subscribe to a query', assert => {
182182
assert.end()
183183
})
184184

185+
test('Should correctly order subscription values if orderByChild was passed to query', assert => {
186+
const mockDatabase = {
187+
ref: path => {
188+
assert.equal(path, 'bar')
189+
190+
return mockDatabase
191+
},
192+
orderByChild: value => {
193+
assert.equal(value, 'order')
194+
195+
return mockDatabase
196+
},
197+
on: (event, callback) => {
198+
assert.equal(event, 'value')
199+
200+
const snapshot = {
201+
val: () => ({
202+
alpha: { order: 3 },
203+
beta: { order: 2 },
204+
gamma: { order: 1 },
205+
}),
206+
207+
forEach: iterator => {
208+
iterator({ key: 'gamma', val: () => ({ order: 1 }) })
209+
iterator({ key: 'beta', val: () => ({ order: 2 }) })
210+
iterator({ key: 'alpha', val: () => ({ order: 3 }) })
211+
},
212+
}
213+
214+
callback(snapshot)
215+
},
216+
}
217+
218+
const mapFirebaseToProps = () => ({
219+
bar: {
220+
path: 'bar',
221+
orderByChild: 'order',
222+
},
223+
})
224+
225+
const firebaseApp = createMockApp(mockDatabase)
226+
const stub = renderStub({ mapFirebaseToProps, firebaseApp })
227+
228+
assert.deepEqual(Object.keys(stub.getSubscriptionState().bar), ['gamma', 'beta', 'alpha'])
229+
assert.deepEqual(Object.keys(stub.getProps().bar), ['gamma', 'beta', 'alpha'])
230+
assert.end()
231+
})
232+
185233
test('Should not subscribe to functions', assert => {
186234
const mapFirebaseToProps = (props, ref) => ({
187235
foo: 'foo',

0 commit comments

Comments
 (0)