react-testing-library-fireEvent.select()不起作用

Zac*_*ach 5 javascript reactjs jestjs react-testing-library

我目前正在为我正在从事的项目运行一些测试,但在使用fireEvent.select()这种方法专注于输入字段时遇到了麻烦。

到目前为止,我的测试:

it('is not working :(', () => {
  const input = queryByPlaceholderText('blah');

  fireEvent.change(input, {
    target: {value: 'some text'},
  });

  expect(input).toHaveAttribute('value', 'some text');

  fireEvent.select(input); <-- issue here
});
Run Code Online (Sandbox Code Playgroud)

我正在测试的组件具有一个下拉菜单,该菜单仅在输入集中时才显示,但似乎fireEvent.change()也不fireEvent.select()在现场。我知道这会fireEvent.change()改变输入值。

到目前为止,我已经尝试过:

  • fireEvent.click()
  • fireEvent.focus()
  • fireEvent.select()
  • input.focus()

但这些选项均无效。

我需要能够在此下拉菜单中选择一个选项,以便能够正确测试组件。

TL; DR

有没有一种方法可以专注于RTL的输入字段?

the*_*kes 3

为了让它与 React-Select 一起使用,我必须使用 ReactDOM 而不是fireEvent. 然后我可以react-testing-library像平常一样测试/运行断言。

import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';

export const openDropdown = ($container) => {
  // Opens the dropdown
  // eslint-disable-next-line react/no-find-dom-node
  selectOption(ReactDOM.findDOMNode($container).querySelector('.Select-arrow'));
};

export const selectOption = ($container) => {
  // Selects an option from the dropdown
  TestUtils.Simulate.mouseDown($container, { button: 0 });
};
Run Code Online (Sandbox Code Playgroud)
    const $fooSelect = await waitForElement(() => app.getByTestId('foo-select'));
    openDropdown($fooSelect);
    const $fooElement = await waitForElement(() => app.getByText(fooFixture[0].name));

    selectOption($fooElement);

    await wait()
    // do stuff...
Run Code Online (Sandbox Code Playgroud)