0

I can render my component properly and get the input tag by using .getByPlaceholderText. The problem is that after I use fireEvent to change the input value, I am not able to get the same input by using findByText. This is not the test I'm going to use; I just want to understand why this is not working.

    it('displays the clearIcon by default', async () => {
      // Render my component
      render(<SearchInput {...defaultProps} />)
      // find the input html element by placeholder - it works
      const inputNode = await screen.findByPlaceholderText(/search/i)
      // fire an event where I change the value of the input to "Text"
      fireEvent.change(inputNode, { target: { value: 'Text' } })
      // find the same element searching by the text I added in it - "Text"
      // this fails saying that it's "unable to find an element with the text /Text/i"
      const sameInput = await screen.findByText(/Text/i)
      expect(sameInput).toBeInTheDocument()
    })

1 Answer 1

1

The problem was that I was trying to test a nested component, and the states for the input value are in the wrapper component. Moving my tests to the wrapping component solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.