1

I am having problems testing the onChangeCommitted event using the slider from Mui. In a code sandbox I've recreated a simple implementation of the use of a slider.

Here's the code sandbox.

The problem I'm having is that while I figured out how to change the aria-valuenow property on the slider element itself (span with role="slider") the test does not seem to trigger the onChangeCommitted event. This is pretty annoying as i have side effects in the onChangeCommitted event that need to be tested.

2 Answers 2

1

JSdom actually renders these tests without physical width so a method needed to be mocked with jest.

The actual solution to this problem was to mock screen.getByRole('slider').getBoundingClientRect using jest like so:

    screen.getByRole('slider').getBoundingClientRect = jest.fn(() => ({
      width: 100,
      height: 10,
      bottom: 10,
      left: 0,
      x: 0,
      y: 0,
      right: 0,
      top: 0,
      toJSON: jest.fn(),
    }));

Then mouse up and mouse down could be used like so:

fireEvent.mouseDown(screen.getByRole('slider'), { buttons: 1, clientX: 30 });
fireEvent.mouseUp(screen.getByRole('slider'), { buttons: 1, clientX: 30 });
Sign up to request clarification or add additional context in comments.

Comments

0

You should trigger a mouseUp event in your tests. Referring to the mui docs

onChangeCommitted
Callback function that is fired when the mouseup is triggered.

Signature:
function(event: object, value: number | number[]) => void
event: The event source of the callback.
value: The new value.

So

fireEvent.mouseUp(...

instead of mouseDown

1 Comment

I should trigger this on a specific mark right? Because no event works when targeting the marks themself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.