使用带有酶的期望测试库无法在DOM上找到具有两个类的元素

在测试React-Redux组件时,是否可以通过第二个类名aur或具有两个或多个类名的元素来查找元素。

例如:  我有一个div

<div classname="navigations active">
....
</div>

尽管我能够通过班级名找到它

const wrapper = mount(
   <Provider store={store}>
       <TodoApp />
   </Provider>
);

expect(wrapper.find('.navigations ')).toHaveLength(4);

但无法通过二级名称找到它,例如:

expect(wrapper.find('.active')).toHaveLength(1);

它总是使我的长度为零,但活动类已附加到DOM。 我只想知道是否可以访问它?

carol548 回答:使用带有酶的期望测试库无法在DOM上找到具有两个类的元素

您应该可以同时做这两项...我已经创建了快速测试来检查您的示例:

import React from 'react';
import { mount } from 'enzyme';

const MyComp = ({active}) => <div className={`navigations${active ? ' active': ''}`} />;

describe('Name of the group',() => {
  it('should ',() => {
    const wrapper = mount(
      <>
        <MyComp />
        <MyComp active/>
        <MyComp />
        <MyComp />
      </>,);

    expect(wrapper.find('.navigations')).toHaveLength(4);
    expect(wrapper.find('.active')).toHaveLength(1);
  });
});
本文链接:https://www.f2er.com/3160529.html

大家都在问