Created
January 11, 2019 08:56
-
-
Save eskimoblood/20fcc8b7994c1abcb192a07936885a4f to your computer and use it in GitHub Desktop.
Simple test helper for testing recompose HOCs
This file contains hidden or 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.