【发布时间】:2021-01-15 23:20:46
【问题描述】:
Material UI 的Button 组件呈现一个相对简单的按钮元素,其背景颜色可以在网络检查器中看到。
但是当我们在 testing-library/react 渲染中使用 getComputedStyle 检查时,报告的颜色是“透明的”。
有没有办法测试按钮的背景颜色?
例如,如果一个组件返回:
<Button data-testid="blah" variant="contained" color="primary">
hi
</Button>
它会创建一个带有默认主题的蓝色按钮 (#3f51b5)。但是这个测试:
it("renders with the correct background color", () => {
const { getAllByTestId } = render(<App />);
const button = getAllByTestId("blah")[0];
const buttonStyle = window.getComputedStyle(button);
const backgroundColor = buttonStyle["background-color"];
expect(backgroundColor).toEqual("#3f51b5");
});
失败
Expected: "#3f51b5"
Received: "transparent"
【问题讨论】:
标签: material-ui react-testing-library