2

我将一个项目从迷人转移到了情感。唯一缺失的拼图?测试。

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

$component.find('StyledComponent');

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

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

import StyledComponent from 'my/ui/StyledComponent';

$component.find(StyledComponent);

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

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

$component.find('div[className*="StyledComponent"]');

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

有没有更好的办法?谢谢阅读!

4

1 回答 1

1

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

const StyledComponent = styled('div')` // ... `;
StyledComponent.displayName = 'StyledComponent';

这将允许您在测试期间使用初始值找到:

$component.find('StyledComponent');

我希望这有帮助。

于 2019-03-01T11:52:07.077 回答