Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added assertion to Ember.on for invalid arguments #15463

Merged
merged 1 commit into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ember-metal/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,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