简体   繁体   English

使用酶,正弦波测试反应成分内部的图像加载

[英]Test image onload inside react component using enzyme, sinon

I'm trying to test that a function callback is being called for an image inside my component. 我正在尝试测试组件内部的图像正在调用函数回调。

This is the component: 这是组件:

const ImageById = (props) => {
  return (
    <div>
      <img src={props.url} onLoad={props.onLoad} onError={props.onError} />
    </div>
  );
};

And my test mounts the component and then spy the callback function: 然后我的测试会挂载该组件,然后监视回调函数:

describe('ImageById', () => {
  it('should call load or error', () => {
    let callbackSpy = sinon.spy();
    let comp = mount(ImageById, {url: 'some-url', onLoad: callbackSpy, onError: callbackSpy});
    expect(callbackSpy.called).to.equal(true);
  });
});

The test fails because the inner Img tag is never calling its onload nor onerror methods. 测试失败,因为内部Img标记从不调用其onloadonerror方法。 In production the code is working fine, it might be something related to the test environment. 在生产中,代码运行良好,可能与测试环境有关。 Its like Img tag doesn't react to the url being set. 就像Img标签一样,它对设置的url没有反应。

I discovered that mounting the component doesn't causes load and error events to occur. 我发现安装该组件不会导致发生loaderror事件。 I found a way of simulate them using TestUtils like this: 我找到了一种使用TestUtils模拟它们的方法,如下所示:

var Wrapper = React.createClass({
  render: function () {
    return (
      <div>{this.props.children}</div>
    );
  },
  propTypes: {
    children: PropTypes.object
  }
});

describe('ImageById', () => {
  it('should call load or error', () => {
    let callbackSpy = sinon.spy();
    // need to wrap `Images` inside a class component because `renderIntoDocument`
    // wont render pure functional components.
    // https://github.com/facebook/react/issues/4692#issuecomment-163029873
    var component = TestUtils.renderIntoDocument(<Wrapper><ImageById url={'some-url'} onLoad={callbackSpy} onError={callbackSpy} /></Wrapper>);
    var image = TestUtils.findRenderedDOMComponentWithTag(component, 'img');
    TestUtils.Simulate.load(image);
    expect(callbackSpy.called).to.equal(true);
  });
});

暂无
暂无

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

相关问题 使用 Jest/Enzyme 在 React 功能组件中测试封闭组件 - Test enclosing component inside a React functional component using Jest/Enzyme 如何使用Jest +酶在本机反应中测试样式化组件内部的文本 - How to test the text inside a styled component in react native using jest + enzyme 尝试使用 Jest 和 Enzyme 对 React 组件进行单元测试 - Trying to unit test a React component using Jest and Enzyme 如何使用Jest和Enzyme为简单的React组件编写测试用例 - How to write a test case for a simple React component using Jest and Enzyme 如何使用玩笑和酶测试异步数据获取反应组件? - How to test async data fetching react component using jest and enzyme? 如何使用 Jest/Enzyme 在功能性 React 组件中测试 lambda 函数? - How to test lambda functions in a functional React Component using Jest/Enzyme? 如何在反应中使用 jest 和酶对高阶组件进行单元测试 - How to unit test a higher order component using jest and enzyme in react 如何使用 Jest + Enzyme 覆盖 Svg 反应组件的测试用例 - How to cover test cases for Svg react component using Jest + Enzyme 为什么在React单元测试中的模拟事件中使用sinon.spy模拟函数和React Component的asen.shallow渲染时,这是未定义的? - Why is this is undefined when mocking event in React unit testing, using sinon.spy to mock function and enzyme.shallow render for React Component? 使用 Jest / Enzyme 在 React 中的功能组件内部测试方法 - Testing method inside functional component in React using Jest / Enzyme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM