Тестирование реагирующих горячих клавиш с использованием реагирующей тестирующей библиотеки - PullRequest
0 голосов
/ 21 декабря 2018

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'));
  });
});

Вот мой установочный файл шутки:

/* 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);
};

Есть идеи?

1 Ответ

0 голосов
/ 30 января 2019

Вот как я это сделал:

Я поместил тестовый идентификатор на сам HotKeys -компонент:

<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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...