ember-test-helpers icon indicating copy to clipboard operation
ember-test-helpers copied to clipboard

Return the event which is triggered by a DOM event helper

Open bastimeyer opened this issue 7 years ago • 1 comments

Currently, none of the available DOM event helpers (triggerEvent, triggerKeyEvent, click, etc.) return the event which they have triggered on an element and instead return the value of the isSettled() call. This means that in cases where for example a component has prevented the browser's default action or has stopped the propagation of a certain event, this can't be tested by using the available DOM helpers of ember-test-helpers.

What is even the reason for returning the value of isSettled()? By looking at the code, it seems like the intention for this was to just chain the settled() promise in the returned event helper promise.

See this simple use case:

test( "foo", async function( assert ) {
  await render( hbs`{{foo-bar}}` );
  const event = await triggerEvent( ".foo-bar", "eventname" );
  assert.ok( event.defaultPrevented, "Prevents default action" );
});

I've been previously using jQuery for having a dead simple API to trigger DOM events in tests, but since Ember is slowly getting rid of jQuery (which is a good thing), the logical choice for devs is to stop using jQuery in tests, too, and using the helper methods of ember-test-helpers instead. But since the test helpers don't return the events which they are triggering, this is a problem in certain test scenarios.

bastimeyer avatar Oct 21 '18 08:10 bastimeyer

@bastimeyer Right now every helper returns the value of calling settled(), which unless I'm wrong it is just a Promise<void>. What you are suggesting is that we change it so it returns Promise<Event>.

In ok with the idea in principle, as right now afaik we don't have any compromise of what the resolution value of the promises are. Changing from Promise<void> to Promise<Event> would give us more capabilities and wouldn't mess with existing code.

However it can be tricky for events like click or fillIn that in reality trigger several events. Click, for instance, fires mousedown, focus, mouseup and then click. We could just return click or return an object or array with all the fired events, but we'd have to decide what is the right thing to do first.

cibernox avatar Oct 21 '18 16:10 cibernox