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] computed.sort array should update if sort properties array is empty #16632

Merged
merged 1 commit into from
May 16, 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
14 changes: 10 additions & 4 deletions packages/@ember/object/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,17 @@ function propertySort(itemsKey, sortPropertiesKey) {

let itemsKeyIsAtThis = itemsKey === '@this';
let normalizedSortProperties = normalizeSortProperties(sortProperties);
activeObservers = normalizedSortProperties.map(([prop]) => {
let path = itemsKeyIsAtThis ? `@each.${prop}` : `${itemsKey}.@each.${prop}`;
if (normalizedSortProperties.length === 0) {
let path = itemsKeyIsAtThis ? `[]` : `${itemsKey}.[]`;
addObserver(this, path, sortPropertyDidChange);
return [this, path, sortPropertyDidChange];
});
activeObservers = [[this, path, sortPropertyDidChange]];
} else {
activeObservers = normalizedSortProperties.map(([prop]) => {
let path = itemsKeyIsAtThis ? `@each.${prop}` : `${itemsKey}.@each.${prop}`;
addObserver(this, path, sortPropertyDidChange);
return [this, path, sortPropertyDidChange];
});
}

activeObserversMap.set(this, activeObservers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,56 @@ moduleFor(
);
}

['@test array should update if items to be sorted is replaced when sort properties array is empty'](
assert
) {
var o = EmberObject.extend({
sortedItems: sort('items', 'itemSorting'),
}).create({
itemSorting: emberA([]),
items: emberA([6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5]),
});

assert.deepEqual(
o.get('sortedItems'),
[6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5],
'array is not changed'
);

set(o, 'items', emberA([5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is testing "replacement" of items, not mutation.

Can you add another test that does o.get('items').pushObject(...)?


assert.deepEqual(
o.get('sortedItems'),
[5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4],
'array was updated'
);
}

['@test array should update if items to be sorted is mutated when sort properties array is empty'](
assert
) {
var o = EmberObject.extend({
sortedItems: sort('items', 'itemSorting'),
}).create({
itemSorting: emberA([]),
items: emberA([6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5]),
});

assert.deepEqual(
o.get('sortedItems'),
[6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5],
'array is not changed'
);

o.get('items').pushObject(12);

assert.deepEqual(
o.get('sortedItems'),
[6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12],
'array was updated'
);
}

['@test array observers do not leak'](assert) {
let daria = { name: 'Daria' };
let jane = { name: 'Jane' };
Expand Down