question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

keyEvent does not seem to trigger events in tests using input helper

See original GitHub issue

Hello, thanks for creating this package!

Currently, using keyEvent(), particularly for ENTER or ESC keycodes (13 and 27 respectively), on an {{input}} element in a component while running a phantomJS test does not fire the event.

Take the following test:

test('test does not work', function(assert) {
  this.set('escPressed', () => {
    // never called
    console.log('keyup called!');
    assert.ok(true);
  });
  this.render(hbs`{{input escape-press=(action escPressed)}}`);
  let input = find('input');
  focus(input);
  keyEvent(input, 'keyup', 27); // esc
});

After doing a bit of research, it appears that when creating events using KeyboardEvents or KeyEvents in the fire-event.js, PhantomJS assigns all keyCode property on events to 0, for reasons I’m not too sure of. As a result, Ember is not able to correctly detect the pressed key because keyCode is being set to 0 by PhantomJS.

A workaround was to create an event in a legacy way. For instance, instead of using keyEvent, triggering an event like this works.

   let evt = document.createEvent('CustomEvent');
   evt.initEvent('keyup', true, false);
   evt.keyCode = 27;
   input.dispatchEvent(evt);

This may be something that phantomjs may be able to fix in its package, but I was thinking it wouldn’t be unreasonable for this package to support this?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
GCheung55commented, Nov 8, 2017

Dug a little more and eventually came across Ember.TextField documentation that the {{input bubbles=true}} would bubble up the keyUp event to parent nodes.

Instead of using keyUp, I used keyPress instead. With keyPress, I do not have to set the bubbles property.

1reaction
cibernoxcommented, Jun 22, 2017

@brandynbennett If it’s possible, I’d recomend trying headless chrome instead of phantom. Faster, more reliable. We haven’t decided yet if this is something we want to fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fireEvent.keyPress is not working (ignored or not ... - GitHub
If I fireEvent.change(input, { target: { value: 'test' } }); I can expect its ... it is expected that a lone keydown event...
Read more >
WPF - Raise KeyEvent Trigger in a Unit Test does not work
1 Answer 1 · the OPs tries to unit test a custom control. There is no viewmodel behind a control. · sounds like...
Read more >
How simulate "enter" keypress in integration test? - Ember.JS
I have an input that triggers an action when enter key is pressed. I've tried programmatically simulating that event in my integration test...
Read more >
KeyboardEvent - Web APIs | MDN
A key that normally produces a character value has been pressed. This event was highly device-dependent and is obsolete. You should not use...
Read more >
Mousetrap - Keyboard shortcuts in Javascript - Craig Campbell
Mousetrap is a simple keyboard shortcut/event library written in Javascript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found