reactjs - Enzyme/Jest spy 未调用 onClick

标签 reactjs testing jestjs enzyme

我正在尝试使用 Enzyme/Jest 为 react 组件测试事件处理程序,但是我的 spy 函数从未被调用...

我的组件有一个带有 id 的 div,我用它来查找 dom 元素

 render() {
    return (
        <div>
            <Top
                {...this.props}
            />
            <div 
                id = 'keyboard_clickable'
                onClick = {this._handleClick}
                style= {styles.main}>
                {this._showKeyBoard()}
            </div>
            <input onChange = {() => {}}/>
      </div>
    );
  }
}

我的测试

describe('onclick function is called ...', () => {
  it.only('spyOn', () => {
    const spy = jest.fn()

    const wrapper = shallow(
      <Keyboard 
      _getData      = { () => {} }
      _erase        = { () => {} }
      _get_letters  = { () => {} }
      _createWord   = { () => {} }
      _modeSwitch   = { () => {} }
      onClick       = { spy      }
      />
    )

    wrapper.find('#keyboard_clickable').simulate('click', {
      target:{
        parentElement:{ id: 5 },
        id:6
        }
    })
    expect(spy).toHaveBeenCalled();

  })
})

我的 HandleClick

_handleClick = e => {
    let parent = e.target.parentElement;
    if(e.target.id || (!isNaN(parent.id) && parent.id > 0) ) {
        this.props._getData(e.target.id || e.target.parentElement.id)
        this.setState({status:'ok'})
    }else{
        this.setState({status:'Use numbers between 2 and 9'},()=>{
            return alert(this.state.status)
        })

    }
}

测试输出

Expected mock function to have been called, but it was not called.

最佳答案

要测试您的事件处理程序是否被调用,您必须将事件处理程序替换为模拟函数。一种方法是扩展您的组件类:

class TestKeyboard extends Keyboard {
  constructor(props) {
    super(props)
    this._handleClick = this.props._handleClick
  }
}

describe('onclick function is called ...', () => {
  it.only('spyOn', () => {
    const spy = jest.fn()

    const wrapper = shallow(
      <TestKeyboard 
      _getData      = { () => {} }
      _erase        = { () => {} }
      _get_letters  = { () => {} }
      _createWord   = { () => {} }
      _modeSwitch   = { () => {} }
      _handleClick  = { spy      }
      />
    )

    wrapper.find('#keyboard_clickable').simulate('click', {
      target:{
        parentElement:{ id: 5 },
        id:6
        }
    })
    expect(spy).toHaveBeenCalled();

  })
})

Prop _handleClick 替换了 Keyboard._handleClick,因此当 onClick 在被点击的元素中触发时被调用。

或者,jest.spyOn

如果您需要让事件处理程序执行测试调用它的内容,您可以使用jest.spyOn。我发现这种方法更复杂,但更灵活。

import { mount } from 'enzyme'

describe('onclick function is called ...', () => {
  it.only('spyOn', () => {
    const wrapper = mount(
      <Keyboard 
      _getData      = { () => {} }
      _erase        = { () => {} }
      _get_letters  = { () => {} }
      _createWord   = { () => {} }
      _modeSwitch   = { () => {} }
      />
    )

    const spy = jest.spyOn(wrapper.instance(), '_handleClick')
    wrapper.instance().forceUpdate()

    wrapper.find('#keyboard_clickable').simulate('click', {
      target:{
        parentElement:{ id: 5 },
        id:6
        }
    })
    expect(spy).toHaveBeenCalled();

  })
})

请注意,这在使用浅层渲染时会失败,因此您必须改用 enzyme.mount。

关于reactjs - Enzyme/Jest spy 未调用 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51684072/

相关文章:

javascript - 单击时将类添加到 ReactJS 中的多个对象

javascript - ReactJS 中 <select> 标签的 onChange 事件的替代方案?

unit-testing - 将 POST 变量添加到 Go 测试 http 请求

react-native - 无法从 'react-dom' 找到模块 'mobxreact.cjs.development.js'

reactjs - 在 componentDidMount 中获取时如何测试 react 组件?

javascript - React 表单 onChange 处理程序设置错误键的状态

testing - 如何使用 Web 浏览器测试 Spring MVC 的 HTTP POST?

Maven tomcat 插件挂起

javascript - 单元测试 Controller 使用 Jest、NodeJS

javascript - 表单正在清除空白输入上一组数据