最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I test `onKeyDown` prop in React with @testing-libraryuser-event version 14? - Stack Overflow

programmeradmin1浏览0评论

I am creating a list of menu items using React. I want to test that when a user clicks or presses enter on an item, the onSelect prop is called.

Here is a simplified version of my ponent:

import React from "react";

export const Item = ({ children, onSelect }) => {
  const onKeyDown = ({ keyCode }) => keyCode === 13 && onSelect();
  return (
    <div onClick={onSelect} onKeyDown={onKeyDown} tabIndex={0}>
      {children}
    </div>
  );
};

Here is my test:

describe("Item", () => {
    it("calls onSelect when user clicks on the item or presses Enter", async () => {
      const user = userEvent.setup()
      const onSelect = jest.fn();
      const children = "Settings";
      render(<Item onSelect={onSelect}>{children}</Item>);

      const item = screen.getByText(children);
      await user.click(item);
      expect(onSelect).toHaveBeenCalledTimes(1);

      await user.pointer({target: item, keys: '[Enter]'})
      expect(onSelect).toHaveBeenCalledTimes(2);
    });
  });

When running the tests I get the following output:

Item › calls onSelect when user clicks on the item or presses Enter

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 2
    Received number of calls: 1

      48 |
      49 |       await user.pointer({target: item, keys: '[Enter]'})
    > 50 |       expect(onSelect).toHaveBeenCalledTimes(2);
         |                        ^
      51 |     });
      52 |   });
      53 |

How can I test that onSelect was called when the user presses Enter on the item ponent?

I am creating a list of menu items using React. I want to test that when a user clicks or presses enter on an item, the onSelect prop is called.

Here is a simplified version of my ponent:

import React from "react";

export const Item = ({ children, onSelect }) => {
  const onKeyDown = ({ keyCode }) => keyCode === 13 && onSelect();
  return (
    <div onClick={onSelect} onKeyDown={onKeyDown} tabIndex={0}>
      {children}
    </div>
  );
};

Here is my test:

describe("Item", () => {
    it("calls onSelect when user clicks on the item or presses Enter", async () => {
      const user = userEvent.setup()
      const onSelect = jest.fn();
      const children = "Settings";
      render(<Item onSelect={onSelect}>{children}</Item>);

      const item = screen.getByText(children);
      await user.click(item);
      expect(onSelect).toHaveBeenCalledTimes(1);

      await user.pointer({target: item, keys: '[Enter]'})
      expect(onSelect).toHaveBeenCalledTimes(2);
    });
  });

When running the tests I get the following output:

Item › calls onSelect when user clicks on the item or presses Enter

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 2
    Received number of calls: 1

      48 |
      49 |       await user.pointer({target: item, keys: '[Enter]'})
    > 50 |       expect(onSelect).toHaveBeenCalledTimes(2);
         |                        ^
      51 |     });
      52 |   });
      53 |

How can I test that onSelect was called when the user presses Enter on the item ponent?

Share Improve this question asked Apr 21, 2022 at 12:51 mancristianamancristiana 2,1654 gold badges21 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

From documentation

fireEvent.keyDown(domNode, {key: 'Enter', code: 'Enter', charCode: 13})

fireEvent.keyDown(domNode, {key: 'A', code: 'KeyA'})

https://testing-library./docs/user-event/keyboard

The keyboard API allows to simulate interactions with a keyboard. It accepts a string describing the key actions.

await user.keyboard('[Enter]')

KeyboardEvent.keyCode is deprecated.
Therefore we don't support it and encourage you to update your implementation to use non-deprecated features like KeyboardEvent.key or KeyboardEvent.code.

https://codesandbox.io/s/crazy-snyder-gurptu?file=/src/Item.test.js

发布评论

评论列表(0)

  1. 暂无评论