4

I am using react-testing-library for my react app. In one test case, I need to fill some value in a textbox and focusout.

Here is the test script -

it('searchbox wrapper',async()=>{
    let wrapper=getSearchBoxWrapperInstance('')
    let inputBox=wrapper.findByTestId('inputText');
    inputBox.value='12345';
    fireEvent(inputBox,'focusOut');

})

I am getting the following error when I run the test case -

TypeError: element.dispatchEvent is not a function

  79 |     let inputBox=wrapper.findByTestId('inputText');
  80 |     inputBox.value='12345';
> 81 |     fireEvent(inputBox,'focusOut');
     |     ^
  82 |     //fireEvent('Blur',
  83 | 
  84 |     //await (() => wrapper.getByText('')))

  at fireEvent (node_modules/dom-testing-library/dist/events.js:533:18)

Please let me know if I can provide further information

3 Answers 3

8

I'm assuming getSearchBoxWrapperInstance returns the result of render.

Try if this works:

it('searchbox wrapper',async()=>{
  let wrapper=getSearchBoxWrapperInstance('')
  let inputBox=wrapper.findByTestId('inputText');
  fireEvent.change(inputBox, { target:  { value: '12345' } });
  fireEvent.focusOut(inputBox);
  // In alternative you could try fireEvent.blur
})

It's also possible that findByTestId doesn't find your element. Try to log it out to see if that's the case or use getByTestId which errors our if the element is not found.

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

4 Comments

I am getting this error now - "The given element does not have a value setter", at this line - fireEvent.change(inputBox, { target: { value: '1234567' } });
Are you sure inputBox is what you expect it to be?
Is there a way to print log using this library... I could not find enough docs on this
const { debug } = render() by default if you call debug() you will get the whole DOM printed. You can also to debug(inputBox)
2
// import ing userevent
import userEvent from '@testing-library/user-event';
// detecting the input in component by the placeholder name
const input = screen.queryByPlaceholderText('input placeholder 
name');
// entering data in the userEvent
userEvent.type(input,'typing some value into the input');
// invoking blur functiuonality.
fireEvent.blur(input);

1 Comment

Welcome to SO? Can you explain what your problem is in the question?
0

By Writing

const inputElement = screen
      .getByTestId('textarea-provided-test-id')
      .querySelector('input');

Solved my problem Reference link

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.