Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrowDown / ArrowUp events in "number" input fields do not trigger onChange #1066

Open
ericnicolaas opened this issue Sep 30, 2022 · 2 comments · May be fixed by #1272
Open

ArrowDown / ArrowUp events in "number" input fields do not trigger onChange #1066

ericnicolaas opened this issue Sep 30, 2022 · 2 comments · May be fixed by #1272
Labels
accuracy Improves the accuracy of how behavior is simulated enhancement New feature or request

Comments

@ericnicolaas
Copy link

Reproduction example

https://codesandbox.io/s/eloquent-tristan-ixhpuv?file=/src/number.test.js

Prerequisites

  1. Create a Number component with an onChange handler that calls back to the container.
  2. Create a test where Number component is rendered with an onChange func created with jest.fn(). Use userEvent to click into the field and then simulate a down-arrow keyboard press.

For example:

test('the value can be changed with arrow up & down', async () => {
    const initialValue = '7';
    const minValue = '5';
    const onChange = jest.fn();

    render(<NumberField label={LABEL} value={initialValue} onChange={onChange} min={minValue} />);

    const input = screen.getByRole('spinbutton');

    await userEvent.click(input);
    await userEvent.keyboard('{arrowdown}');
    expect(onChange).toBeCalledWith('6');
})

Expected behavior

Expected behaviour is for the test to pass. Pressing the down arrow in a browser would cause the number value to reduce by 1 (assuming step=1).

Actual behavior

Upon running the tests, the test fails with the following result:

expect(jest.fn()).toBeCalledWith(...expected)

    Expected: "6"

    Number of calls: 0

    > 64 |              expect(onChange).toBeCalledWith('6');
         |                               ^
      65 |      });
      66 | });

User-event version

14.4.3

Environment

Testing Library framework: @testing-library/[email protected]
JS framework: [email protected]
Test environment: [email protected]
DOM implementation: [email protected]

Additional context

No response

@ericnicolaas ericnicolaas added bug Something isn't working needs assessment This needs to be looked at by a team member labels Sep 30, 2022
@ph-fritsche ph-fritsche added enhancement New feature or request accuracy Improves the accuracy of how behavior is simulated and removed bug Something isn't working needs assessment This needs to be looked at by a team member labels Nov 14, 2022
@TheSonOfThomp
Copy link

TheSonOfThomp commented Aug 28, 2023

This doesn't work with userEvent.type either with userEvent 13.5.0

const input = screen.getByRole('spinbutton');
await userEvent.type(input, '{arrowdown}');
expect(onChange).toBeCalledWith('6');

@eliuAtFanatics
Copy link

I tried something similar to above, and this is still not working with userEvent 14.5.2

@Haberkamp Haberkamp linked a pull request Feb 6, 2025 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accuracy Improves the accuracy of how behavior is simulated enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants