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

Unable to test with DateRangePicker #2238

Open
xereda opened this issue Aug 22, 2023 · 10 comments
Open

Unable to test with DateRangePicker #2238

xereda opened this issue Aug 22, 2023 · 10 comments

Comments

@xereda
Copy link

xereda commented Aug 22, 2023

react-dates version
21.8.0

Describe the bug
I use the "DateRangePicker" component in a project and I need to test the user flow with the Testing Library. The problem is that when I do the first click (userEvent.click()), on the first date, the modal is closed, not waiting for the selection of the second date.

 "@testing-library/react": "^14.0.0",
 "@testing-library/user-event": "^14.4.3",
 "react-dates": "^21.8.0",

Source code (including props configuration)
Steps to reproduce the behavior:

    <DateRangePicker
      noBorder
      minimumNights={0}
      startDate={startDate}
      startDateId="start-date-id"
      endDate={endDate}
      endDateId="end-date-id"
      onDatesChange={handleDatesChange}
      focusedInput={focusedInput}
      onFocusChange={(focusedInput) => setFocusedInput(focusedInput)}
      isOutsideRange={isOutsideRange}
      onClose={handleOnClose}
      withPortal={true}
      customInputIcon={label}
      hideKeyboardShortcutsPanel={true}
    />
@ljharb
Copy link
Member

ljharb commented Aug 22, 2023

We recommend testing with enzyme, but in particular, no testing solution's event simulations (enzyme and RTL included) are actually true matches of browser behavior - they just invoke props - so they're not very useful.

The click you want doesn't go on the picker directly, it goes on one of the smaller elements contained within it.

@xereda
Copy link
Author

xereda commented Aug 22, 2023

@ljharb Thanks for the answer.
I use the Testing Library to test cover the entire application. At the moment there are more than 600 test scenarios. Many of them simulating user interaction and working perfectly. It is not an option to switch to Enzyme. The only issue we had was with the DateRangePicker.

@ljharb
Copy link
Member

ljharb commented Aug 22, 2023

Understood, but I'm trying to convey that "simulating user interaction" with either RTL or with enzyme doesn't actually give you value, even if it seems to "work", because it doesn't actually match user interaction.

At any rate, the issue at hand is likely that your .click() isn't targeting the right element. If you can make a codesandbox that can reproduce the problem I'm happy to take a look.

@xereda
Copy link
Author

xereda commented Aug 22, 2023

I got the elements for the start and end date through the getByRole method. Note below:

    const firstDate = screen.getByRole('button', {
      name: /choose terça-feira, 1 de agosto de 2023 as your check-in date. it’s available./i,
    });
    const lastDate = screen.getByRole('button', {
      name: /choose terça-feira, 22 de agosto de 2023 as your check-in date. it’s available./i,
    });

@ljharb
Copy link
Member

ljharb commented Aug 22, 2023

Sounds like an issue with RTL then; i'm not sure how getByRole works.

@xereda
Copy link
Author

xereda commented Aug 22, 2023

@ljharb

https://github.com/xereda/datepicker-test

In this repository I managed to reproduce the problem.

@xereda
Copy link
Author

xereda commented Aug 23, 2023

Sounds like an issue with RTL then; i'm not sure how getByRole works.

getByRole

The getByRole method from the Testing Library provides a way to select an element by its accessible role. This query is recommended because it encourages developers to write tests that rely on the accessibility information of elements, which ultimately leads to more accessible applications.

https://testing-library.com/docs/queries/byrole/

@xereda
Copy link
Author

xereda commented Aug 23, 2023

@ljharb

https://github.com/xereda/datepicker-test

In this repository I managed to reproduce the problem.

src/components/Datepicker/Datepicker.test.js

https://github.com/xereda/datepicker-test/blob/main/src/components/Datepicker/Datepicker.test.js

yarn test:watch

Note that the modal is closed on the first click.

@xereda

This comment was marked as spam.

@robinovitch61

This comment was marked as spam.

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

No branches or pull requests

3 participants