使用 react-testing-library 测试 react-hotkeys

Testing react-hotkeys using react-testing-library

https://github.com/greena13/react-hotkeys

https://github.com/kentcdodds/react-testing-library

我无法触发热键(运行 开玩笑)。演示组件:

import React from 'react';
import { HotKeys } from 'react-hotkeys';

class TestComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { enterClicked: false };
  }

  render() {
    const { enterClicked } = this.state;
    return (
      <HotKeys
        handlers={{
          enter: (e) => {
            console.log('ENTER', e);
            this.setState({ enterClicked: true });
          },
        }}
      >
        <input placeholder="input" type="text" />
        {enterClicked && <div>Enter clicked</div>}
      </HotKeys>
    );
  }
}

export default TestComponent;

这是一个测试:

import React from 'react';
import { cleanup, fireEvent, render, waitForElement } from 'react-testing-library';

import TestComponent from './TestComponent';

afterEach(cleanup);

describe('Test', () => {
  it('Hotkeys', () => {
    const { debug, getByPlaceholderText, getByText } = render(<TestComponent />);
    const inputNode = getByPlaceholderText('input');
    fireEvent.keyDown(inputNode, { key: 'Enter', keyCode: 13, code: 13, bubbles: true });
    debug();
    waitForElement(() => getByText('Enter clicked'));
  });
});

这是我的 jest 安装文件:

/* global jest */
import { JSDOM } from 'jsdom';
import 'jest-localstorage-mock';
import dotenv from 'dotenv';

dotenv.config({ path: '../.env' });

const jsdom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'https://example.com' });
const { window } = jsdom;
window.location.reload = jest.fn();
window.location.assign = jest.fn();

function copyProps(src, target) {
  const props = Object.getOwnPropertyNames(src)
    .filter((prop) => typeof target[prop] === 'undefined')
    .reduce(
      (result, prop) => ({
        ...result,
        [prop]: Object.getOwnPropertyDescriptor(src, prop),
      }),
      {},
    );
  Object.defineProperties(target, props);
}

if (!window.attachEvent) {
  // eslint-disable-next-line func-names
  const attachEvent = function(on, callback) {
    on = on.substring(2, on.length);
    return this.addEventListener(on, callback);
  };

  window.Element.prototype.attachEvent = attachEvent;
}

if (!window.detachEvent) {
  // eslint-disable-next-line func-names
  const detachEvent = function(on, callback) {
    on = on.substring(2, on.length);
    return this.removeEventListener(on, callback);
  };

  window.Element.prototype.detachEvent = detachEvent;
}

global.window = window;
global.document = window.document;
global.navigator = {
  userAgent: 'node.js',
  platform: 'Mac',
};
copyProps(window, global);

global.fetch = require('jest-fetch-mock');

global.requestAnimationFrame = (cb) => {
  setTimeout(cb, 0);
};

global.cancelAnimationFrame = (cb) => {
  setTimeout(cb, 0);
};

有什么想法吗?

我是这样实现的:

我在 HotKeys-组件本身上放置了一个测试 ID:

<HotKeys
  data-testid="hot-keys-id"
  ...

然后我聚焦组件,然后手动创建一个事件:

const hotkeys = await waitForElement(() => getByTestId('hot-keys-id'));
hotkeys.focus();

const keyDownEvent = new Event('keydown');
keyDownEvent.keyCode = 13;
keyDownEvent.key = 'Enter';
keyDownEvent.code = 13;

hotkeys.dispatchEvent(keyDownEvent);