-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #342 from stevoland/deferred
Support fetchDataDeferred
- Loading branch information
Showing
4 changed files
with
102 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { expect } from 'chai'; | ||
import React from 'react'; | ||
import { div } from 'react-dom'; | ||
import getDataDependencies from '../getDataDependencies'; | ||
|
||
describe('getDataDependencies', () => { | ||
let getState; | ||
let dispatch; | ||
let location; | ||
let params; | ||
let CompWithFetchData; | ||
let CompWithNoData; | ||
let CompWithFetchDataDeferred; | ||
let ConnectedCompWithFetchData; | ||
let ConnectedCompWithFetchDataDeferred; | ||
|
||
beforeEach(() => { | ||
getState = 'getState'; | ||
dispatch = 'dispatch'; | ||
location = 'location'; | ||
params = 'params'; | ||
|
||
CompWithNoData = () => | ||
<div />; | ||
|
||
CompWithFetchData = () => | ||
<div />; | ||
|
||
CompWithFetchData.fetchData = (_getState, _dispatch, _location, _params) => { | ||
return `fetchData ${_getState} ${_dispatch} ${_location} ${_params}`; | ||
}; | ||
CompWithFetchDataDeferred = () => | ||
<div />; | ||
|
||
CompWithFetchDataDeferred.fetchDataDeferred = (_getState, _dispatch, _location, _params) => { | ||
return `fetchDataDeferred ${_getState} ${_dispatch} ${_location} ${_params}`; | ||
}; | ||
|
||
ConnectedCompWithFetchData = () => | ||
<div/>; | ||
|
||
ConnectedCompWithFetchData.WrappedComponent = CompWithFetchData; | ||
|
||
ConnectedCompWithFetchDataDeferred = () => | ||
<div/>; | ||
|
||
ConnectedCompWithFetchDataDeferred.WrappedComponent = CompWithFetchDataDeferred; | ||
}); | ||
|
||
it('should get fetchDatas', () => { | ||
const deps = getDataDependencies([ | ||
CompWithFetchData, | ||
CompWithNoData, | ||
CompWithFetchDataDeferred, | ||
ConnectedCompWithFetchData, | ||
ConnectedCompWithFetchDataDeferred | ||
], getState, dispatch, location, params); | ||
|
||
expect(deps).to.deep.equal([ | ||
'fetchData getState dispatch location params', | ||
'fetchData getState dispatch location params' | ||
]); | ||
}); | ||
|
||
it('should get fetchDataDeferreds', () => { | ||
const deps = getDataDependencies([ | ||
CompWithFetchData, | ||
CompWithNoData, | ||
CompWithFetchDataDeferred, | ||
ConnectedCompWithFetchDataDeferred | ||
], getState, dispatch, location, params, true); | ||
|
||
expect(deps).to.deep.equal([ | ||
'fetchDataDeferred getState dispatch location params', | ||
'fetchDataDeferred getState dispatch location params' | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
const getDataDependency = (component = {}) => { | ||
const getDataDependency = (component = {}, methodName) => { | ||
return component.WrappedComponent ? | ||
getDataDependency(component.WrappedComponent) : | ||
component.fetchData; | ||
getDataDependency(component.WrappedComponent, methodName) : | ||
component[methodName]; | ||
}; | ||
|
||
export default (components, getState, dispatch, location, params) => { | ||
export default (components, getState, dispatch, location, params, deferred) => { | ||
const methodName = deferred ? 'fetchDataDeferred' : 'fetchData'; | ||
|
||
return components | ||
.filter((component) => getDataDependency(component)) // only look at ones with a static fetchData() | ||
.map(getDataDependency) // pull out fetch data methods | ||
.filter((component) => getDataDependency(component, methodName)) // only look at ones with a static fetchData() | ||
.map((component) => getDataDependency(component, methodName)) // pull out fetch data methods | ||
.map(fetchData => | ||
fetchData(getState, dispatch, location, params)); // call fetch data methods and save promises | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters