react-autosuggest icon indicating copy to clipboard operation
react-autosuggest copied to clipboard

Unable to find the suggestion items during a unit test

Open gazpachu opened this issue 5 years ago • 6 comments

I get a Timed out in waitForElement.

it('calls "onSelect" prop when an item is selected', async () => {
  const onSelect = jest.fn();
  const { getByPlaceholderText, container } = customRender(<AutoComplete placeholder="test" data={data} onSelect={onSelect} />);
  const inputNode = getByPlaceholderText('test');

  // This will write `pete` in the input field
  fireEvent.change(inputNode, { target: { value: 'pete' } });

  const items = await waitForElement(() => {
    container.querySelectorAll('.react-autosuggest__suggestion');
  });

  console.log(items);

  // Fire the click on the first item
  // fireEvent.click(items[0]);
  expect(onSelect).toHaveBeenCalled();
});

More info here: https://spectrum.chat/react-testing-library/help?thread=68e5c2dc-21fa-4b8d-9c6d-3bf2c0613c4a

Any help would be much appreciated.

gazpachu avatar Nov 14 '18 13:11 gazpachu

Actually Readme.md should be expanded on how to (and what to) test using the component. I'm also struggling to check if a change in input causes my mock client to be called. :|

andrastothtw avatar Nov 14 '18 14:11 andrastothtw

Here is sandbox that another user made to show the issue: https://codesandbox.io/s/5yzq54p8yl

gazpachu avatar Nov 14 '18 16:11 gazpachu

if anyone's still interested in this... I got that codepen working by inserting

inputNode.focus()

into line 17 in Autocomplete.test.js 👍

dallin-christensen avatar Mar 15 '19 00:03 dallin-christensen

Legend. Thank you fo much for this. Saved me a lot of time.

TimPerry avatar Sep 01 '19 20:09 TimPerry

inputNode.focus() doesn't work for me :(

toantd90 avatar May 27 '22 06:05 toantd90

Has anyone figured out how to test this using react testing library ? I have the same issue where even after focusing or clicking on the input or the combobox, it just doesn't call the onSuggestionsFetchRequested

EDIT: I was able to make it work using userEvent library in my RTL tests to type into the textbox. Did not need the extra input.focus

gaurav5430 avatar Mar 14 '23 05:03 gaurav5430