question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

trigger react-ace onChange via React Testing Library (RTL)

See original GitHub issue

Problem

in my code, i have component that using react-ace. in my render function i have this row:

<CodeEditor onChange={onCodeChanged} value={code} />

callback function:

const onCodeBodyChanged = (newCode) => {
      // some code ...
      dispatch(setResource({ newCode }));
}

I want to test onCodeChanged via RTL, so I tried to find the text area on change the value, but without any success

example of (not working) test:

      const { container } = render(<ResourceEditorPanel />, createMockStore());
      const ace = container.querySelectorAll('.ace_text-input');
      fireEvent.change(ace, { target: { value: 'someText' } });
      await waitFor(() => {
        expect(dispatch).toHaveBeenCalled();
      });

the problem is fireEvent.change(ace, { target: { value: 'someText' } }); doesn’t trigger my function - onCodeChanged.

Do you know how can I test it?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10

github_iconTop GitHub Comments

5reactions
zaharzagravacommented, Mar 24, 2021
5reactions
IvanKalinincommented, Jan 6, 2021

I was able to test it by using the paste event rather than change. And here is what I do:

Component:

<AceEditor
    ...
    placeholder={process.env.NODE_ENV === 'test' ? 'Enter your text' : ''}
    ...
  />

Test:

....
import userEvent from '@testing-library/user-event';
.....
const aceTextareaPlaceholder = getByText('Enter your text');
const aceTextarea = aceTextareaPlaceholder.parentElement?.parentElement?.querySelector(
  'textarea'
) as HTMLTextAreaElement;

userEvent.paste(aceTextarea, 'Your text goes here');
Read more comments on GitHub >

github_iconTop Results From Across the Web

trigger react-ace onChange via React Testing Library (RTL)
[Solved]-trigger react-ace onChange via React Testing Library (RTL)-Reactjs. Search. score:-1. Ace editor awaits for an input event so you'll also need to ...
Read more >
onChange event not firing · Issue #637 · testing-library ...
So I'm not sure the first option would be useful for this test, as (if I understand correctly), I would essentially be triggering...
Read more >
How to Test onChange Events in React Testing Library
To select an option from an HTML select element, you will need to grab the element with either screen.getByTestId or screen.getByRole , and...
Read more >
How do I trigger a change event on radio buttons in react- ...
import React from 'react'; import { render, cleanup, fireEvent } from 'react-testing-library'; import App from './App'; afterEach(cleanup); it(' ...
Read more >
Testing events with react testing library
Lets see our first example, we will test onChange event for an input field. // app.js const [name, setName] = useState( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found