admin管理员组文章数量:1314564
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 badges2 Answers
Reset to default 8You 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();
本文标签: javascriptHow can I test an onClick line of a React component using JestStack Overflow
版权声明:本文标题:javascript - How can I test an onClick line of a React component using Jest? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741966405a2407572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论