最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I test an onClick line of a React component using Jest? - Stack Overflow

programmeradmin2浏览0评论

I am learning React and I am currently trying Jest/testing. I am starting tests on a small project and I want to get 100% code coverage. Here's what I have.

ponent:

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

tests:

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');
    })

})

The line indicated as "uncovered" is

onClick={() => props.onClick()}

What is the best way to test this line? Any remendations?

I am learning React and I am currently trying Jest/testing. I am starting tests on a small project and I want to get 100% code coverage. Here's what I have.

ponent:

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

tests:

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');
    })

})

The line indicated as "uncovered" is

onClick={() => props.onClick()}

What is the best way to test this line? Any remendations?

Share Improve this question asked Jun 28, 2019 at 21:43 Garrett Daniel DeMeyerGarrett Daniel DeMeyer 9312 gold badges13 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You would use a Mock Function

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);
});

Also, for what it's worth, in your ponent you can assign the onClick handler straight to the prop i.e.

<button
  className={className}
  onClick={props.onClick}
>

Simply target the element and call its handler:

tree.root.findByType('button').props.onClick();
发布评论

评论列表(0)

  1. 暂无评论