React-Dimension getWrappedInstance()在酶测试

时间:2017-06-01 21:27:39

标签: reactjs testing enzyme karma-mocha

我遇到了一个关于一个reactJs组件的create test的问题,该组件被React-Dimension包装为HOC。这是简单的设置

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);

这是测试

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 !!');
});

});

getWrappedInstance()返回undefined。如果我不将Dimension作为包装HOC,那么测试用例就可以了。我错过了什么,所以getWrappedInstance()的返回是未定义的吗?

我尝试使用mount或sallow而不是更改结果。我需要测试的是这种情况下CustomComponent的sayHi函数。

0 个答案:

没有答案