-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12991 from mmun/fix-computed-sort
[BUGFIX beta] Fix a regression in Ember.computed.sort
- Loading branch information
Showing
2 changed files
with
150 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ObjectProxy from 'ember-runtime/system/object_proxy'; | |
import { get } from 'ember-metal/property_get'; | ||
import { set } from 'ember-metal/property_set'; | ||
import { addObserver } from 'ember-metal/observer'; | ||
import { computed } from 'ember-metal/computed'; | ||
import { observer } from 'ember-metal/mixin'; | ||
import { | ||
sum, | ||
|
@@ -1203,10 +1204,10 @@ QUnit.module('sort - stability', { | |
sortedItems: sort('items', 'sortProps') | ||
}).create({ | ||
items: [ | ||
{ name: 'A', count: 1 }, | ||
{ name: 'B', count: 1 }, | ||
{ name: 'C', count: 1 }, | ||
{ name: 'D', count: 1 } | ||
{ name: 'A', count: 1, thing: 4 }, | ||
{ name: 'B', count: 1, thing: 3 }, | ||
{ name: 'C', count: 1, thing: 2 }, | ||
{ name: 'D', count: 1, thing: 4 } | ||
] | ||
}); | ||
}, | ||
|
@@ -1223,19 +1224,20 @@ QUnit.test('sorts correctly as only one property changes', function() { | |
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'B', 'C', 'D'], 'final'); | ||
}); | ||
|
||
|
||
var klass; | ||
QUnit.module('sort - concurrency', { | ||
setup() { | ||
obj = EmberObject.extend({ | ||
klass = EmberObject.extend({ | ||
sortProps: ['count'], | ||
sortedItems: sort('items', 'sortProps'), | ||
customSortedItems: sort('[email protected]', (a, b) => a.count - b.count) | ||
}).create({ | ||
}); | ||
obj = klass.create({ | ||
items: emberA([ | ||
{ name: 'A', count: 1 }, | ||
{ name: 'B', count: 2 }, | ||
{ name: 'C', count: 3 }, | ||
{ name: 'D', count: 4 } | ||
{ name: 'A', count: 1, thing: 4, id: 1 }, | ||
{ name: 'B', count: 2, thing: 3, id: 2 }, | ||
{ name: 'C', count: 3, thing: 2, id: 3 }, | ||
{ name: 'D', count: 4, thing: 1, id: 4 } | ||
]) | ||
}); | ||
}, | ||
|
@@ -1255,7 +1257,7 @@ QUnit.test('sorts correctly after mutation to the sort properties', function() { | |
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'D', 'B', 'C'], 'final'); | ||
}); | ||
|
||
QUnit.test('sort correctl after mutation to the sor ', function() { | ||
QUnit.test('sort correctly after mutation to the sort', function() { | ||
deepEqual(obj.get('customSortedItems').mapBy('name'), ['A', 'B', 'C', 'D'], 'initial'); | ||
|
||
set(obj.get('items')[1], 'count', 5); | ||
|
@@ -1266,6 +1268,84 @@ QUnit.test('sort correctl after mutation to the sor ', function() { | |
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'D', 'B', 'C'], 'final'); | ||
}); | ||
|
||
QUnit.test('sort correctly on multiple instances of the same class', function() { | ||
var obj2 = klass.create({ | ||
items: emberA([ | ||
{ name: 'W', count: 23, thing: 4 }, | ||
{ name: 'X', count: 24, thing: 3 }, | ||
{ name: 'Y', count: 25, thing: 2 }, | ||
{ name: 'Z', count: 26, thing: 1 } | ||
]) | ||
}); | ||
|
||
deepEqual(obj2.get('sortedItems').mapBy('name'), ['W', 'X', 'Y', 'Z'], 'initial'); | ||
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'B', 'C', 'D'], 'initial'); | ||
|
||
set(obj.get('items')[1], 'count', 5); | ||
set(obj.get('items')[2], 'count', 6); | ||
set(obj2.get('items')[1], 'count', 27); | ||
set(obj2.get('items')[2], 'count', 28); | ||
|
||
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'D', 'B', 'C'], 'final'); | ||
deepEqual(obj2.get('sortedItems').mapBy('name'), ['W', 'Z', 'X', 'Y'], 'final'); | ||
|
||
obj.set('sortProps', ['thing']); | ||
|
||
deepEqual(obj.get('sortedItems').mapBy('name'), ['D', 'C', 'B', 'A'], 'final'); | ||
|
||
obj2.notifyPropertyChange('sortedItems'); // invalidate to flush, to get DK refreshed | ||
obj2.get('sortedItems'); // flush to get updated DK | ||
|
||
obj2.set('items.firstObject.count', 9999); | ||
|
||
deepEqual(obj2.get('sortedItems').mapBy('name'), ['Z', 'X', 'Y', 'W'], 'final'); | ||
}); | ||
|
||
|
||
QUnit.test('sort correctly when multiple sorts are chained on the same instance of a class', function() { | ||
var obj2 = klass.extend({ | ||
items: computed('sibling.sortedItems.[]', function() { | ||
return this.get('sibling.sortedItems'); | ||
}), | ||
asdf: observer('sibling.sortedItems.[]', function() { | ||
this.get('sibling.sortedItems'); | ||
}) | ||
}).create({ | ||
sibling: obj | ||
}); | ||
|
||
/* | ||
┌───────────┐ ┌────────────┐ | ||
│sortedProps│ │sortedProps2│ | ||
└───────────┘ └────────────┘ | ||
▲ ▲ | ||
│ ╔═══════════╗ │ | ||
│─ ─ ─ ─ ─ ─ ─ ▶║ CP (sort) ║◀─ ─ ─ ─ ─ ─ ─ ┤ | ||
│ ╚═══════════╝ │ | ||
│ │ | ||
┌───────────┐ ┏━━━━━━━━━━━┓ ┏━━━━━━━━━━━━┓ | ||
│ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┃ ┃ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┃ ┃ | ||
│ items │◀── [email protected] │◀──┃sortedItems┃◀─── [email protected] │◀───┃sortedItems2┃ | ||
│ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┃ ┃ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┃ ┃ | ||
└───────────┘ ┗━━━━━━━━━━━┛ ┗━━━━━━━━━━━━┛ | ||
*/ | ||
|
||
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'B', 'C', 'D'], 'obj.sortedItems.name should be sorted alpha'); | ||
deepEqual(obj2.get('sortedItems').mapBy('name'), ['A', 'B', 'C', 'D'], 'obj2.sortedItems.name should be sorted alpha'); | ||
|
||
set(obj.get('items')[1], 'count', 5); | ||
set(obj.get('items')[2], 'count', 6); | ||
|
||
deepEqual(obj.get('sortedItems').mapBy('name'), ['A', 'D', 'B', 'C'], 'obj.sortedItems.name should now have changed'); | ||
deepEqual(obj2.get('sortedItems').mapBy('name'), ['A', 'D', 'B', 'C'], 'obj2.sortedItems.name should still mirror sortedItems2'); | ||
|
||
obj.set('sortProps', ['thing']); | ||
obj2.set('sortProps', ['id']); | ||
|
||
deepEqual(obj2.get('sortedItems').mapBy('name'), ['A', 'B', 'C', 'D'], 'we now sort obj2 by id, so we expect a b c d'); | ||
deepEqual(obj.get('sortedItems').mapBy('name'), ['D', 'C', 'B', 'A'], 'we now sort obj by thing'); | ||
}); | ||
|
||
QUnit.module('max', { | ||
setup() { | ||
obj = EmberObject.extend({ | ||
|