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 beta] Fixes computed.sort works incorrectly with empty sort properties array #13124

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 7 additions & 2 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import compare from 'ember-runtime/compare';
import { isArray } from 'ember-runtime/utils';
import { A as emberA } from 'ember-runtime/system/native_array';
import isNone from 'ember-metal/is_none';
import isEmpty from 'ember-metal/is_empty';
import getProperties from 'ember-metal/get_properties';
import WeakMap from 'ember-metal/weak_map';

Expand Down Expand Up @@ -667,8 +668,12 @@ function normalizeSortProperties(sortProperties) {
});
}

function sortByNormalizedSortProperties(items, normalizedSortProperties) {
return emberA(items.slice().sort((itemA, itemB) => {
function sortByNormalizedSortProperties(_items, normalizedSortProperties) {
let items = _items.slice();
if (isEmpty(normalizedSortProperties)) {
return emberA(items);
}
return emberA(items.sort((itemA, itemB) => {
for (let i = 0; i < normalizedSortProperties.length; i++) {
let [prop, direction] = normalizedSortProperties[i];
let result = compare(get(itemA, prop), get(itemB, prop));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,20 @@ QUnit.test('updating sort properties invalidates the sorted array', function() {
], 'after updating sort properties array is updated');
});

QUnit.test('sorted array should not be changed if sort properties array is empty', function () {
// issue doesn't appear with small arrays (about 10 items), so 20 items are used
var o = EmberObject.extend({
sortedItems: sort('items', 'itemSorting')
}).create({
itemSorting: emberA([]),
items: emberA([
{ p: 1 }, { p: 2 }, { p: 3 }, { p: 4 }, { p: 5 }, { p: 6 }, { p: 7 }, { p: 8 }, { p: 9 }, { p: 10 },
{ p: 11 }, { p: 12 }, { p: 13 }, { p: 14 }, { p: 15 }, { p: 16 }, { p: 17 }, { p: 18 }, { p: 19 }, { p: 20 }
])
});
deepEqual(o.get('sortedItems').mapBy('p'), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 'array is not changed');
});

QUnit.test('updating new sort properties invalidates the sorted array', function() {
deepEqual(obj.get('sortedItems').mapBy('fname'), [
'Cersei',
Expand Down