简体   繁体   English

反应测试。 如何测试反应组件内的功能

[英]React Test. how to test function inside react component

What is the proper way to test a method within a react component, in this case componentDidMount.在反应组件中测试方法的正确方法是什么,在这种情况下是 componentDidMount。 I want to test the setTimeOut function inside the compoenent.我想测试组件内的 setTimeOut 函数。 Should I use stub?我应该使用存根吗? For example the code below:例如下面的代码:

 componentDidMount() {
        setTimeout(() => this.setState({ isOpen: true }), 1);
  }

How can I test the setTimeout being called?如何测试被调用的 setTimeout?

I tried with the following and didn't work.我尝试了以下方法但没有奏效。 What am I missing?我错过了什么?

my imports:

import test from 'ava';
import React from 'react';
import { ad } from 'components/Ad/Ad';
import { shallow, mount } from 'enzyme';
import { stub } from 'sinon';
import { expect } from 'chai';
import  sinon from 'sinon';

let info;

test.beforeEach(() => {

  info = shallow(<ad {...props} />)
});

test('is active: true after mounting', done => {
  info.instance().componentDidMount()
  setTimeout(() => {
    info.state('active').should.be.true  <--line:42
    done()
  }, 0)
})

I get the following error: TypeError: Cannot read property 'be' of undefined null._onTimeout (test/Components/Ad.unit.js:42:5)我收到以下错误:TypeError:无法读取未定义的 null._onTimeout 属性 'be' (test/Components/Ad.unit.js:42:5)

Here's an example that uses mocha, chai, and enzyme:下面是一个使用 mocha、chai 和酶的示例:

The component:组件:

import React, {PropTypes as T} from 'react'
import classnames from 'classnames'

export default class FadeIn extends React.Component {
  constructor(...args) {
    super(...args)
    this.state = {active: false}
  }

  componentDidMount() {
    setTimeout(() => this.setState({active: true}), 0)
  }

  render() {
    const {active} = this.state
    return (
      <div className={classnames('fade-in', {active}, this.props.className)}>
        {this.props.children}
      </div>
    )
  }
}

FadeIn.propTypes = {
  className: T.string
}

FadeIn.displayName = 'FadeIn'

The test:考试:

import React from 'react'
import {shallow} from 'enzyme'
import FadeIn from '../../src/components/FadeIn'

describe('FadeIn', () => {
  let component

  beforeEach(() => {
    component = shallow(<FadeIn/>)
  })

  it('is initially active: false', () => {
    component.state('active').should.be.false
    component.find('div.fade-in').prop('className').should.equal('fade-in')
  })

  it('is active: true after mounting', done => {
    component.instance().componentDidMount()
    setTimeout(() => {
      component.state('active').should.be.true
      component.find('div.fade-in').prop('className').should.equal('fade-in active')
      done()
    }, 0)
  })

})

The Mocha framework has support for asynchronous tests and allows you to use setTimeout in your test as well. Mocha 框架支持异步测试,并允许您在测试中使用setTimeout Then you make your assertions within the asynchronous callback.然后在异步回调中进行断言。

describe('test component', function() {
    it('should have isOpen true', function(done) {
        // *** setup your component here *** 
        console.log('waiting 3 seconds...');
        setTimeout(function () {
            console.log('waiting over.')
            // ** assert component state.isOpen == true **
            done(); // callback to indicate that test is over
        }, 3000);
    });

How can I use setTimeout() functions within Mocha test cases? 如何在 Mocha 测试用例中使用 setTimeout() 函数?

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

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