Skip to content

Commit

Permalink
added simple integration test for hds-register-event
Browse files Browse the repository at this point in the history
  • Loading branch information
didoo committed Apr 15, 2024
1 parent 861f366 commit 060d0d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/modifiers/hds-register-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { modifier } from 'ember-modifier';
// see: https://github.com/emberjs/ember.js/issues/19869#issuecomment-1909118910
// see: https://github.com/emberjs/ember.js/pull/20629
// see also: https://github.com/emberjs/ember.js/blob/main/packages/%40ember/-internals/glimmer/lib/modifiers/on.ts#L30
export default modifier((element, positional, named) => {
export default modifier((element, positional, named = {}) => {
// the "target" element the listeners are added to
// notice: this is the element the Ember modifier is attached to
const targetElement = element;
Expand All @@ -19,7 +19,7 @@ export default modifier((element, positional, named) => {
// the options for the `addEventListener()` method
// see: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
// notice: it's expressed as "named" argument (object) for the modifier
const { useCapture = false } = named ?? {};
const { useCapture = false } = named;

targetElement.addEventListener(event, eventHandler, useCapture);

Expand Down
23 changes: 23 additions & 0 deletions showcase/tests/integration/modifiers/hds-register-event-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { click, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';

module('Integration | Modifier | hds-register-event', function (hooks) {
setupRenderingTest(hooks);

test('it calls the callback associated with the `click` event assigned via the modifier', async function (assert) {
let clicked;
this.set('onClick', () => (clicked = true));
await render(
hbs`<button id="test-button" {{hds-register-event 'click' this.onClick}}>Test</button>`
);
await click('button#test-button');
assert.true(clicked);
});
});

0 comments on commit 060d0d5

Please sign in to comment.