dynamic-hoc
A utility that wraps a React HOC and produces a HOC with additional methods to swap out the given HOC at runtime.
This module provides the ability to wrap any React HOC (such as react-redux.connect
or react-router.withRouter
) to produce a new HOC. The resultant HOC function has properties that allow you to change the HOC at runtime and re-render the wrapped component.
Why?
The primary purpose of this function is to provide the ability to write unit tests for React components in isolation from data injected by a HOC, which often requires extensive mocking. While the developer can export both the original component and the wrapped component, this technique does not apply if the component’s descendants are wrapped by the HOC. With dynamicHoc
, each wrapped component can have its HOC replaced with mock data or functions in the test suite.
This enables developers to test React components as units. This is normally possible by exporting the unwrapped component and testing it in isolation, but the component cannot be fully rendered in a test if it contains child components that are wrapped in HOCs. If the HOC has logic of its own, such as functions mapStateToProps
, they can be tested independently with dedicated unit tests, or composed with a utility like reselect
, where the selectors have their own dedicated unit tests. See the Example section for an illustration of a typical scenario where a “pure” component’s children are components wrapped in a HOC.
Installation
# npm npm install dynamic-hoc # yarn yarn add dynamic-hoc
Example
Components
import connect from 'react-redux'import React from 'react'import dynamicHoc from 'dynamic-hoc' const TodoList = todos <ul> todos </ul> const TodoListContainer = TodoList const Todo = creator onComplete todo <div> <span>todotext</span> <span>Created by creatorname</span> <button =>Complete</button> </div> const TodoContainer = Todo
Tests
import React from 'react'import render from '@testing-library/react'import test from 'ava'import injectProps from 'inject-props' test
API
dynamicHoc(initialHoc): Component => WrapperComponent
Argument | Type | Description |
---|---|---|
initialHoc |
Component => WrapperComponent |
Any higher order component factory. |
Component |
ReactComponent |
The component to wrap. |
The returned wrapper component has additional methods:
WrappedComponent.replaceHoc(hoc): void
Replaces the current HOC, triggering any component instances to re-render.
Argument | Type | Description |
---|---|---|
hoc |
Component => WrapperComponent |
Any higher order component factory. |
const MyConnectedComponent = MyComponent MyConnectedComponent <MyConnectedComponent = = />// → <MyComponent x={1} y={2} location={{ pathname: '/foo' }} />
WrappedComponent.resetHoc(): void
Replaces the current HOC with initialHoc
, triggering any component instances to re-render.
MyConnectedComponent
FAQ
What if I don't want to add a test dependency to my production app?
You could export a version of the HOC that only loads dynamic-hoc
when in your test environment.
let connect = reduxConnect if processenvNODE_ENV === 'test' const dynamicHoc =
Or you could use a test library to mock the dependency.
jest