Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用Jest的快照测试内部函数_Javascript_Reactjs_Jestjs - Fatal编程技术网

Javascript 用Jest的快照测试内部函数

Javascript 用Jest的快照测试内部函数,javascript,reactjs,jestjs,Javascript,Reactjs,Jestjs,我想知道是否有一种方法可以使用Jest的快照测试道具中没有的内部组件函数 对于该组件: class Comp extends Component { state = { ... } handleSomething = () => { doSomething() } render() { return ( <div> { this.props.foo } <ChildComp onC

我想知道是否有一种方法可以使用Jest的快照测试道具中没有的内部组件函数

对于该组件:

class Comp extends Component {
  state = {
    ...
  }

  handleSomething = () => {
    doSomething()
  }

  render() {
    return (
      <div>
        { this.props.foo }
        <ChildComp onClick={ this.handleSomething } />
      </div>
    )
  }
}
export default Comp
我正在尝试这样做:

test('Comp', () => {
  const component = renderer.create(
    <Comp foo="bar" />
  )

  let tree = component.toJSON()
  expect(tree).toMatchSnapshot()

  tree.props.handleSomething() // TypeError: tree.props.handleSomething is not a function
  tree = component.toJSON()
  expect(tree).toMatchSnapshot()
})
但是我得到了类型错误,因为handleSomething不是组件道具

有没有办法使用快照测试此功能?

您可以使用快照渲染组件并触发单击ChildComp

请注意,您需要使快照功能正常工作

import { shallow } from 'enzyme'

test('Comp', () => {
  const component = shallow(
    <Comp foo="bar" />
  )

  component.find('ChildComp').simulate('click')
  expect(component).toMatchSnapshot()
})