How to update DatePicker values with key events from the React testing library

1 Answer 275 Views
DatePicker
Thomas
Top achievements
Rank 1
Iron
Thomas asked on 11 Jun 2024, 05:45 PM | edited on 11 Jun 2024, 06:04 PM
Because our DatePicker implementations need custom logic for keyUp and keyDown events, it's critical that our unit tests simulate key presses instead of just populating entire preset values. We are currently unable to update any DatePicker's value with the react testing library fireEvents (@testing-library/react). We're aware that React also has Keyboard events available in the user-event library, but the installation conflicts with our kendo library dependencies. How do we get fireEvent keyUp/Down events to change a DatePicker's value, or is this not possible in KendoReact?

test("update datepicker value with key events", () => {
    render(
      <DatePicker id="testDatePicker" defaultValue={new Date("10/11/2024")} />,
    );
    const dateInput = screen.getByRole("combobox");
    act(() => {
      fireEvent.keyDown(dateInput, { key: "2", code: "Digit2" });
      fireEvent.keyUp(dateInput, { key: "2", code: "Digit2" });
    });
    expect(dateInput).toHaveDisplayValue(["02/11/2024"]);
  });



1 Answer, 1 is accepted

Sort by
1
Accepted
Wissam
Telerik team
answered on 13 Jun 2024, 09:11 AM

Hello, Thomas,

Thank you for the code snippet.

Regarding the user-event library `@testing-library/user-even`, it is not supposed to conflict with any KendoReact packages. I tested it and it was working on my side. Therefore, can you please tell me with which package its conflicting and send me your package.json so that I can check the dependency conflicts?

With it, it is easy to test the DatePicker value change with key event changes:

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { DatePicker } from '@progress/kendo-react-dateinputs';

test('DatePicker change value', async () => {
  render(<DatePicker />);
  const datePickerInput = screen.getByRole('combobox');
  datePickerInput.focus();
  await userEvent.type(datePickerInput, '06/15/2024', { delay: 100 });
  expect(datePickerInput.value).toBe('6/15/2024');
});

I also tried making this work without the user-event library, but it only worked by setting the value programatically with the change event and not a key event which is different than the goal of the test:

test("update datepicker value with key events", () => { render(<DatePicker id="testDatePicker" defaultValue={new Date("10/11/2024")} />); const dateInput = screen.getByRole("combobox"); act(() => { fireEvent.focus(dateInput); }); const newValue = "02/11/2024"; for (let i = 0; i < newValue.length; i++) { const char = newValue[i]; act(() => {

fireEvent.keyDown(dateInput, { key: char, code: `Digit${char}`, charCode: char.charCodeAt(0) }); //not working fireEvent.keyUp(dateInput, { key: char, code: `Digit${char}`, charCode: char.charCodeAt(0) }); //not working dateInput.value = newValue.slice(0, i + 1); fireEvent.change(dateInput); }); } expect(dateInput).toHaveDisplayValue(["02/11/2024"]); });

Therefore, can you please provide me more information on the issue that is not allowing you to use `user-event`?

Looking forward for your reply.

Regards,
Wissam
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources!

Thomas
Top achievements
Rank 1
Iron
commented on 13 Jun 2024, 02:34 PM

Hi Wissam,

Thanks for getting back to us! Strangely enough, all of the dependency errors went away this morning when I ran the install on the user-event library again. Not sure what changed, but your solution is working perfectly. Thank you so much!
Wissam
Telerik team
commented on 13 Jun 2024, 02:38 PM

You are welcome, Thomas! I am really glad that everything is working now.
Tags
DatePicker
Asked by
Thomas
Top achievements
Rank 1
Iron
Answers by
Wissam
Telerik team
Share this question
or