Skip to content

Commit

Permalink
Merge pull request #15463 from bekzod/added-assetion
Browse files Browse the repository at this point in the history
added assertion to `Ember.on` for invalid arguments
  • Loading branch information
locks authored Aug 18, 2017
2 parents d1df859 + 2c2eee1 commit f7e1d18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/ember-metal/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ export function listenersFor(obj, eventName) {
export function on(...args) {
let func = args.pop();
let events = args;

assert('Ember.on expects function as last argument', typeof func === 'function');
assert('Ember.on called without valid event names', events.length > 0 && events.every((p)=> typeof p === 'string' && p.length));

func.__ember_listens__ = events;
return func;
}
14 changes: 14 additions & 0 deletions packages/ember-metal/tests/events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ QUnit.test('a listener can be added as part of a mixin', function() {
equal(triggered, 2, 'should invoke listeners');
});

QUnit.test('Ember.on asserts for invalid arguments', function() {
expectAssertion(()=> {
Mixin.create({
foo1: on('bar'),
});
}, 'Ember.on expects function as last argument');

expectAssertion(()=> {
Mixin.create({
foo1: on(function(){}),
});
}, 'Ember.on called without valid event names');
});

QUnit.test('a listener added as part of a mixin may be overridden', function() {
let triggered = 0;
let FirstMixin = Mixin.create({
Expand Down

0 comments on commit f7e1d18

Please sign in to comment.