cypress: can't trigger 'onChange' for an input type='range' rendered by React
Current behavior:
...
return (
...
<div className="search-bar__form-range">
<input type="range" min={10} max={900} step={10} value={500} onChange={(event)=>alert(`slider changed
to:${event.target.value}`)}
/>
...
cy.get('.search-bar__form-range > input[type=range]').as('range').invoke('val', 700)
.trigger('change');
Changes the slider value. Doesn’t trigger the onChange handler.
Desired behavior:
should trigger the onChange handler
Steps to reproduce:
Set up a React app with an input type=‘range’ and an onChange handler Try to trigger the onChange event from cypress.
Versions
“cypress”: “^2.1.0” “react”: “^15.4.2”
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 10
- Comments: 31 (1 by maintainers)
I have put together a workaround for this issue that does not require switching to the
input
event.There are two parts:
change
event on it such that React picks up the change.That works like this:
I’m fairly new to the cypress ecosystem, so perhaps someone can do this in a more cypress-y way, but this solves the problem.
It doesn’t work for TypeScript. I received the error:
Argument of type '{ value: number; bubbles: true; }' is not assignable to parameter of type 'EventInit'. Object literal may only specify known properties, and 'value' does not exist in type 'EventInit'
So I’ve solved my problem with this issue for TypeScript using the following code: In commands:
And use it in the test:
✋ Also effected by this same issue:
HTML
Cypress
Workaround 🎉
I have implemented @davemyersworld’s workaround, and can confirm it works! 👍
I’ve taken his example a wee further and abstracted a higher order function to perform the input value change. Here is the Cypress test to change and test the input range value from 0 to 1 to 2 to 3:
Looking forward to replacing workaround when a fix is available.
Thanks for triaging @jennifer-shehane
It looks like this issue has been solved, but I wanted to put this here in case anyone comes across this in google like I did today -
I was able to register a custom command in in the
cypress/support/commands.js
file:This is used in the test as:
ok, so I’ve found sort of a workaround. it looks like the
onInput
event can be triggered just fine. In the case of input type=‘range’ the “input” event is triggered in the same situation as the “change” is so I was able to switch it.Workaround would be: use
onInput
with.trigger('input')
instead ofonChange
I was also running into this issue and had to use beausmith’s workaround (adapted from Dave Myers’).
I had to make a couple further adjustments, so I’ll share them here, hoping they can save other people some time.
What changed:
input
andchange
events. Our slider implementation uses both, maybe that’s the case for you as well.Hopefully I didn’t misunderstand any Cypress or DOM details - but the above solution, adapted from @beausmith 's, worked well for me, letting me test sliders on my project.
*** The issue is resolved by the code below: *** - Thx to Vinod Mathew@k.vinodmathew_gitlab ***
thank you @davemyersworld! that fixed the issue for me!!
@davemyersworld Can’t thank you enough for this solution!!
This might help anyone coming here looking for answers but this helped us:
@RobertoPegoraro, this worked for me, thanks!
Has anyone managed to program the workaround with Typescript?
I’m getting always a Type Error - Illegal invocation for this:
I’m encountering the same issue. Using
.trigger('input')
instead of thechange
event also didn’t affect anything.I’ve been playing around with a number of combinations using
click
,mousedown
,mousemove
,mousemove
to trigger an interaction on aninput type='range'
, but nothing seems to work.So far, I have this piece of code that sets and changes the value of the input in the DOM, and it moves the range slider (the circle knob), but the input still doesn’t recognize that there’s an interaction:
I’ve tried
.click()
,.click('left')
, and.click(200, 15)
– positional clicking within the input, but Cypress doesn’t click on the input.Does anyone know a good way of triggering an
onChange
event? Is this an actual bug?