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.

userEvent.selectOptions does not trigger onChange event

See original GitHub issue
  • @testing-library/user-event version: 12.0.0
  • Testing Framework and version: jest@24.7.1
  • DOM Environment: jsdom@14.1.0

Relevant code or config

// Select.js
import React from "react";

const Select = props => {
  const handleChange = e => {
    console.log(e.target.value);
    props.test(e.target.value);

  };
  return (
    <select data-testid="select" onChange={handleChange}>
      <option data-testid="first" value="first">
        first
      </option>
      <option data-testid="second" value="second">
        second
      </option>
      <option data-testid="third" value="third">
        third
      </option>
    </select>
  );
};
export default Select;

// Select.test.js
  import React from 'react';
  import Select from './Select';
  import userEvent from '@testing-library/userEvent';
  import render from '@testing-library/react';


  test("`userEvent.selectOptions` should trigger an onChange event", () => {
    const mockTest = jest.fn().mockImplementation(val => console.log(val));
    const { getByTestId, debug } = render(<Select test={mockTest} />);
    const select = getByTestId("select");
    const second = getByTestId("second");

    userEvent.selectOptions(select, ["second"]);
    debug();
    console.log(select.value)
    expect(mockTest).toHaveBeenCalledTimes(1);
    expect(mockTest).toHaveBeenCalledWith('second');
    expect(second.selected).toBe(true);
  });

What you did: I first noticed this behavior in a larger project, so I made a small repo to reproduce.

I made a <select/> element in React that fires a function received from props onChange. This behavior works as expected in the browser. When testing the behavior, I can successfully assert that a mocked version of that onChange function is called when I trigger a change event (via importing fireEvent from RTL), but cannot assert that the same mock is called when using userEvent.selectOptions.

What happened: Screen Shot 2020-06-16 at 9 28 24 AM

Reproduction repository: https://github.com/khalidwilliams/select-test-demo

Problem description: userEvent.selectOptions doesn’t trigger a change event. onChange handlers won’t fire in tests that use this method, leading to problems with testing any change-dependent behavior.

Suggested solution: I’m not sure what the best solution is, it seems like this line may not be firing as expected? Please let me know if there’s something I’m missing here – I’m happy to keep digging!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

19reactions
arjun-jscommented, Dec 1, 2021

It is happening now for select dropdowns.

I am using userEvent.selectOptions to change the value. I am able to change the value but the change event is not getting triggered.

I am using:

@testing-library/user-event : 13.5.0

1reaction
Bhuvanesh-git331commented, Aug 12, 2022

const dbEle=document.getElementById(‘schemaname’) const optionEle=document.createElement(‘option’) optionEle.setAttribute(‘value’, ‘el_cardnumber_3’) dbEle.domNode = optionEle dbEle.appendChild(optionEle); console.log(screen.debug(document.querySelectorAll(‘option’)[1])) userEvent.selectOptions(dbEle, document.querySelectorAll(‘option’)[1])

//Hi I tried the above method its working for me. The onChange event is getting triggered

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test trigger on change event by selecting data list
The onChange event handler is used by <input type='text'> jsx, so you should use type(element, text, [options]) to writes text inside an ...
Read more >
user-event v13 - Testing Library
If they are not closed explicitly, then events will be fired to close them automatically (to disable this, set the skipAutoClose option to...
Read more >
Simulate Browser Interactions with Testing Library's UserEvent
It builds upon Fire Event to trigger the sequence of events that would normally occur on the browser. This allows you to interact...
Read more >
User Event API Version 13 - Coding Ninjas CodeStudio
In dblClick(element, eventInit, options) options includes pointer events options. test('double click', () => { const onChange = jest.fn() render ...
Read more >
Building and Testing a Select Component - Debbie O'Brien
We then need an onChange function to handle the change for every time ... If not using Bit then you can create mocks...
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