Skip to content

keyDown, 'keyPress and friends do not support altKey or ctrlKey` values on the events #5

@jvanoostveen

Description

@jvanoostveen

The control or alt key are not taken into account when handling a keyDown or keyPress.

Example test scenario / API:

it('will remember if alt or ctrl key is pressed when simulating another keyPress', () => {
  let handleKeyDown = sinon.stub();
  let vnode = h('div', { tabindex: '-1', onkeydown: handleKeyDown });

  var nodeQuery = createQuery(vnode);
  nodeQuery.simulate.keyDown(17); // control
  nodeQuery.simulate.keyDown(18); // alt
  nodeQuery.simulate.keyPress(97, '', ''); // a

  expect(handleKeyDown).to.have.been.calledThrice;

  let event: KeyboardEvent = handleKeyDown.lastCall.args[0];
  expect(event.ctrlKey, 'ctrlKey should be set').to.be.true;
  expect(event.altKey, 'altKey should be set').to.be.true;

  nodeQuery.simulate.keyUp(17); // control
  nodeQuery.simulate.keyUp(18); // alt
  nodeQuery.simulate.keyPress(97, '', ''); // a

  event = handleKeyDown.lastCall.args[0];
  expect(event.ctrlKey, 'ctrlKey should not be set').to.be.false;
  expect(event.altKey, 'altKey should not be set').to.be.false;
});

This would require the simulator to remember the state.

Issue in current implementation:

  • each time nodeQuery.simulate is done, a new simulator is created.

Issues when one simulator per query is created:

  • it might leak state if not reset properly.
  • if the query is not stored, it would still create a new simulator for each query.

Other possible solution would be to allow some event data in the keyDown/keyPress calls:

nodeQuery.simulate.keyDown(97, undefned, { ctrlKey: true, altKey: true });
nodeQuery.simulate.keyPress(97, '', '', undefned, { ctrlKey: true, altKey: true });

but this does not make a really nice API or corresponds on how the keys are handles by the browser (it would not help you making sure you can handle a keyDown event for only the control key).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions