Skip to content

Instantly share code, notes, and snippets.

@eskimoblood
Created January 11, 2019 08:56
Show Gist options
  • Save eskimoblood/20fcc8b7994c1abcb192a07936885a4f to your computer and use it in GitHub Desktop.
Save eskimoblood/20fcc8b7994c1abcb192a07936885a4f to your computer and use it in GitHub Desktop.
Simple test helper for testing recompose HOCs
import React from 'react'
import { mount } from 'enzyme'
const render = ({ hoc, data, context }) => {
const Component = () => <div />
const WrappedComponent = hoc(Component)
return mount(<WrappedComponent {...data} />, { context })
}
// By default, return the 'inner', wrapped component.
export default (hoc, data, context) =>
render({ hoc, data, context }).find('Component')
// to test state updates we need to return the component and the update method
export const renderHOCWithStateUpdate = (hoc, data, context) => {
const component = render({ hoc, data, context })
const update = () => {
component.update()
return component.find('Component')
}
return {
component: component.find('Component'),
update,
root: component,
}
}
// Usage
import renderHOC, {renderHOCWithStateUpdate} from './utils/renderHOC'
// simple case without setState
const component = renderHOC({hoc: someHOC, data:{some:'data'}})
expect(component).toMatchSnapshot()
// case without setState
const wrapper = renderHOCWithStateUpdate({hoc: someHOC, data:{some:'data'}})
let component = wrapper.component
let update = wrapper.update
component.prop('someStateUpdateMethod')()
component = update()
expect(component).toMatchSnapshot()
@eskimoblood
Copy link
Author

The main idea is to render HOCs independently from real views, by using just a simple dummy component and only test that the props passed to the dummy are correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment