javascript - 浅渲染不渲染包装在消费者中的组件( Jest/ enzyme )

标签 javascript reactjs jestjs enzyme

我有这样一个组件:

import React from 'react';
import classNames from 'classnames/bind';

import { connect } from 'react-redux';

import Topic from './components/Topics';
import AutoItem from './AutoItem';
import DataContext from "./dataContext";

const cx = classNames.bind(Styles);

export class TestComponent extends React.Component {


  render() {


    return (
      <div>
              <DataContext.Consumer>
                {value => (
                  <AutoItem
                    data={value}
                  />
                )}
              </DataContext.Consumer>


            <DataContext.Consumer>
              {value => (
                <Topic data={value} />
              )}
            </DataContext.Consumer>

      </div>
    );
  }
}

const mapStateToProps = state => ({
  ...
});

const mapDispatchToProps = dispatch => ({
  ...
});

export default connect(mapStateToProps, mapDispatchToProps)(TestComponent);

我想用 jest/enzyme 测试它。我编写了这样的测试以检查在 TestComponent 中呈现的 Topic 和 AutoItem 组件:

describe('<TestComponent  /> Component render', () => {

  let wrapper;

  beforeEach(() => {
    wrapper = shallow(<TestComponent {...props} />);
  });

  describe('<TestComponent  /> rendering', () => {

    test('should render child components in <TestComponent  /> component', () => {
      console.log(wrapper.debug())
      expect(wrapper.find(AutoItem).length).toEqual(1);
      expect(wrapper.find(Topic).length).toEqual(1);
    });

  });
});

但测试失败,这是我在控制台中的内容:

expect(received).toEqual(expected) // deep equality

    Expected: 1
    Received: 0

这是 console.log(wrapper.debug()) 的输出:

<div className="">

            <ContextConsumer>
              [function]
            </ContextConsumer>
          </div>

          <ContextConsumer>
            [function]
          </ContextConsumer>


    </div>

我不明白为什么包装在 Consumer 中的组件不呈现并在控制台输出中显示为 [function]。有人可以帮我解决这个问题吗?

"jest": "^24.8.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",

根据我写的答案:

import DataContext from './dataContext';


test('should render child components in <TestComponent  /> component', () => {
      const wrapper = shallow(<TestComponent {...props} />)
        .find(DataConsumer.Consumer)
        .renderProp('children')();

          expect(wrapper.find(AutoItem).length).toEqual(1);
          expect(wrapper.find(Topic).length).toEqual(1);
    });

这就是我在 cosnole 中的内容:

Expected: 1
Received: 0

这是我在调试包装器时在控制台中得到的:

最佳答案

在 Enzyme 中你应该显式渲染 renderProp: enzyme

关于javascript - 浅渲染不渲染包装在消费者中的组件( Jest/ enzyme ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57400667/

相关文章:

javascript - React 中的 Ag-grid 纯 JS 单元渲染器

javascript - 从子级发送到父级的 Prop 返回最后一个值

javascript - 如何测试 React 组件使用 jest 渲染列表

javascript - 将 backbone.js View 附加到现有元素与将 el 插入 DOM

javascript - 如何使用 JavaScript 设置使用 innerHTML 插入的文本的样式

javascript - 通过 jQuery 求数字之和

javascript - 状态更改后 React 不会更新组件

javascript - 如何使用守卫nestjs进行e2e

node.js - Jest 的 expect(value).toBeInstanceOf(Class) 因 expect(util.promisify(...)()).toBeInstanceOf(Promise) 等而失败

javascript - 使用 Mongoose 时出现错误 'Object has no method xxx '