我正在编写一个测试来满足以下业务规则:
如果我们
Canada
从国家下拉列表中选择,如果省和邮政编码字段为空,则在模糊上显示错误消息。
我正在测试的 React 最终形式是:
<Form
onSubmit={onSubmit}
validate={values => {
const errors: ValidationErrors = {};
if (!values.country) {
errors.country = "Country must be selected";
}
if (values.country === "Canada" && !values.province) {
errors.province = "Province must be provided";
}
if (values.country === "Canada" && !values.postalCode) {
errors.postalCode = "Postal code must be provided";
}
return errors;
}}
render={({
...
}) => (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="country">Country</label>
<br />
<Field<string> id="country" name="country" component="select">
<option />
<option value="Canada">Canada</option>
<option value="US">US</option>
</Field>
</div>
<Condition when="country" is="Canada">
<div>
<label htmlFor="province">Province</label>
<br />
<Field id="province" name="province" component="input" />
<Error name="province" />
</div>
<div>
<label htmlFor="postalCode">Postal Code</label>
<br />
<Field id="postalCode" name="postalCode" component="input" />
<Error name="postalCode" />
</div>
</Condition>
...
</form>
)}
我正在编写的测试如下所示:
describe("Contact form", () => {
test("should show errors if Canada is seledted but province and postal code are blank", async () => {
const { getByLabelText, findByRole } = render(<ContactForm />);
fireEvent.change(getByLabelText("Country"), {
target: { value: "Canada" }
});
fireEvent.change(getByLabelText("Province"), {
target: { value: "" }
});
fireEvent.change(getByLabelText("Postal Code"), {
target: { value: "" }
});
fireEvent.blur(getByLabelText("Postal Code"));
const postalAlert = await findByRole("alert", {
name: "Postal code must be provided"
});
expect(postalAlert).toBeInTheDocument();
const provinceAlert = await findByRole("alert", {
name: "Province must be provided"
});
expect(provinceAlert).toBeInTheDocument();
我正在尝试触发错误消息-它会被渲染出来role="alert"
-但我的测试失败了:Unable to find role="alert"
现在,如果我删除name
我试图过滤的属性,alert
则会找到:
const postalAlert = await findByRole("alert"); // SUCCESS
我可以检索警报findAllByRole
并对其进行迭代,但我真的只想用可访问的名称显式查询每个警报,并断言它们在文档中。
我在调试屏幕时看到了元素,我只想弄清楚如何使用它的角色和名称直接查询它:
<span
role="alert"
style="color: rgb(153, 0, 0);"
>
Postal code must be provided
</span>
示例表格:https ://codesandbox.io/s/react-ts-unit-test-example-6scjc?file=/src/ContactForm.test.tsx