F
F
fif2022-04-10 16:24:22
typescript
fif, 2022-04-10 16:24:22

How to get a button from a child component in tests?

A simple test for the presence of a button by its text.

test('render search button', async () => {
  const { getByText } = render(<Home />);

  const searchButton = getByText('Search');
  expect(searchButton).toBeInTheDocument();
});


But this test fails, as I understand it, this is why:
This button that I want to find is in the Search component , which I like this
<form onSubmit={this.submitSearching.bind(this)} className="search-input">
            <Search />
</form>

I use, here is the button itself from Search :
<button onClick={this.buttonClick.bind(this)} type="submit" className="input-btn">
          Search
</button>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question