javascript - React 测试库:测试失败样式(从 MUI 中选择 + 表单控件)
问题描述
问题
如果组件处于错误状态,我正在尝试测试边框是否为红色,但它在本地机器中失败,但在代码和框中没有
我试过的:
FormHelperText
检查样式和fieldSet
样式是否有区别- 试图通过代码沙箱复制
- 阅读文档和研究发现了一个类似的案例,但使用了不同的样式:https ://twitter.com/hieptuanle5/status/1051811353176858625
错误状态设置为的代码组件true
import React from "react";
import "./styles.css";
import FormControl from "@material-ui/core/FormControl";
import FormHelperText from "@material-ui/core/FormHelperText";
import Select from "@material-ui/core/Select";
export const SelectInput = () => {
return (
<FormControl error={true} variant="outlined">
<Select />
<FormHelperText> test </FormHelperText>
</FormControl>
);
};
测试
import { render, screen, waitFor } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import { SelectInput } from "../App";
test("test form control error state", async () => {
render(<SelectInput />);
const fieldSet = screen.getByRole("group", { hidden: true });
const helperText = screen.getByText("test");
await waitFor(() => expect(fieldSet).toBeInTheDocument());
expect(helperText).toHaveStyle(`color: #f44336;`);
expect(fieldSet).toHaveStyle(`border-color: #f44336;`);
});
codesanbox链接 https://codesandbox.io/s/rtl-mui-select-test-style-border-tekgl?file=/src/test /select.test.js:0-549
编辑 2 本地机器错误日志:
● should accept error string props
expect(element).toHaveStyle()
- Expected
- border-color: #f44336;
+ border-color: rgba(0, 0, 0, 0.23);
> 97 | expect(fieldSet).toHaveStyle(`border-color: #f44336;`)
任何线索将不胜感激。谢谢
解决方案
推荐阅读
- c# - 为什么我的 RLE 的最后一个字符不显示?
- sql - 如何在元组中选择最旧的值
- java - ArrayLists 数组的未检查或不安全操作错误
- javascript - Vuex:根据已获取的数据使用 axios 获取数据
- javascript - RxJS 5,Angular:如何每 x 秒执行一次请求,直到消息匹配某个字符串?
- python - 熊猫:从其他列的值填充新列
- sql - 谷歌表格查询 - 为没有记录数据的日期填充零
- javascript - 如何在 JavaScript 中将字符串分成三部分
- r - 编写一个函数来检查一个字符串是否符合十六进制表示法
- marklogic - Marklogic - 将属性值查询限制为特定集合