Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Populate fixed to handle a list with a Firebase list as a parameter (…
…nested list). Tests for populate improved.
  • Loading branch information
Scott Prue committed Feb 1, 2017
commit f1767f5db919511597352f3c90d8123d8ef3cd81
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
example/**
examples/**
coverage/**
node_modules/**
*.spec.js
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-redux-firebase",
"version": "1.2.2",
"version": "1.2.3",
"description": "Redux integration for Firebase. Comes with a Higher Order Component for use with React.",
"main": "dist/index.js",
"module": "src/index.js",
Expand Down
32 changes: 12 additions & 20 deletions src/utils/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,24 @@ export const getPopulateChild = (firebase, populate, id) =>
* @param {Object} populate - Object containing populate information
* @param {Object} results - Object containing results of population from other populates
*/
export const populateList = (firebase, originalData, p, results) => {
const mainChild = p.child.split('[]')[0]
const childParam = p.child.split('[]')[1]
export const populateList = (firebase, list, p, results) => {
// Handle root not being defined
if (!results[p.root]) {
set(results, p.root, {})
}
return Promise.all(
map(get(originalData, mainChild), (id, childKey) => {
map(list, (id, childKey) => {
// handle list of keys
const populateKey = id === true ? childKey : id
return getPopulateChild(
firebase,
p,
childParam
? get(id, childParam) // get child parameter if [] notation
: populateKey
populateKey
)
.then(pc => {
if (pc) {
// write child to result object under root name if it is found
if (!childParam) {
return set(results, `${p.root}.${populateKey}`, pc)
}
// handle child param
return ({
[childKey]: set(
id,
childParam,
Object.assign(pc, { key: get(id, childParam) })
)
})
return set(results, `${p.root}.${populateKey}`, pc)
}
return results
})
Expand All @@ -131,7 +121,9 @@ export const promisesForPopulate = (firebase, originalData, populatesIn) => {

// Single parameter with list
if (has(originalData, mainChild)) {
return promisesArray.push(populateList(firebase, originalData, p, results))
return promisesArray.push(
populateList(firebase, originalData[mainChild], p, results)
)
}
// Loop over each object in list
forEach(originalData, (d, key) => {
Expand Down Expand Up @@ -161,7 +153,7 @@ export const promisesForPopulate = (firebase, originalData, populatesIn) => {
if (isArray(idOrList) || isObject(idOrList)) {
// Create single promise that includes a promise for each child
return promisesArray.push(
populateList(firebase, originalData, p, results)
populateList(firebase, idOrList, p, results)
)
}
})
Expand Down
60 changes: 54 additions & 6 deletions test/unit/utils/populate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPopulateObj,
getPopulates,
getPopulateChild,
getPopulateObjs,
promisesForPopulate
} from '../../../src/utils/populate'

Expand All @@ -11,15 +12,30 @@ describe('Utils: Populate', () => {
it('returns object with child and root', () => {
expect(getPopulateObj('some:value')).to.have.keys('child', 'root')
})
it('returns object if passed', () => {
const inputObj = { child: 'some', root: 'some' }
expect(getPopulateObj(inputObj)).to.equal(inputObj)
})
})

describe('getPopulateObj', () => {
it('returns object with child and root', () => {
expect(getPopulateObjs(['some:value'])[0]).to.have.keys('child', 'root')
})
it('handles basic populates', () => {
const inputString = 'populate=uid:users'
expect(getPopulateObjs(inputString)).to.equal(inputString)
})
})

describe('getPopulates', () => {
it('no populates', () => {
it('handles no populates', () => {
expect(getPopulates(['orderByPriority']))
})
it('basic populates', () => {
it('handles basic populates', () => {
expect(getPopulates(['populate=uid:users']))
})

})

describe('getPopulateChild', () => {
Expand All @@ -30,22 +46,54 @@ describe('Utils: Populate', () => {
})

describe('promisesForPopulate', () => {
it('none existant child', () =>
promisesForPopulate(Firebase, {uid: '123123'}, [{child: 'random', root: 'users'}])
it('handles non-existant single child', () =>
promisesForPopulate(Firebase, { uid: '123123' }, [{child: 'random', root: 'users'}])
.then((v) => {
expect(JSON.stringify(v)).to.equal(JSON.stringify({}))
})
)
it('string populate', () =>

it('populates single property containing a list', () =>
promisesForPopulate(Firebase, { collaborators: { 'Iq5b0qK2NtgggT6U3bU6iZRGyma2': true, '123': true } }, [{child: 'collaborators', root: 'users'}])
.then((v) => {
expect(v).to.exist
expect(v).to.have.keys('users')
expect(v.users['Iq5b0qK2NtgggT6U3bU6iZRGyma2']).to.be.an.object
})
)

it('populates list with single property populate', () =>
promisesForPopulate(Firebase, { 1: { owner: 'Iq5b0qK2NtgggT6U3bU6iZRGyma2' } }, [{child: 'owner', root: 'users'}])
.then((v) => {
expect(v).to.have.keys('users')
expect(v.users['Iq5b0qK2NtgggT6U3bU6iZRGyma2']).to.be.an.object
})
)
it('array populate', () =>

it('populates list with property containing array property', () =>
promisesForPopulate(Firebase, { 1: { collaborators: ['Iq5b0qK2NtgggT6U3bU6iZRGyma2', '123'] } }, [{child: 'collaborators', root: 'users'}])
.then((v) => {
expect(v).to.exist
expect(v).to.have.keys('users')
expect(v.users['Iq5b0qK2NtgggT6U3bU6iZRGyma2']).to.be.an.object
})
)

it('populates list with property containing firebase list', () =>
promisesForPopulate(Firebase, { 1: { collaborators: { 'Iq5b0qK2NtgggT6U3bU6iZRGyma2': true, '123': true } } }, [{child: 'collaborators', root: 'users'}])
.then((v) => {
expect(v).to.exist
expect(v).to.have.keys('users')
expect(v.users['Iq5b0qK2NtgggT6U3bU6iZRGyma2']).to.be.an.object
})
)

it('populates list with property containing invalid child id', () =>
promisesForPopulate(Firebase, { 1: { collaborators: ['1111', '123'] } }, [{child: 'collaborators', root: 'users'}])
.then((v) => {
expect(v).to.exist
expect(v.users).to.have.keys('123') // sets valid child
expect(v.users).to.not.have.keys('111') // does not set invalid child
})
)
})
Expand Down