Skip to content

Commit

Permalink
feat: add eventTarget assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens committed May 12, 2019
1 parent 60d93e1 commit 225fd2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions addon/helpers/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const assertValidEventOptions =

function setupListener(eventTarget, eventName, callback, eventOptions) {
if (DEBUG) assertValidEventOptions(eventOptions, eventName);
assert(
`ember-on-helper: '${eventTarget}' is not a valid event target. It has to be an Element or an object that conforms to the EventTarget interface.`,
eventTarget &&
typeof eventTarget.addEventListener === 'function' &&
typeof eventTarget.removeEventListener === 'function'
);
assert(
`ember-on-helper: '${eventName}' is not a valid event name. It has to be a string with a minimum length of 1 character.`,
typeof eventName === 'string' && eventName.length > 1
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/helpers/on-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ module('Integration | Helper | on', function(hooks) {
await testExpression(`{{on this.testElement "click"}}`);
await testExpression(`{{on this.testElement "" undefined}}`);
await testExpression(`{{on this.testElement 10 undefined}}`);
await testExpression(`{{on this.testElement}}`);
await testExpression(`{{on null 10 undefined}}`);
await testExpression(`{{on}}`);

assert.counts({ adds: 0, removes: 0 });
Expand All @@ -242,7 +244,9 @@ module('Integration | Helper | on', function(hooks) {
"Assertion Failed: ember-on-helper: 'undefined' is not a valid callback. Provide a function.",
"Assertion Failed: ember-on-helper: '' is not a valid event name. It has to be a string with a minimum length of 1 character.",
"Assertion Failed: ember-on-helper: '10' is not a valid event name. It has to be a string with a minimum length of 1 character.",
"Assertion Failed: ember-on-helper: 'undefined' is not a valid event name. It has to be a string with a minimum length of 1 character."
"Assertion Failed: ember-on-helper: 'undefined' is not a valid event name. It has to be a string with a minimum length of 1 character.",
"Assertion Failed: ember-on-helper: 'null' is not a valid event target. It has to be an Element or an object that conforms to the EventTarget interface.",
"Assertion Failed: ember-on-helper: 'undefined' is not a valid event target. It has to be an Element or an object that conforms to the EventTarget interface."
]);
});

Expand Down

0 comments on commit 225fd2c

Please sign in to comment.