jeudi 1 juin 2017

React-Dimension getWrappedInstance() returns undefined in enzyme test

I ran into an issue on create test for a reactJs component that is wrapped by React-Dimension as an HOC. Here is the simple set up

import React, { PropTypes } from 'react';
import Dimensions from 'react-dimensions';

export class CustomComponent extends React.Component {
  static propTypes = {
    messageProp: PropTypes.string.isRequired
  };

  constructor(props, context) {
    super(props, context);
    this.state = {
      message: props.messageProp
    };
  };

// a simple function to be tested
  sayHi () {
    return this.state.message;
  }

  render () {
    return (<div>{this.sayHi()}</div>);
  }
}

export default Dimensions({elementResize: true})(CustomComponent);

And here is the test

import React from 'react';
import { mount, shallow } from 'enzyme';
import CustomComponent from 'components/CustomComponent/CustomComponent.js';


describe('<CustomComponent /> presentation', () => {
  let _props = {
    messageProp: 'Hi Message !!'
  }

it('HOC Wrapped Custom test ', () => {
  const c = mount(<CustomComponent {..._props}/>);
  const inst = c.instance();
  const childInst = inst.getWrappedInstance();
  expect(childInst.sayHi()).to.equal('Hi Message !!');
});

});

The getWrappedInstance() is returning undefined. If I don't put the Dimension as the wrapping HOC, the test case will work just fine. Did I missed something so the return of the getWrappedInstance() is undefined?

I have tried to use mount or sallow and not changing the result. What I need to test is the sayHi function of the CustomComponent in this case.

Aucun commentaire:

Enregistrer un commentaire