Skip to content

Commit

Permalink
fix tests, sort macro
Browse files Browse the repository at this point in the history
  • Loading branch information
pzuraq committed Oct 15, 2018
1 parent da28cea commit ef21456
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ moduleFor(
['@test should be able to render an unbound helper invocation for helpers with dependent keys']() {
this.registerHelper('capitalizeName', {
destroy() {
this.removeObserver('value.firstName');
this.removeObserver('value.firstName', this, this.recompute);
this._super(...arguments);
},

compute([value]) {
if (this.get('value')) {
this.removeObserver('value.firstName');
this.removeObserver('value.firstName', this, this.recompute);
}
this.set('value', value);
this.addObserver('value.firstName', this, this.recompute);
Expand All @@ -382,8 +382,8 @@ moduleFor(
this._super(...arguments);
},
teardown() {
this.removeObserver('value.firstName');
this.removeObserver('value.lastName');
this.removeObserver('value.firstName', this, this.recompute);
this.removeObserver('value.lastName', this, this.recompute);
},
compute([value]) {
if (this.get('value')) {
Expand Down Expand Up @@ -478,13 +478,13 @@ moduleFor(
['@test should be able to render an unbound helper invocation with bound hash options']() {
this.registerHelper('capitalizeName', {
destroy() {
this.removeObserver('value.firstName');
this.removeObserver('value.firstName', this, this.recompute);
this._super(...arguments);
},

compute([value]) {
if (this.get('value')) {
this.removeObserver('value.firstName');
this.removeObserver('value.firstName', this, this.recompute);
}
this.set('value', value);
this.addObserver('value.firstName', this, this.recompute);
Expand All @@ -498,8 +498,8 @@ moduleFor(
this._super(...arguments);
},
teardown() {
this.removeObserver('value.firstName');
this.removeObserver('value.lastName');
this.removeObserver('value.firstName', this, this.recompute);
this.removeObserver('value.lastName', this, this.recompute);
},
compute([value]) {
if (this.get('value')) {
Expand Down
5 changes: 3 additions & 2 deletions packages/@ember/-internals/metal/tests/alias_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,22 @@ moduleFor(
redefining the alias on the instance to another property dependent on same key
does not call the observer twice`](assert) {
let obj1 = Object.create(null);
obj1.incrementCount = incrementCount;

meta(obj1).proto = obj1;

defineProperty(obj1, 'foo', null, null);
defineProperty(obj1, 'bar', alias('foo'));
defineProperty(obj1, 'baz', alias('foo'));
addObserver(obj1, 'baz', incrementCount);
addObserver(obj1, 'baz', null, 'incrementCount');

let obj2 = Object.create(obj1);
defineProperty(obj2, 'baz', alias('bar')); // override baz

set(obj2, 'foo', 'FOO');
assert.equal(count, 1);

removeObserver(obj2, 'baz', incrementCount);
removeObserver(obj2, 'baz', null, 'incrementCount');

set(obj2, 'foo', 'OOF');
assert.equal(count, 1);
Expand Down
46 changes: 26 additions & 20 deletions packages/@ember/-internals/metal/tests/events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ moduleFor(
}

['@test listeners should be inherited'](assert) {
let obj = {};
let count = 0;
let F = function() {
count++;

let obj = {
func() {
count++;
},
};

addListener(obj, 'event!', F);
addListener(obj, 'event!', null, 'func');

let obj2 = Object.create(obj);

Expand All @@ -41,7 +43,7 @@ moduleFor(
sendEvent(obj2, 'event!');
assert.equal(count, 1, 'received event');

removeListener(obj2, 'event!', F);
removeListener(obj2, 'event!', null, 'func');

count = 0;
sendEvent(obj2, 'event!');
Expand All @@ -52,13 +54,15 @@ moduleFor(
}

['@test adding a listener more than once should only invoke once'](assert) {
let obj = {};
let count = 0;
function F() {
count++;
}
addListener(obj, 'event!', F);
addListener(obj, 'event!', F);
let obj = {
func() {
count++;
},
};

addListener(obj, 'event!', null, 'func');
addListener(obj, 'event!', null, 'func');

sendEvent(obj, 'event!');
assert.equal(count, 1, 'should only invoke once');
Expand Down Expand Up @@ -135,19 +139,21 @@ moduleFor(
}

['@test calling removeListener without method should remove all listeners'](assert) {
let obj = {};
function F() {}
function F2() {}
expectDeprecation(() => {
let obj = {};
function F() {}
function F2() {}

assert.equal(hasListeners(obj, 'event!'), false, 'no listeners at first');
assert.equal(hasListeners(obj, 'event!'), false, 'no listeners at first');

addListener(obj, 'event!', F);
addListener(obj, 'event!', F2);
addListener(obj, 'event!', F);
addListener(obj, 'event!', F2);

assert.equal(hasListeners(obj, 'event!'), true, 'has listeners');
removeListener(obj, 'event!');
assert.equal(hasListeners(obj, 'event!'), true, 'has listeners');
removeListener(obj, 'event!');

assert.equal(hasListeners(obj, 'event!'), false, 'has no more listeners');
assert.equal(hasListeners(obj, 'event!'), false, 'has no more listeners');
}, /The remove all functionality of removeListener and removeObserver has been deprecated/);
}

['@test a listener can be added as part of a mixin'](assert) {
Expand Down
4 changes: 4 additions & 0 deletions packages/@ember/-internals/metal/tests/observer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ moduleFor(
let yetAnotherBeer = new Beer();
addObserver(yetAnotherBeer, 'type', K);
set(yetAnotherBeer, 'type', 'ale');
addObserver(beer, 'type', K);
removeObserver(beer, 'type', K);
assert.deepEqual(
Object.keys(yetAnotherBeer),
Expand All @@ -923,6 +924,7 @@ moduleFor(

let itsMyLastBeer = new Beer();
set(itsMyLastBeer, 'type', 'ale');
addObserver(beer, 'type', K);
removeObserver(beer, 'type', K);
assert.deepEqual(Object.keys(itsMyLastBeer), ['type'], 'set -> removeObserver');
}
Expand All @@ -945,6 +947,7 @@ moduleFor(
let yetAnotherBeer = new Beer();
addObserver(yetAnotherBeer, 'type', K);
set(yetAnotherBeer, 'type', 'ale');
addObserver(beer, 'type', K);
removeObserver(beer, 'type', K);
assert.deepEqual(
Object.keys(yetAnotherBeer),
Expand All @@ -954,6 +957,7 @@ moduleFor(

let itsMyLastBeer = new Beer();
set(itsMyLastBeer, 'type', 'ale');
addObserver(beer, 'type', K);
removeObserver(beer, 'type', K);
assert.deepEqual(Object.keys(itsMyLastBeer), ['type'], 'set -> removeObserver');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

function testObserver(assert, setup, teardown, key = 'key') {
let obj = {};
function fn() {}

assert.equal(isWatching(obj, key), false, 'precond - isWatching is false by default');
setup(obj, key, fn);
setup(obj, key, 'fn');
assert.equal(isWatching(obj, key), true, 'isWatching is true when observers are added');
teardown(obj, key, fn);
teardown(obj, key, 'fn');
assert.equal(isWatching(obj, key), false, 'isWatching is false after observers are removed');
}

Expand All @@ -29,7 +28,7 @@ moduleFor(
assert,
(obj, key, fn) => {
Mixin.create({
didChange: observer(key, fn),
[fn]: observer(key, function() {}),
}).apply(obj);
},
(obj, key, fn) => removeObserver(obj, key, obj, fn)
Expand Down Expand Up @@ -62,21 +61,21 @@ moduleFor(
testObserver(
assert,
(obj, key, fn) => {
defineProperty(obj, 'computed', computed(fn).property(key));
get(obj, 'computed');
defineProperty(obj, fn, computed(function() {}).property(key));
get(obj, fn);
},
obj => defineProperty(obj, 'computed', null)
(obj, key, fn) => defineProperty(obj, fn, null)
);
}

['@test isWatching is true for chained computed properties'](assert) {
testObserver(
assert,
(obj, key, fn) => {
defineProperty(obj, 'computed', computed(fn).property(key + '.bar'));
get(obj, 'computed');
defineProperty(obj, fn, computed(function() {}).property(key + '.bar'));
get(obj, fn);
},
obj => defineProperty(obj, 'computed', null)
(obj, key, fn) => defineProperty(obj, fn, null)
);
}

Expand Down
12 changes: 10 additions & 2 deletions packages/@ember/object/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,17 @@ function propertySort(itemsKey, sortPropertiesKey) {
let activeObserversMap = cp._activeObserverMap || (cp._activeObserverMap = new WeakMap());
let activeObservers = activeObserversMap.get(this);

function sortPropertyDidChange() {
this.notifyPropertyChange(key);
let sortPropertyDidChangeMap =
cp._sortPropertyDidChangeMap || (cp._sortPropertyDidChangeMap = new WeakMap());

if (!sortPropertyDidChangeMap.has(this)) {
sortPropertyDidChangeMap.set(this, function() {
this.notifyPropertyChange(key);
});
}

let sortPropertyDidChange = sortPropertyDidChangeMap.get(this);

if (activeObservers !== undefined) {
activeObservers.forEach(path => removeObserver(this, path, sortPropertyDidChange));
}
Expand Down Expand Up @@ -871,6 +878,7 @@ function propertySort(itemsKey, sortPropertiesKey) {
);

cp._activeObserverMap = undefined;
cp._sortPropertyDidChangeMap = undefined;

return cp;
}
Expand Down

0 comments on commit ef21456

Please sign in to comment.