简体   繁体   English

使用Jest测试React Component Mount

[英]Test a React Component Mount with Jest

I'm trying to figure out how to test a react component when initial data gets loaded via ajax on the mount. 我试图找出如何在通过mount上的ajax加载初始数据时测试react组件。

var ResourceList = React.createClass({
  componentDidMount: function() {
    api.fetchResources(this.props.type).then(function(raw) {
        console.log(raw);
      this.setState({
        resources: raw
      });
    }.bind(this));
  },

  getInitialState: function() {
    return {
      resources: []
    };
  },

  render: function() {
    var resources = this.state.resources.map(function(resource, index) {
      return (
        <Resource raw={resource} key={index}/>
      );
    });

    return (
      <ul>{resources}</ul>
    );
  }
});

The componentDidMount function calls an API that returns a jquery promise. componentDidMount函数调用返回jquery promise的API。

var fetchResources = function(type) {
  var endpoint = '/resources';

  return $.ajax({
    url: base + endpoint + '/' + type,
    dataType: 'json',
    headers: headers
  });
}

// Return promise
module.exports = {
  fetchResources: fetchResources
};

My test code looks like this: 我的测试代码如下所示:

describe('something', function() {
  beforeEach(function() {
    React = require('react/addons');
    TestUtils = React.addons.TestUtils;
    ResourceListComponent = require('../app/components/resource-list');

    ResourceList = TestUtils.renderIntoDocument(<ResourceListComponent />);

    // Dummy data from server
    resources = [{
      author: 'author',
      body: 'body'
    }];

    // Add the dummy data from the server
    ResourceList.setState({
      resources: resources
    });
  });


  it('1', function() {
    expect(ResourceList.state.resources.length).toBe(1);
  });
});

The error I am getting is: "TypeError: Cannot call method 'then' of undefined", which is understandable, but how can I correct this? 我得到的错误是:“TypeError:无法调用方法'然后'未定义',这是可以理解的,但我怎么能纠正这个?

If you haven't already (you're not showing if you're doing this when looking at your pasted code) then you need to add either of the two lines below. 如果你还没有(你在查看粘贴的代码时没有显示你是否这样做),那么你需要添加以下两行中的任何一行。

jest.dontMock('../app/components/resource-list');

or 要么

jest.dontMock('../path/to/your/apiclient');

Jest mocks all of the required modules dependencies by default which will have your API client always return undefined for any method called. 默认情况下,Jest会模拟所有必需的模块依赖项,这将使您的API客户端始终为任何调用的方法返回undefined。

Secondly, I would like to push for you to use your actual implementation of your API handler and mock out the HTTP call instead using a module like nock . 其次,我想推动您使用您的API处理程序的实际实现,并使用像nock这样的模块来模拟HTTP调用。 The scopes return by nock will intercept all HTTP calls and let you make expectations on the responses so when mounting your component you could have nock actually return valid data and expect that everything worked. nock返回的作用域将拦截所有HTTP调用,并让您对响应有所期望,因此在安装组件时,您可以让nock实际返回有效数据并期望一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM