Skip to content

Commit

Permalink
Failing test for #14672
Browse files Browse the repository at this point in the history
PROPERTY_DID_CHANGE is the mechanism by which
Ember's Glimmer refs are dirtied.  Currently if
something watches then unwatches an alias, the DK
that is installed by the alias is removed.
  • Loading branch information
krisselden committed Dec 6, 2016
1 parent 01fb8ea commit 5d64229
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion packages/ember-metal/tests/watching/watch_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { meta } from '../../meta';
import { set } from '../../property_set';
import get from '../../property_get';
import { computed } from '../../computed';
import alias from '../../alias';
import { defineProperty } from '../../properties';
import { testBoth } from 'internal-test-helpers';
import { addListener } from '../../events';
import {
watch,
unwatch,
destroy
destroy,
isWatching
} from '../../watching';
import {
PROPERTY_DID_CHANGE
} from '../../property_events';

let willCount, didCount, willKeys, didKeys, originalLookup;

Expand Down Expand Up @@ -253,3 +258,60 @@ testBoth('watch + Ember.set + no-descriptor', function(get, set) {
equal(child.b, 1, 'child.b (after watch)');
equal(get(child, 'b'), 1, 'Ember.get(child, "b") (after watch)');
});

testBoth('PROPERTY_DID_CHANGE + alias + cp', function(get, set) {
let counts = {};
let obj = {
child: {},
[PROPERTY_DID_CHANGE](keyName) {
counts[keyName] = (counts[keyName] || 0) + 1;
}
};

defineProperty(obj, 'cost', alias('child.cost'));
defineProperty(obj, 'tax', alias('child.tax'));

defineProperty(obj, 'total', computed('cost', 'tax', {
get() {
return get(this, 'cost') + get(this, 'tax');
}
}));


ok(!isWatching(obj, 'child.cost'), 'precond alias target `child.cost` is not watched');
equal(get(obj, 'cost'), undefined);
// this is how PROPERTY_DID_CHANGE will get notified
ok(isWatching(obj, 'child.cost'), 'alias target `child.cost` is watched after consumption');

ok(!isWatching(obj, 'child.tax'), 'precond alias target `child.cost` is not watched');
equal(get(obj, 'tax'), undefined);
// this is how PROPERTY_DID_CHANGE will get notified
ok(isWatching(obj, 'child.tax'), 'alias target `child.cost` is watched after consumption');

// increments the watching count on the alias itself to 1
ok(isNaN(get(obj, 'total')), 'total is initialized');

// decrements the watching count on the alias itself to 0
set(obj, 'child', {
cost: 399.00,
tax: 32.93
});

// this should have called PROPERTY_DID_CHANGE for all of them
equal(counts['cost'], 1, 'PROPERTY_DID_CHANGE called with cost');
equal(counts['tax'], 1, 'PROPERTY_DID_CHANGE called with tax');
equal(counts['total'], 1, 'PROPERTY_DID_CHANGE called with total');

// we should still have a dependency installed
ok(isWatching(obj, 'child.cost'), 'watching child.cost');
ok(isWatching(obj, 'child.tax'), 'watching child.tax');

set(obj, 'child', {
cost: 100.00,
tax: 10.00
});

equal(counts['cost'], 2, 'PROPERTY_DID_CHANGE called with cost');
equal(counts['tax'], 2, 'PROPERTY_DID_CHANGE called with tax');
equal(counts['total'], 1, 'PROPERTY_DID_CHANGE called with total');
});

0 comments on commit 5d64229

Please sign in to comment.