【发布时间】:2020-01-24 17:07:43
【问题描述】:
我正在使用 react-testing-library 和 jest-styled-components 进行测试。
我有一个包装器组件,它根据传递给它的选定道具呈现其子按钮的样式。
这是代码:
const selectedStyles = css`
background-image: url(../image);
background-position: center;
background-repeat: no-repeat;
border-color: ${color.grey6};
height: 38px;
width: 58px;
& span {
display: none;
}
`;
const ButtonWrapper = styled.div`
& button {
font-size: 15px;
line-height: 20px;
padding: 8px 12px;
${props =>
props.selected
? css`
${selectedStyles}
`
: ""}
&:hover,
:focus {
${props =>
props.selected
? css`
${selectedStyles}
`
: ""}
}
}
`;
和测试
test("it renders the correct styles when selected ", () => {
const { container } = render(<CheckButton selected>Add</CheckButton>);
const button = container.querySelector("button");
expect(button).toHaveStyleRule("background-position", "center");
});
但它与 "Property 'background-position' not found in style rules" 失败,这对于原始按钮是正确的,但是当它的父级传递选定的道具时,此样式适用。
我也在对组件进行快照测试,但是不测试通过的道具会降低测试覆盖率。
谁能帮忙?
【问题讨论】:
标签: jestjs styled-components react-testing-library