React测试库仅适用于内联样式
我正在尝试测试元素是否具有适当的样式。但是,这只有在我将内联样式添加到元素中时才起作用。我还使用CSS模块,所以我不确定这是否是问题。 测试
test("3.1.2.if the nav link opens properly", () => {
render(
<Router>
<Header />
</Router>
);
const nav = screen.getByRole("navigation");
expect(nav.style).toHaveProperty("transform", "rotate(0)")
});
(相关)JSX
<div
className={`${styles.options} ${
dropdownOpened ? styles.options__dropdown__open : ""
}`}
onClick={handleDropdown}
role="navigation"
>
<IconContext.Provider
value={{
color: "white",
className: styles.options__dropdown__btn,
}}
>
<IoMdArrowDropdown />
</IconContext.Provider>
</div>
(相关)CSS
.options:not(.options__menu__opened, .options__dropdown__open) {
transform: rotate(0);
}
I'm trying to test if an element has the proper styles. However this only works when I add inline styling to the element. I'm also using css modules, so I'm not sure if that is the problem.
The test
test("3.1.2.if the nav link opens properly", () => {
render(
<Router>
<Header />
</Router>
);
const nav = screen.getByRole("navigation");
expect(nav.style).toHaveProperty("transform", "rotate(0)")
});
(Relevant) JSX
<div
className={`${styles.options} ${
dropdownOpened ? styles.options__dropdown__open : ""
}`}
onClick={handleDropdown}
role="navigation"
>
<IconContext.Provider
value={{
color: "white",
className: styles.options__dropdown__btn,
}}
>
<IoMdArrowDropdown />
</IconContext.Provider>
</div>
(Relevant) CSS
.options:not(.options__menu__opened, .options__dropdown__open) {
transform: rotate(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 window> window.getComputedStylel ,另请参见<,另请参见< a href =“ https://stackoverflow.com/questions/66912897/testing-library-dom-window-getcomputed-not-implented-not-implemend--eror-even-after”>@testing-testing-library/dom dom window.getComputedEdstyle“未实现”即使在设置ComputedStyleSupportsPseudoelements之后:True
You may try to use window.getComputedStyle, see also @testing-library/dom window.getComputedStyle "Not implemented" error even after setting computedStyleSupportsPseudoElements: true