【问题标题】:how to get the text of anchor tag in jest如何在笑话中获取锚标签的文本
【发布时间】:2021-11-09 21:45:58
【问题描述】:
  <a href={LinkMappingUtility.getMappedWebUiLink('/#/dashboard')}
    data-id="0"
    className="nav-menu-item"
    autoFocus="true">

  {menuItem({ linkTitle: translate('sbDashboard', currentLanguage), icon: 'swn-home-icon' })}

这是我有菜单项文本的锚标记,我想要酶中的文本

我的代码是:

test('所有菜单项显示正确的文字', () => {

    const allMenuItemText = [
        { linkTitle: 'Dashboard', icon: 'swn-home-icon' },
        { linkTitle: 'Alerts', icon: 'swn-alerts-icon' },
        { linkTitle: 'Contact Management', icon: 'swn-home-icon'},
        { linkTitle: 'Reporting', icon: 'swn-home-icon' },
        { linkTitle: 'Solutions', icon: 'swn-home-icon' },
        { linkTitle: 'Integrations', icon: 'swn-home-icon' },
        { linkTitle: 'Account Settings', icon: 'swn-home-icon'},
        { linkTitle: 'Support & Training', icon: 'swn-home-icon' }           
    ];

    wrapper.find('.nav-menu-item').forEach((node, index) => {            
        expect(node.text()).toEqual(allMenuItemText[index].linkTitle);       
    })

输出:这里我得到一个空字符串作为回报

【问题讨论】:

    标签: javascript reactjs jestjs enzyme


    【解决方案1】:

    我猜只是一个错字,你的意思是使用类选择器?

    // tag selector
    wrapper.find('nav-menu-item') ...
    
    // class selector
    wrapper.find('.nav-menu-item') ...
    

    【讨论】:

    • 不,我仍然得到空字符串 text() 方法没有返回值
    • @SurinRanjanPanda 您是否尝试过使用快照来交叉检查锚元素的加载方式?根据对您问题的修改,我觉得我们没有一个最小的、完整的、可验证的示例可供使用。
    【解决方案2】:

    您是否尝试过选择链接标题-在测试库中很难选择锚标记-因此,如果您选择链接标题,则单击它可能会起作用

    const anchorTag = wrapper.find("my-link-title");
    expect(anchorTag).toHaveAttribute("href", "https://url-test-1#");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2011-04-21
      相关资源
      最近更新 更多