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

Support the change event for <input type="range"> #871

Open
illright opened this issue Mar 3, 2022 · 7 comments
Open

Support the change event for <input type="range"> #871

illright opened this issue Mar 3, 2022 · 7 comments
Labels
enhancement New feature or request needs specification The desired behavior is not defined yet

Comments

@illright
Copy link

illright commented Mar 3, 2022

Problem description

I want to test that my <input type="range"> calls the necessary function upon moving the slider thumb to a particular value. So far, it doesn't seem to be possible with the current capabilities of user-event.

Suggested solution

Similarly to selectOptions, I would like to have something like this:

await userEvent.setValueInRange(screen.getByRole('slider'), 50);

Other ideas for the name could be dragTo, moveSliderTo.

Additional context

No response

@illright illright added enhancement New feature or request needs assessment This needs to be looked at by a team member labels Mar 3, 2022
@illright
Copy link
Author

illright commented Mar 3, 2022

I would be willing to implement this feature with some guidance

@ph-fritsche
Copy link
Member

The problem with utility APIs like selectOptions is that they define the interaction by the effect. The user can achieve that effect by different means. So there isn't a single truth of desired behavior that we could check the implementation against.

An <input type="range"/> can be changed per keyboard or per pointer - per different keys and movements. I'd rather implement the behavior on those events than introduce a new utility.
Then interacting with the control in the test gives confidence that a user can actually change the value.

Keyboard interaction should be easy to implement. ArrowLeft, ArrowRight, Home, End, PageUp, PageDown, anything else?
Pointer interaction might be more tricky as the pointer position corresponds with a value based on the control dimensions. And possibly how the control is displayed by different browsers?
In testing environments without layout (JSDOM), this might be tricky. But we might also just view that problem as out of scope and rely on the user to set the necessary element properties in such environment.

@ph-fritsche ph-fritsche added needs specification The desired behavior is not defined yet and removed needs assessment This needs to be looked at by a team member labels Mar 4, 2022
@dios-david
Copy link

dios-david commented Mar 4, 2022

In the meantime, this works fine:

import { fireEvent } from '@testing-library/dom'; // or `@testing-library/react`

// ...
const slider = container.querySelector('input[type="range"]')!;

fireEvent.change(slider, { target: { value: 170 } });

But it would be cool to see support for this in @testing-library/user-event as well.

@0fprod
Copy link

0fprod commented Apr 22, 2022

I've tried using .click(element) to give the focus to the element and then using .keyboard() with arrow keys but it's currently throwing an error as described here #901 😅

Meanwhile, we can achieve this with fireEvent like @illright says, in my case I used

const slider = screen.getByRole('slider');
await fireEvent.update(slider, '5');

@cscheffauer
Copy link

+1 to get support for this

1 similar comment
@ismaelcostarc
Copy link

+1 to get support for this

tujoworker added a commit to dnbexperience/eufemia that referenced this issue Oct 4, 2023
Sadly there is no support for simulating a keyboard usage. Like so:

```js
await userEvent.keyboard('[ArrowRight]')
```

- userEvent issue: testing-library/user-event#871

So no tests on this one.
tujoworker added a commit to dnbexperience/eufemia that referenced this issue Oct 5, 2023
Sadly there is no support for simulating a keyboard usage. Like so:

```js
await userEvent.keyboard('[ArrowRight]')
```

- userEvent issue: testing-library/user-event#871

So no tests on this one.
tujoworker added a commit to dnbexperience/eufemia that referenced this issue Oct 5, 2023
Sadly there is no support for simulating a keyboard usage. Like so:

```js
await userEvent.keyboard('[ArrowRight]')
```

- userEvent issue:
testing-library/user-event#871

So I thought, maybe its time to add an e2e test for a Eufemia component
as well.
And I can confirm that the test fails without the "fix".

With mouse:


https://github.com/dnbexperience/eufemia/assets/1501870/ce03fac9-d4ec-405c-b672-ac6e679f4771

With keyboard:



https://github.com/dnbexperience/eufemia/assets/1501870/8a60adc4-01d8-45ac-8977-13ab2129749f

With keyboard after the fix:


https://github.com/dnbexperience/eufemia/assets/1501870/e474091c-3646-4a20-a555-3eac77086872
@Rishavraaj
Copy link

Hi there! if there are plans to integrate <input type="range"> support within userEvent. It would be incredibly helpful for more precise testing scenarios. Any updates or insights on this would be much appreciated. Thanks a lot!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs specification The desired behavior is not defined yet
Projects
None yet
Development

No branches or pull requests

7 participants