Skip to content

Commit

Permalink
Merge pull request #16785 from rwjblue/fix-invoke
Browse files Browse the repository at this point in the history
[BUGFIX] Ensure `ArrayMixin#invoke` returns an Ember.A.
  • Loading branch information
rwjblue authored Jun 28, 2018
2 parents 5ade8f6 + 7542bc4 commit c714f6e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,11 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
invoke(methodName, ...args) {
return this.map(item => tryInvoke(item, methodName, args));
let ret = A();

this.forEach(item => ret.push(tryInvoke(item, methodName, args)));

return ret;
},

/**
Expand Down
29 changes: 28 additions & 1 deletion packages/ember-runtime/tests/array/invoke-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EmberObject from '../../lib/system/object';
import { Object as EmberObject, NativeArray } from '../../index';
import { AbstractTestCase } from 'internal-test-helpers';
import { runArrayTests } from '../helpers/array';

Expand Down Expand Up @@ -28,6 +28,33 @@ class InvokeTests extends AbstractTestCase {
obj.invoke('foo', 2);
this.assert.equal(cnt, 6, 'should have invoked 3 times, passing param');
}

'@test invoke should return an array containing the results of each invoked method'(assert) {
let obj = this.newObject([
{
foo() {
return 'one';
},
},
{}, // intentionally not including `foo` method
{
foo() {
return 'two';
},
},
]);

let result = obj.invoke('foo');
assert.deepEqual(result, ['one', undefined, 'two']);
}

'@test invoke should return an extended array (aka Ember.A)'(assert) {
let obj = this.newObject([{ foo() {} }, { foo() {} }]);

let result = obj.invoke('foo');

assert.ok(NativeArray.detect(result), 'NativeArray has been applied');
}
}

runArrayTests('invoke', InvokeTests);

0 comments on commit c714f6e

Please sign in to comment.