用反应钩形式选择的React Mui可以与Cypress一起使用
在我的React应用程序中,我的表格有一个下拉列表选择。根据所选选项,呈现不同的输入。
const [templateType, setTemplateType] = useState("");
const {
register,
formState: { errors },
setValue,
} = useFormContext();
...
<FormControl>
<InputLabel>Template Typ</InputLabel>
<Select id="SelectTemplateType"
className="custom-input"
{...register("templateType", { required: true })}
label="Template Typ"
value={templateType}
onChange={(e) => setTemplateType(e.target.value)}
error={errors.templateType}
>
{TEMPLATE_TYPES.map((option) => (
<MenuItem value={option.value}>{option.label}</MenuItem>
))}
</Select>
{errors.templateType && (
<FormHelperText sx={{ color: "#d32f2f" }}>
Eintrag darf nicht leer sein!
</FormHelperText>
)}
</FormControl>
<TemplateFormSwitch templateType={templateType} />
TemplateFormSwitch根据所选的TemplateType返回不同的形式组件。
我正在使用form-provider和useformContext的react-hook-form,因为我的表单在多个组件/文件上拆分。
我尝试通过首先单击“选择”,然后单击所需的选项来编写柏树测试:
cy.get('#SelectTemplateType').click({ force: true })
cy.get('[data-value="Text"]').click({ force: true });
//Entering Test values to TextFields
cy.get('#mui-1').type("Test");
cy.get('#mui-2').type("HelloWorld");
然后,当我尝试提交我的表单时,所有Textfields都会正确验证。但是以某种方式使用TemplateType选择了选择并未验证,并且提交操作被阻止。
奇怪的是,当我在应用程序中手动单击时,所有内容都可以正常工作,并且TemplateType Select可以正确验证。
我需要做些什么/更改才能正确测试MUI正确选择并触发React-Hook-form-form验证,就像我手动测试一样?
In my react application I have a form with a dropdown select. Depending on the selected Option different inputs are rendered.
const [templateType, setTemplateType] = useState("");
const {
register,
formState: { errors },
setValue,
} = useFormContext();
...
<FormControl>
<InputLabel>Template Typ</InputLabel>
<Select id="SelectTemplateType"
className="custom-input"
{...register("templateType", { required: true })}
label="Template Typ"
value={templateType}
onChange={(e) => setTemplateType(e.target.value)}
error={errors.templateType}
>
{TEMPLATE_TYPES.map((option) => (
<MenuItem value={option.value}>{option.label}</MenuItem>
))}
</Select>
{errors.templateType && (
<FormHelperText sx={{ color: "#d32f2f" }}>
Eintrag darf nicht leer sein!
</FormHelperText>
)}
</FormControl>
<TemplateFormSwitch templateType={templateType} />
The TemplateFormSwitch returns different form-components depending on the selected templateType.
I'm using react-hook-form with FormProvider and useFormContext because my form is split up over multiple components/files.
I tried to write a Cypress-Test, by first clicking the select and then clicking on the desired option:
cy.get('#SelectTemplateType').click({ force: true })
cy.get('[data-value="Text"]').click({ force: true });
//Entering Test values to TextFields
cy.get('#mui-1').type("Test");
cy.get('#mui-2').type("HelloWorld");
Then when I try to submit my form all textfields get validated correctly. But somehow the select with the templateType doesn't validate and the submit action gets blocked.
Weirdly when I click manually in the application everything works fine and the templateType select gets validated correctly.
What do I need to do/change in order to test the MUI select correctly and trigger the react-hook-form validation accordingly, like I would if I test manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时,命令的JavaScript代码使用不足以触发验证检查。
用户将集中精力并模糊每个字段,因此您也可以做到这一点
Sometimes the javascript code use by commands is not enough to trigger validation checks.
The user will focus and blur each field, so you can also do that
我克隆了您的仓库并进行了测试,但是对我来说,没有产生的错误。我运行了确切的脚本。
测试跑步者的屏幕快照:
I cloned your Repo and ran the test, but for me there was no error that was produced. I ran the exact script.
Screenshot of the test runner: