我有一个使用 Jest 和 React 测试库的单元测试来填充和提交表单。问题是在将 Material UI 升级到版本 4 后,我的单元测试无法选择一个选项。错误是:“无法找到带有文本的元素:巴西”巴西是我尝试选择的文本选项。使用 Material UI 版本 3 工作得很好。
测试代码 - 给出错误:“无法找到带有文本的元素:巴西。”
fireEvent.click(getByTestId("id-country"));
const countryOption = await waitForElement(() => getByText("Brazil"));
fireEvent.click(countryOption);
react组件代码
<Grid item xs={12} sm={4}>
<TextField
id="select-country"
name="country"
select
helperText={touched.country ? errors.country : ""}
error={touched.country && Boolean(errors.country)}
required
label="Country"
onChange={handleChange}
value={values.country}
className={classes.selectField}
SelectProps={{
SelectDisplayProps: {
"data-testid": "id-country"
}
}}
>
{countryEnum.map(country => (
<MenuItem key={country.type} value={country.type}>
{country.label}
</MenuItem>
))}
</TextField>
</Grid>