Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
[Slider] Testing onChangeCommitted from click #23398
Comments
|
Thanks for the report. You have a controlled component but use When I use Could you expand a bit on the "Steps to reproduce"? Just the user interface is not enough. What did you do? What did you expect? What happened instead? |
|
@eps1lon I replaced the defaultValue with value and updated the original post with suggested info. |
|
Right, it's about testing not actual user interaction. The Slider is using pointer position which a programmatic click doesn't include. You would need to calculate the pointer position when the pointer would click the specified element and pass that. I can't help you with that right now. |
|
@eps1lon thanks il give your suggestion a shot |
|
Hey @eps1lon I tried passing clientX and clientY to mousedown events options but what ever values i pass it seems that i allways end up with the same change(i.e. the end result is always I'm sorry, this probably is not a bug but it seems to me that testing these should be more straightforward so I made this issue. Any help is appreciated and I think it will help other people with the same issue. |
|
@Npervic Did you consider asking on StackOverflow? This sounds like a support request. |
|
@Npervic |
|
It seems that we can improve our tests. It's more logical to fire up and down on the same node: diff --git a/packages/material-ui/src/Slider/Slider.test.js b/packages/material-ui/src/Slider/Slider.test.js
index f7afee0554..85956e704c 100644
--- a/packages/material-ui/src/Slider/Slider.test.js
+++ b/packages/material-ui/src/Slider/Slider.test.js
@@ -59,7 +59,7 @@ describe('<Slider />', () => {
const slider = getByRole('slider');
fireEvent.mouseDown(container.firstChild);
- fireEvent.mouseUp(document.body);
+ fireEvent.mouseUp(container.firstChild);
expect(handleChange.callCount).to.equal(1);
expect(handleChangeCommitted.callCount).to.equal(1);We could also test the end value, based on the position: diff --git a/packages/material-ui/src/Slider/Slider.test.js b/packages/material-ui/src/Slider/Slider.test.js
index f7afee0554..5f815a8578 100644
--- a/packages/material-ui/src/Slider/Slider.test.js
+++ b/packages/material-ui/src/Slider/Slider.test.js
@@ -56,13 +56,25 @@ describe('<Slider />', () => {
const { container, getByRole } = render(
<Slider onChange={handleChange} onChangeCommitted={handleChangeCommitted} value={0} />,
);
+ stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
+ width: 100,
+ left: 0,
+ }));
const slider = getByRole('slider');
- fireEvent.mouseDown(container.firstChild);
- fireEvent.mouseUp(document.body);
+ fireEvent.mouseDown(container.firstChild, {
+ buttons: 1,
+ clientX: 10,
+ });
+ fireEvent.mouseUp(container.firstChild, {
+ buttons: 1,
+ clientX: 10,
+ });
expect(handleChange.callCount).to.equal(1);
+ expect(handleChange.args[0][1]).to.equal(10);
expect(handleChangeCommitted.callCount).to.equal(1);
+ expect(handleChangeCommitted.args[0][1]).to.equal(10);
slider.focus();
fireEvent.keyDown(slider, {@Npervic Why are you testing this in jsdom and not a browser? |
|
Why wouldn't I? I dont see a reason a component as such would have problems being tested in jsdom or am i missing something @oliviertassinari ? |
|
@Npervic jsdom has no layouting, all the elements are width: 0, height: 0. |
|
@oliviertassinari I see so testing this in js dom is not possible if I understand you correctly? How should I go about testing this then? |
|
@Npervic I have added the Otherwise, in your case, the Slider relies on mouse down & mouse up & the layout dimension to trigger its event. I would suggest you to either 1. apply the same approach as the proposed diff or 2. test the thing in a real browser. |
|
@oliviertassinari this sounds actually interesting, and I'd love to contribute if I can. Il take a look at solving this over the work week for my case, and on the weekend I could try and contribute from my personal GH account. |
|
@Npervic Great :), to remove any source possible of confusion. We apply the "good first issue" anytime there is a working diff that very likely solves the problem, in our case, it's #23398 (comment). |



Current Behavior馃槸
When testing the slider functionality using rtl and jest no significant DOM update happens when clicking on labels (or doing mousedown or other events)
Expected Behavior馃
DOM should update with the selected mark as Slider value
Steps to Reproduce馃暪
In the codesadbox example i tried to test this component of a slider value change on label (slider mark) click. But i cant seem to get the onChangeCommitted to trigger when testing. The slider component dose not indicate any change
https://codesandbox.io/s/romantic-goldstine-gpjbj?file=/src/App.test.js
Context馃敠
I am trying to test onChangeCommitted in the slider component. But ive tried up and down to test this event on the slider and its getting really frustrating as there dosent seem to be many threads or SO posts about so I might just be missing something trivial. Ive added a sample on codesandbox. Hope some one can help thanks :)
Your Environment馃寧