【问题标题】:Enzyme: shallow render inner react-redux componentsEnzyme:浅渲染内部 react-redux 组件
【发布时间】:2017-03-11 05:53:56
【问题描述】:

我有一个简单的反应组件,它使用来自 antd 的 Card:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Card } from 'antd';

export class TBD extends Component {

  constructor() {
    super();
  }

  render() {
    return (
      <Card title={this.props.pathname}>
        TODO
      </Card>
    );
  }
}

export let select = (state) => {
  return state.route;
};

export default connect(select)(TBD);

现在我写了一些简单的测试并想检查我的 TBD 组件是否使用 Card

import React from 'react';
import {mount, shallow}  from 'enzyme';
import {Provider, connect}  from 'react-redux';
import {createMockStore}  from 'redux-test-utils';
import {expect} from 'chai';
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
chai.use(chaiEnzyme());
import { Card } from 'antd';
import TBDConnected,  {TBD, select} from '../src/js/components/TBD';

describe('Fully Connected:', function () {
    it('show selected item text', function () {

      const expectedState = {route: {pathname: 'Menu1'}};
      const store = createMockStore(expectedState);
      const ConnectedComponent = connect(select)(TBDConnected);
      const component = shallow(<ConnectedComponent store={store} />).shallow().shallow();
      console.log(component.debug());
      expect(component.equals(<Card/>)).is.equal(true);
    });
  });

它失败了,因为3浅返回我

<Component title="Menu1">
TODO
</Component>

但我期待

<Card title="Menu1">
TODO
</Card>

再一次渲染后,我从渲染 Card 中得到了纯 html 我不明白为什么它将它渲染到 Component 而不是 Card 以及如何获得我想要的结果。

更新

这个例子可以简化我的问题。下一次测试失败:

describe('TBD', function () {
  it('Renders a Card', function () {
    const component = shallow(<TBD />);
    console.log(component.debug());
    expect(component.equals(<Card/>)).is.equal(true);
  });
});

控制台中的调试输出:

<Component title={[undefined]}>
TODO
</Component>

但我希望:

<Card title={[undefined]}>
TODO
</Card>

【问题讨论】:

  • connect 是一个高阶组件,因此将Card 包装在一个通用组件中。我认为shallow 只会呈现第一级(这将是通用组件)。这个讨论可能有用:github.com/reactjs/redux/issues/1534
  • @DavinTryon,如果我扔掉所有 redux 部分(连接、存储等),只渲染反应组件(它单独导入):shallow(&lt;TBD pathname='my title' /&gt;),那么我再次得到结果@987654333 @.

标签: javascript reactjs redux react-redux enzyme


【解决方案1】:

Ant Delvelope 组件中的问题。该组件的一部分编写为简单的匿名函数,没有扩展 React.Component 等。结果 Enzyme 将其呈现为 &lt;Component /&gt;,在浏览器中呈现为 &lt;StatelessComponent /&gt;

【讨论】:

  • 哦,我明白了,我们正在使用classNamefind('.my-component') 来断言组件是否呈现,我将调试我的纯箭头函数组件,看看它们是否像你的一样...
【解决方案2】:

您不需要测试整个连接的组件。 我会先测试 presentational pure component(作为单元测试),然后您可以单独测试 connector

I.E.

import React from 'react';
import {shallow}  from 'enzyme';
import {expect} from 'chai';
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
chai.use(chaiEnzyme());
import { Card } from 'antd';
import {TBD} from '../src/js/components/TBD';

describe('TBD', function () {
  it('Renders a Card', function () {
    const component = shallow(<TBD />);
    expect(component.equals(<Card/>)).is.equal(true);
  });
  it('sets the right title', function () {
    const component = shallow(<TBD pathname="example" />);
    expect(component.prop('title')).is.equal("example");
  });
});

如您所见,您的纯组件必须作为纯函数进行测试。你传递了一些道具并期待一些渲染。

然后,当您测试您的连接器时,您可以断言它正确映射了 stateToProps 和 dispatchToProps...

【讨论】:

  • 这将是完美的,但测试“渲染卡”中的问题 - 它失败了。 console.log(component.debug()); 用于此测试中的组件返回 &lt;Component title={[undefined]}&gt; TODO &lt;/Component&gt;。而且我不明白为什么它会在超类 Component 中渲染 Card。
  • 如果你使用纯函数而不是 React.Component,它会渲染 export const TBD = ({pathname}) =&gt; &lt;Card title={pathname}&gt;TODO&lt;/Card&gt;
  • 不,仍然呈现为组件。而现在我更加困惑了。
  • 这看起来像是 antd 组件中的问题。我尝试使用自己的组件,一切正常。
猜你喜欢
  • 2020-08-15
  • 2016-12-18
  • 2016-10-29
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2020-03-12
  • 2020-04-20
  • 1970-01-01
相关资源
最近更新 更多