How to use the @testing-library/react.createEvent.keyDown function in @testing-library/react

To help you get started, we’ve selected a few @testing-library/react examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / shared-behaviours / cancel-while-dragging.spec.js View on Github external
it('should cancel when pressing escape', () => {
    const onDragEnd = jest.fn();
    const { getByText } = render();
    const handle: HTMLElement = getByText('item: 0');

    simpleLift(control, handle);
    expect(isDragging(handle)).toBe(true);

    // cancel
    const event: Event = createEvent.keyDown(handle, {
      keyCode: keyCodes.escape,
    });

    fireEvent(handle, event);

    // event consumed
    expect(event.defaultPrevented).toBe(true);
    // drag ended
    expect(isDragging(handle)).toBe(false);
    expect(onDragEnd.mock.calls[0][0].reason).toBe('CANCEL');
  });
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / keyboard-sensor / directional-movement.spec.js View on Github external
it('should move up when pressing the up arrow', () => {
  const onDragUpdate = jest.fn();
  const { getByText } = render();
  const handle: HTMLElement = getByText('item: 1');

  simpleLift(keyboard, handle);

  const event: Event = createEvent.keyDown(handle, {
    keyCode: keyCodes.arrowUp,
  });
  fireEvent(handle, event);

  // flush async responder
  jest.runOnlyPendingTimers();
  expect(onDragUpdate).toHaveBeenCalled();
  expect(onDragUpdate.mock.calls[0][0].destination.index).toBe(0);

  // event consumed
  expect(event.defaultPrevented).toBe(true);
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / keyboard-sensor / stopping-a-drag.spec.js View on Github external
it('should prevent default on the event that causes a drop', () => {
  const onDragEnd = jest.fn();
  const { getByText } = render();
  const handle: HTMLElement = getByText('item: 0');

  simpleLift(keyboard, handle);
  expect(isDragging(handle)).toBe(true);

  const event: Event = createEvent.keyDown(handle, { keyCode: keyCodes.space });
  fireEvent(handle, event);

  expect(event.defaultPrevented).toBe(true);
  expect(getDropReason(onDragEnd)).toBe('DROP');
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / mouse-sensor / stopping-a-drag.spec.js View on Github external
it('should prevent default on an escape press', () => {
  const onDragEnd = jest.fn();
  const { getByText } = render();
  const handle: HTMLElement = getByText('item: 0');

  simpleLift(mouse, handle);

  const event: Event = createEvent.keyDown(handle, {
    keyCode: keyCodes.escape,
  });
  fireEvent(handle, event);

  expect(event.defaultPrevented).toBe(true);
  expect(getDropReason(onDragEnd)).toBe('CANCEL');
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / touch-sensor / stopping-a-drag.spec.js View on Github external
it('should prevent default on an escape press', () => {
  const onDragEnd = jest.fn();
  const { getByText } = render();
  const handle: HTMLElement = getByText('item: 0');

  simpleLift(touch, handle);

  const event: Event = createEvent.keyDown(handle, {
    keyCode: keyCodes.escape,
  });
  fireEvent(handle, event);

  expect(event.defaultPrevented).toBe(true);
  expect(getDropReason(onDragEnd)).toBe('CANCEL');
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / keyboard-sensor / starting-a-drag.spec.js View on Github external
it('should prevent the default keyboard action when lifting', () => {
  const { getByText } = render();
  const handle: HTMLElement = getByText('item: 0');

  const event: Event = createEvent.keyDown(handle, { keyCode: keyCodes.space });
  fireEvent(handle, event);

  expect(isDragging(handle)).toBe(true);
  expect(event.defaultPrevented).toBe(true);
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / mouse-sensor / prevent-standard-keys-while-dragging.spec.js View on Github external
[keyCodes.enter, keyCodes.tab].forEach((keyCode: number) => {
    const event: Event = createEvent.keyDown(handle, { keyCode });
    fireEvent(handle, event);
    expect(event.defaultPrevented).toBe(true);
    expect(isDragging(handle)).toBe(true);
  });
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / keyboard-sensor / prevent-standard-keys-while-dragging.spec.js View on Github external
[keyCodes.enter, keyCodes.tab].forEach((keyCode: number) => {
    const event: Event = createEvent.keyDown(handle, { keyCode });
    fireEvent(handle, event);
    expect(event.defaultPrevented).toBe(true);
  });
});
github atlassian / react-beautiful-dnd / test / unit / integration / drag-handle / keyboard-sensor / prevent-keyboard-scroll.spec.js View on Github external
keys.forEach((keyCode: number) => {
    const event: Event = createEvent.keyDown(handle, { keyCode });
    fireEvent(handle, event);

    expect(event.defaultPrevented).toBe(true);
    expect(isDragging(handle)).toBe(true);
  });
});