有没有办法在react-testing-library中使用fireEvent在'span'上触发点击事件?

时间:2019-05-15 15:48:58

标签: javascript antd react-testing-library

我正在使用ant Design选择组件来创建我的自定义选择。我正在尝试测试(使用react-testing-library)是否在单击标记内的“ x”图标后正确删除了所选元素。不幸的是在测试元素没有被删除。我认为这是'remove'元素的问题,因为它不是按钮,而是跨度(我无法更改它,因为它内置在ant design select组件中的某个位置)

我尝试使用click事件,也使用鼠标向下和向上事件,但似乎不起作用。


const renderMultiselect = () => {
  const { container } = render(
    <I18nextProvider i18n={i18n}>
      <Multiselect
        items={items}
        optionKey="code"
        optionName="name"
        selected={selectedItems}
        testId="types"
        testIdOptionKey="code"
        label="Types"
        onChange={onChange}
      />
    </I18nextProvider>,
  );
  return container;
};

describe('Multiselect', () => {
  it('allows to remove elements', () => {
    const container = renderMultiselect();
    const selected = container.querySelector('.ant-select-selection__rendered ul');
    const removeIcon = selected.firstChild.querySelector('.ant-select-selection__choice__remove');
    fireEvent.mouseDown(removeIcon);
    fireEvent.mouseUp(removeIcon);
    //also tried: fireEvent.click(removeIcon)
  });
});

//that's how the remove 'button' looks inside the DOM
<span class="ant-select-selection__choice__remove">
   <i aria-label="icon: close" class="anticon anticon-close ant-select-remove-icon">
      <svg viewBox="64 64 896 896" class="" data-icon="close" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false">
         <path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 0 0 203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path>
      </svg>
   </i>
</span>

预期结果是触发事件,该事件将从选定项目中删除multiselect中的第一个元素。 这是Ant Design Select组件的外观: https://codepen.io/pen/?&editable=true&editors=001

0 个答案:

没有答案