如何在 React 测试库中获取 Material-UI 密码输入

use*_*009 3 reactjs material-ui react-testing-library

我在一个项目中使用 Material-UI 和 React 测试库,但我在 TextField 设置为type="password". 我知道,在测试中,默认使用testId字段获取元素不是一个好习惯,但我找不到进入密码输入字段的方法。我无法使用,getByRole因为显然<input type="password">没有作用。输入框中没有文本,也没有占位符文本,当我尝试使用时,getByLabelText我收到此错误消息:

TestingLibraryElementError: Found a label with the text of: Password, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
Run Code Online (Sandbox Code Playgroud)

当我查看测试中渲染的元素时(我使用了screen.debug()),我意识到这就是 Material-UI 组成 TextField 的方式:

TestingLibraryElementError: Found a label with the text of: Password, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
Run Code Online (Sandbox Code Playgroud)

因此,<input>未嵌入在 内,<label>并且标签也没有for将其与输入关联的属性。那么,我还能如何在不使用的情况下获得该密码框testId

die*_*edu 7

我认为您需要将id道具设置TextField为将标签与输入相关联