Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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测试React组件的onClick行?_Javascript_Reactjs_Unit Testing_Testing_Jestjs - Fatal编程技术网

Javascript 如何使用Jest测试React组件的onClick行?

Javascript 如何使用Jest测试React组件的onClick行?,javascript,reactjs,unit-testing,testing,jestjs,Javascript,Reactjs,Unit Testing,Testing,Jestjs,我正在学习React,目前正在尝试开玩笑/测试。我正在一个小项目上开始测试,我希望获得100%的代码覆盖率。这是我的 组成部分: import React from 'react'; function Square(props) { const className = props.isWinningSquare ? "square winning-square" : "square"; return ( <button

我正在学习React,目前正在尝试开玩笑/测试。我正在一个小项目上开始测试,我希望获得100%的代码覆盖率。这是我的

组成部分:

import React from 'react';

function Square(props) {
    const className = props.isWinningSquare ?
        "square winning-square" :
        "square";
    return (
        <button
            className={className}
            onClick={() => props.onClick()}
        >
            {props.value}
        </button>
    );
}

export default Square

测试这条线的最佳方法是什么?有什么建议吗?

只需针对元素并调用其处理程序:

tree.root.findByType('button').props.onClick();
您将使用


这很有帮助,谢谢。谢谢你给我额外的小费!
import React from 'react';
import Square from '../square';
import {create} from 'react-test-renderer';

describe('Square Simple Snapshot Test', () => {
    test('Testing square', () => {
        let tree = create(<Square />);
        expect(tree.toJSON()).toMatchSnapshot();
    })
})

describe('Square className is affected by isWinningSquare prop', () => {
    test('props.isWinningSquare is false, className should be "square"', () =>{
        let tree = create(<Square isWinningSquare={false} />);

        expect(tree.root.findByType('button').props.className).toEqual('square');
    }),
    test('props.isWinningSquare is true, className should be "square winning-square"', () =>{
        let tree = create(<Square isWinningSquare={true} />);

        expect(tree.root.findByType('button').props.className).toEqual('square winning-square');
    })

})
onClick={() => props.onClick()}
test('props.onClick is called when button is clicked', () =>{
  const fn = jest.fn();
  let tree = create(<Square onClick={fn} />);
  // Simulate button click
  const button = tree.root.findByType('button'):
  button.props.onClick()
  // Verify callback is invoked
  expect(fn.mock.calls.length).toBe(1);
});
<button
  className={className}
  onClick={props.onClick}
>