admin管理员组文章数量:1390555
I have a simple Icon
ponent that accepts a onClick()
prop which is called when clicked on the icon. Additionally each time the icon is clicked another function event.stopPropagation()
is called. This function is a property of the actual click event fired by the icon (=represents a basic span).
Now I want to check two things:
- The
onClick
prop function should be called. - The
stopPropagation
callback passed via the event should be called.
Previously I was using enzyme to test which worked perfectly fine.
test('Icon should call the callback on when space is pressed', () => {
const onClick = jest.fn();
const stopPropagation = jest.fn();
const icon = shallow(<Icon className="test" name="su-pen" onClick={onClick} />);
icon.simulate('keypress', {key: ' ', stopPropagation});
expect(onClick).toBeCalled();
expect(stopPropagation).toBeCalled();
});
Now I want to migrate this to React Testing Library. I have tried it with fireEvent
but stopPropagation()
doesn't get called.
test('Icon should call the callback on click', () => {
const onClick = jest.fn();
const stopPropagation = jest.fn();
render(<Icon className="test" name="su-pen" onClick={onClick} />);
const icon = screen.queryByLabelText('su-pen');
fireEvent.click(icon, {stopPropagation});
expect(onClick).toBeCalled();
expect(stopPropagation).toBeCalled();
// ^ --> failed
// Expected number of calls: >= 1
// Received number of calls: 0
});
I have a simple Icon
ponent that accepts a onClick()
prop which is called when clicked on the icon. Additionally each time the icon is clicked another function event.stopPropagation()
is called. This function is a property of the actual click event fired by the icon (=represents a basic span).
Now I want to check two things:
- The
onClick
prop function should be called. - The
stopPropagation
callback passed via the event should be called.
Previously I was using enzyme to test which worked perfectly fine.
test('Icon should call the callback on when space is pressed', () => {
const onClick = jest.fn();
const stopPropagation = jest.fn();
const icon = shallow(<Icon className="test" name="su-pen" onClick={onClick} />);
icon.simulate('keypress', {key: ' ', stopPropagation});
expect(onClick).toBeCalled();
expect(stopPropagation).toBeCalled();
});
Now I want to migrate this to React Testing Library. I have tried it with fireEvent
but stopPropagation()
doesn't get called.
test('Icon should call the callback on click', () => {
const onClick = jest.fn();
const stopPropagation = jest.fn();
render(<Icon className="test" name="su-pen" onClick={onClick} />);
const icon = screen.queryByLabelText('su-pen');
fireEvent.click(icon, {stopPropagation});
expect(onClick).toBeCalled();
expect(stopPropagation).toBeCalled();
// ^ --> failed
// Expected number of calls: >= 1
// Received number of calls: 0
});
Share
Improve this question
edited Dec 4, 2022 at 11:47
Behemoth
asked Aug 1, 2022 at 10:15
BehemothBehemoth
9,4005 gold badges27 silver badges48 bronze badges
2
-
You simulate the
keypress
event when using enzyme, but you fire click event when using RTL. They are different events. – Lin Du Commented Aug 2, 2022 at 2:24 - That's right I might have formulated the Question unfortunately. – Behemoth Commented Aug 2, 2022 at 5:31
1 Answer
Reset to default 7You are testing the internals of the ponent this way, not its behavior.
I would rather wrap that in a dummy element with an onclick handler, and check that that is not called when clicking the icon:
test('Icon should not propagate the click event', () => {
const onClick = jest.fn();
const onOuterClick = jest.fn();
render(
<div onClick={onOuterClick}>
<Icon className="test" name="su-pen" onClick={onClick} />
</div>
);
const icon = screen.queryByLabelText('su-pen');
fireEvent.click(icon);
expect(onClick).toHaveBeenCalledTimes(1);
expect(onOuterClick).toHaveBeenCalledTimes(0);
});
本文标签: javascriptHow can I test stopPropagation of a click event using React Testing LibraryStack Overflow
版权声明:本文标题:javascript - How can I test stopPropagation of a click event using React Testing Library? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744703022a2620672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论