Skip to content

Commit

Permalink
[react-events] Fix keyboard responder test (#16368)
Browse files Browse the repository at this point in the history
  • Loading branch information
necolas authored Aug 12, 2019
1 parent 107521a commit 9449f8b
Showing 1 changed file with 18 additions and 40 deletions.
58 changes: 18 additions & 40 deletions packages/react-events/src/dom/__tests__/Keyboard-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,22 @@ let ReactFeatureFlags;
let ReactDOM;
let useKeyboardResponder;

const createEvent = (type, data) => {
const event = document.createEvent('CustomEvent');
event.initCustomEvent(type, true, true);
if (data != null) {
Object.entries(data).forEach(([key, value]) => {
event[key] = value;
});
}
return event;
};
import {keydown, keyup} from '../test-utils';

function initializeModules(hasPointerEvents) {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableFlareAPI = true;
React = require('react');
ReactDOM = require('react-dom');
useKeyboardResponder = require('react-events/keyboard').useKeyboardResponder;
}

describe('Keyboard event responder', () => {
let container;

beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableFlareAPI = true;
React = require('react');
ReactDOM = require('react-dom');
useKeyboardResponder = require('react-events/keyboard')
.useKeyboardResponder;

initializeModules();
container = document.createElement('div');
document.body.appendChild(container);
});
Expand Down Expand Up @@ -66,7 +59,9 @@ describe('Keyboard event responder', () => {
});

it('prevents custom events being dispatched', () => {
ref.current.dispatchEvent(createEvent('scroll'));
const target = ref.current;
target.dispatchEvent(keydown());
target.dispatchEvent(keyup());
expect(onKeyDown).not.toBeCalled();
expect(onKeyUp).not.toBeCalled();
});
Expand All @@ -88,13 +83,7 @@ describe('Keyboard event responder', () => {
});

it('is called after "keydown" event', () => {
ref.current.dispatchEvent(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: 'Q',
}),
);
ref.current.dispatchEvent(keydown({key: 'Q'}));
expect(onKeyDown).toHaveBeenCalledTimes(1);
expect(onKeyDown).toHaveBeenCalledWith(
expect.objectContaining({key: 'Q', type: 'keydown'}),
Expand All @@ -120,20 +109,9 @@ describe('Keyboard event responder', () => {
});

it('is called after "keydown" event', () => {
ref.current.dispatchEvent(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: 'Q',
}),
);
ref.current.dispatchEvent(
new KeyboardEvent('keyup', {
bubbles: true,
cancelable: true,
key: 'Q',
}),
);
const target = ref.current;
target.dispatchEvent(keydown({key: 'Q'}));
target.dispatchEvent(keyup({key: 'Q'}));
expect(onKeyDown).toHaveBeenCalledTimes(1);
expect(onKeyDown).toHaveBeenCalledWith(
expect.objectContaining({key: 'Q', type: 'keydown'}),
Expand Down

0 comments on commit 9449f8b

Please sign in to comment.