将react-intl对象注入已安装的酶组件进行测试

时间:2016-05-04 07:31:25

标签: reactjs enzyme react-intl

编辑:解决了!向下滚动以获得答案

在我们的组件测试中,我们需要他们可以访问react-intl上下文。问题是我们正在安装单个组件(使用Enzyme&#39; mount())而不使用<IntlProvider />父包装器。这可以通过包装提供程序来解决,但root指向IntlProvider实例,而不是CustomComponent

Testing with React-Intl: Enzyme文档仍为空。

&lt; CustomComponent /&gt;

class CustomComponent extends Component {
  state = {
    foo: 'bar'
  }

  render() {
    return (
      <div>
        <FormattedMessage id="world.hello" defaultMessage="Hello World!" />
      </div>
    );
  }
}

标准测试用例(所需)(酶+摩卡+柴)

// This is how we mount components normally with Enzyme
const wrapper = mount(
  <CustomComponent
    params={params}
  />
);

expect( wrapper.state('foo') ).to.equal('bar');

但是,由于我们的组件使用FormattedMessage作为react-intl库的一部分,因此在运行上述代码时会出现此错误:

Uncaught Invariant Violation: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.

使用IntlProvider

进行包装
const wrapper = mount(
  <IntlProvider locale="en">
    <CustomComponent
      params={params}
    />
  </IntlProvider>
);

这为CustomComponent提供了它要求的intl上下文。但是,当尝试进行测试断言时,例如:

expect( wrapper.state('foo') ).to.equal('bar');

引发以下异常:

AssertionError: expected undefined to equal ''

这是因为它试图读取IntlProvider的状态,而不是我们的CustomComponent

尝试访问CustomComponent

我尝试过以下无效:

const wrapper = mount(
  <IntlProvider locale="en">
    <CustomComponent
      params={params}
    />
  </IntlProvider>
);


// Below cases have all individually been tried to call `.state('foo')` on:
// expect( component.state('foo') ).to.equal('bar');

const component = wrapper.childAt(0); 
> Error: ReactWrapper::state() can only be called on the root

const component = wrapper.children();
> Error: ReactWrapper::state() can only be called on the root

const component = wrapper.children();
component.root = component;
> TypeError: Cannot read property 'getInstance' of null

问题是:我们如何在CustomComponent上下文中安装intl,同时仍能执行&#34; root&#34;我们的CustomComponent 上的操作?

1 个答案:

答案 0 :(得分:25)

我已经创建了一个辅助函数来修补现有的酶mount()shallow()函数。我们现在在使用React Intl组件的所有测试中使用这些辅助方法。

您可以在此处找到要点:https://gist.github.com/mirague/c05f4da0d781a9b339b501f1d5d33c37

为了保持数据的可访问性,这里的代码简而言之:

<强>助手/ INTL-test.js

/**
 * Components using the react-intl module require access to the intl context.
 * This is not available when mounting single components in Enzyme.
 * These helper functions aim to address that and wrap a valid,
 * English-locale intl context around them.
 */

import React from 'react';
import { IntlProvider, intlShape } from 'react-intl';
import { mount, shallow } from 'enzyme';

const messages = require('../locales/en'); // en.json
const intlProvider = new IntlProvider({ locale: 'en', messages }, {});
const { intl } = intlProvider.getChildContext();

/**
 * When using React-Intl `injectIntl` on components, props.intl is required.
 */
function nodeWithIntlProp(node) {
  return React.cloneElement(node, { intl });
}

export default {
  shallowWithIntl(node) {
    return shallow(nodeWithIntlProp(node), { context: { intl } });
  },

  mountWithIntl(node) {
    return mount(nodeWithIntlProp(node), {
      context: { intl },
      childContextTypes: { intl: intlShape }
    });
  }
};

<强> CustomComponent

class CustomComponent extends Component {
  state = {
    foo: 'bar'
  }

  render() {
    return (
      <div>
        <FormattedMessage id="world.hello" defaultMessage="Hello World!" />
      </div>
    );
  }
}

<强> CustomComponentTest.js

import { mountWithIntl } from 'helpers/intl-test';

const wrapper = mountWithIntl(
  <CustomComponent />
);

expect(wrapper.state('foo')).to.equal('bar'); // OK
expect(wrapper.text()).to.equal('Hello World!'); // OK