首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试:使用Jest/Enzyme查找带有样式的情感组件

测试:使用Jest/Enzyme查找带有样式的情感组件
EN

Stack Overflow用户
提问于 2019-01-12 05:02:32
回答 1查看 771关注 0票数 2

我把一个项目从魅力转移到了情感上。唯一遗失的拼图吗?测试。

在glamorous中,我可以找到具有如下选择器的元素:

代码语言:javascript
复制
$component.find('StyledComponent');

$component.find('StyledComponent[prop="value"]');

这不再起作用了。到目前为止,我发现的最好的方法是:

代码语言:javascript
复制
import StyledComponent from 'my/ui/StyledComponent';

$component.find(StyledComponent);

$component.find(StyledComponent).filter('[prop="value"]');

我喜欢前一种方式,因为它根本不需要导入组件。某些情感组件是在文件中定义的,而不会导出它们。在这些情况下,它将更加冗长:

代码语言:javascript
复制
$component.find('div[className*="StyledComponent"]');

$component.find('div[className*="StyledComponent"][prop="value"]'); // or
$component.find('div[className*="StyledComponent"]').filter('[prop="value"]')

有没有更好的方法?感谢您的阅读!

EN

回答 1

Stack Overflow用户

发布于 2019-03-01 19:52:07

您仍然可以通过在定义样式组件时设置样式组件的displayName来使用第一种方法。

代码语言:javascript
复制
const StyledComponent = styled('div')` // ... `;
StyledComponent.displayName = 'StyledComponent';

这将允许您在使用首字母的测试期间找到:

代码语言:javascript
复制
$component.find('StyledComponent');

我希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54153985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档