Enzyme HOC Recomposition Testing

How can I test (with jest

+ enzyme

) the following code which is using recompose

to create HoC:

import {compose, withState, withHandlers} from 'recompose'

const addCounting = compose(
  withState('counter', 'setCounter', 0),
  withHandlers({
    increment: ({ setCounter }) => () => setCounter(n => n + 1),
    decrement: ({ setCounter }) => () =>  setCounter(n => n - 1),
    reset: ({ setCounter }) => () => setCounter(0)
  })
)

      

When doing shallow rendering, I have access to properties counter

and setCounter

for example:

import {shallow} from 'enzyme'

const WithCounting = addCounting(EmptyComponent)
const wrapper = shallow(<WithCounting />)

wrapper.props().setCounter(1)
expect(wrapper.props().counter).toEqual(1)

      

The big question is, how can I access the handlers ( increment

, decrement

and reset

) and call them? They do not appear inwrapper.props()

+3


source to share


1 answer


So, you can access the props by first finding the instance:



const EmptyComponent = () => null;
const WithCounting = addCounting(props => <EmptyComponent {...props} />);
const wrapper = mount(<WithCounting />);

wrapper.find(EmptyComponent).props().setCounter(1);
expect(wrapper.find(EmptyComponent).props().counter).toEqual(1);

      

+4


source







All Articles