reactjs - FireEvent.click TypeError:无法读取未定义的属性“单击”
问题描述
我正在使用 React 测试库,并且我有一个嵌套在一堆圆顶元素中的标签。我上面有一个测试 ID,我可以看到它使用正确的节点进行调试。但是当我运行我的测试时。
import { render, cleanup } from '@testing-library/react';
test('bla', () => {
const { fireEvent, getByTestId, debug } = render(
<Group>
<Menu items={items} />
</Group>
);
const viewMore = getByTestId('button');
console.log('viewMore = ', viewMore);
// debug(viewMore);
fireEvent.click(viewMore, {});
});
我收到以下错误。
TypeError: Cannot read property 'click' of undefined
为什么这会告诉我无法读取属性“点击”?
我的带有按钮类的标签位于我的菜单组件内,该组件在 Group 内呈现。
这是viewMore返回的精简版本。
viewMore = HTMLAnchorElement {
'__reactInternalInstance$6ct8li9ulz6':
FiberNode {
tag: 5,
key: null,
elementType: 'a',
type: 'a',
stateNode: [Circular],
return:
FiberNode {
tag: 5,
key: null,
elementType: 'div',
type: 'div'},
child: null,
sibling: null,
index: 1,
ref: null,
pendingProps:
{ href: '#',
className: 'button',
'data-testid': 'button',
onClick: [Function: action],
children: 'VIEW MORE...' },
memoizedProps:
{ href: '#',
className: 'button',
'data-testid': 'button',
onClick: [Function: action],
children: 'VIEW MORE...' }
'__reactEventHandlers$6ct8li9ulz6':
{ href: '#',
className: 'a-tag',
'data-testid': 'button',
onClick: [Function: action],
children: 'VIEW MORE...' } }
解决方案
fireEvent
不是来自render
您需要从 RTL 导入它:
import { fireEvent } from '@testing-library/react'
推荐阅读
- java - 剩余参数的计数
- authentication - 如何使用 supbase.io 实现匿名授权?
- ios - 如何记录视图加载所需的时间
- typescript - 在 Typescript 中,如何在初始化后将类型化值转换为文字值?
- javascript - VS 代码,没有使用 javascript 的自动完成功能 (discord.js)
- python - python - 如何以最有效的方式完成这个特殊的求和
- azure - 如何更改 Azure 中 VMSS 的分析工作区?
- python-3.x - Tkinter 按钮需要在同一个网格中创建多次,但不需要,我如何让它做到这一点?
- elasticsearch - 微服务:与多个实例的服务间通信
- swift - 修改swiftui数组值