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

Cannot test with react-testing-library #661

Open
javillerenas opened this issue Jun 18, 2020 · 1 comment
Open

Cannot test with react-testing-library #661

javillerenas opened this issue Jun 18, 2020 · 1 comment

Comments

@javillerenas
Copy link

@javillerenas javillerenas commented Jun 18, 2020

Description

Hi, I've been working on testing a component which contains multiple <Range> components with @react-testing-library and are having issues attempting to programmatically changing the value of the slider. Specifically I cannot replicate a focus and keydown user event.

Code

This is what my code looks like:

...
const sliders1 = within(rows[0]).getAllByRole("slider");
await act(async () => {
  fireEvent.focus(sliders1[1]);
  console.log('Slider after focus: \n', prettyDOM(sliders1[1]));
  fireEvent.keyDown(sliders1[1], { key: 'ArrowRight', code: 'ArrowRight' });
  fireEvent.keyDown(sliders1[1], { key: 'ArrowRight', code: 'ArrowRight' });
  console.log('Slider after KeyDown: \n', prettyDOM(sliders1[1]));
});

These are the results of the console.log() calls:

Slider after focus:  
    <div
      aria-disabled="false"
      aria-valuemax="60"
      aria-valuemin="0.5"
      aria-valuenow="30.5"
      class="rc-slider-handle rc-slider-handle-2"
      role="slider"
      style="left: 50.42016806722689%; transform: translateX(-50%);"
      tabindex="0"
    />

Slider after KeyDown:  
    <div
      aria-disabled="false"
      aria-valuemax="60"
      aria-valuemin="0.5"
      aria-valuenow="30.5"
      class="rc-slider-handle rc-slider-handle-2"
      role="slider"
      style="left: 50.42016806722689%; transform: translateX(-50%);"
      tabindex="0"
    />

As you can see, the valuenow is the same before and after the keyDown event. Also I noticed the rc-slider-handle-dragging class is not present.

The code below shows how I am currently rendering the <Range> component:

<Range
  min={limitsMin}
  max={limitMax}
  step={0.5}
  value={row.value}
  allowCross={false}
  onChange={(e: any) => handleSliderChange(e, index)}
  marks={getRangeMarks(row)}
  data-testid="range-height"
/>

NOTE 1:

The Range's onChange handler is triggered, but the event contains the same exact values the <Range> currently has which is :[0.5, 30.5, 60] (There's no change in the actual values).

NOTE 2:

The class rc-slider-handle-dragging is present in the first slider even though I never set it myself. If I try to fire a keyDown event it does not work either.

Update 1:

Looking at both the container.baseElement and the actual Range, they both have a clientHeight and clientWidth of 0. Not sure if that might be causing the issues.

Environment

  • rc-slider: "9.2.4"
  • @types/rc-slider: "^8.6.5"
  • @testing-library/react: "^9.5.0"
  • @testing-library/jest-dom: "^4.2.4"
@mstrofbass
Copy link

@mstrofbass mstrofbass commented Sep 22, 2020

Note that in the library tests, which use enzyme, the focus event is fired on the slider component itself, not the handle, which is what the getByRole returns, I believe. Might try adjusting based on that.

I messed around with some basic tests a bit but couldn't get it to work and have more pressing things to do, but wanted to point that out just in case it was helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.