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

[BUGFIX] Ensure ArrayMixin#invoke returns an Ember.A. #16785

Merged
merged 2 commits into from
Jun 28, 2018
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
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);