Skip to content

Instantly share code, notes, and snippets.

@kimniche
Created March 2, 2017 14:39
Show Gist options
  • Select an option

  • Save kimniche/d3b8c0fd2bfcab8faf6c4d686c72aed1 to your computer and use it in GitHub Desktop.

Select an option

Save kimniche/d3b8c0fd2bfcab8faf6c4d686c72aed1 to your computer and use it in GitHub Desktop.
/* eslint-disable class-methods-use-this */
import React from 'react'
import { connectToStores } from 'fluxible-addons-react'
/**
* In our unit test suite, it becomes really cumbersome to
* test around the component wrapper that gets instantiated
* through the `connectToStores` call. This method just lets
* us avoid this wrapper for direct access to the component
* under test.
*/
const _getConnectToStores = (env = process.env.NODE_ENV) => {
if (env === 'test') {
return component => component
}
// Otherwise, just return the Fluxible function
return connectToStores
}
function _withDependenciesInContext(WrappedComponent, dependencies) {
return class extends React.Component {
getChildContext() {
return dependencies
}
render() {
return <WrappedComponent { ...this.props } />
}
}
}
const _getWithDependencies = (env = process.env.NODE_ENV) => {
if (env === 'test') {
// The test is responsible for explicitly specifying
// each of the component's dependencies
return component => component
}
// Otherwise, pack the dependencies provided into componentContext
return _withDependenciesInContext
}
export default {
connectToStores: _getConnectToStores(),
withDependencies: _getWithDependencies(),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment