代码之家  ›  专栏  ›  技术社区  ›  Alp

测试:用jest/酶寻找风格化的情感成分

  •  1
  • Alp  · 技术社区  · 6 年前

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

    在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"]')
    

    有更好的办法吗?谢谢你的阅读!

    0 回复  |  直到 6 年前
        1
  •  1
  •   Raffaele Abramini    6 年前

    通过设置样式化组件的 displayName 当你定义它的时候。

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

    这将允许您在测试过程中使用首字母查找:

    $component.find('StyledComponent');
    

    我希望这能有帮助。