Skip to content

Commit

Permalink
[BUGFIX beta] Fix ChainNode unchaining
Browse files Browse the repository at this point in the history
  • Loading branch information
mmun committed Feb 23, 2018
1 parent 67bf23f commit 94f2b2f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class ChainNode {
let node = chains[key];

// unchain rest of path first...
if (tails.length > 1) {
if (tails.length > 0) {
node.unchain(tails.shift(), tails);
}

Expand Down
49 changes: 48 additions & 1 deletion packages/ember-metal/tests/chains_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
computed,
notifyPropertyChange,
peekMeta,
meta
meta,
watch,
unwatch,
watcherCount
} from '..';

QUnit.module('Chains');
Expand Down Expand Up @@ -96,3 +99,47 @@ QUnit.test('checks cache correctly', function(assert) {

assert.strictEqual(chainNode.value(), undefined);
});

QUnit.test('chains are watched correctly', function(assert) {
let obj = { foo: { bar: { baz: 1 } } };

watch(obj, 'foo.bar.baz');

assert.equal(watcherCount(obj, 'foo'), 1);
assert.equal(watcherCount(obj, 'foo.bar'), 0);
assert.equal(watcherCount(obj, 'foo.bar.baz'), 1);
assert.equal(watcherCount(obj.foo, 'bar'), 1);
assert.equal(watcherCount(obj.foo, 'bar.baz'), 0);
assert.equal(watcherCount(obj.foo.bar, 'baz'), 1);

unwatch(obj, 'foo.bar.baz');

assert.equal(watcherCount(obj, 'foo'), 0);
assert.equal(watcherCount(obj, 'foo.bar'), 0);
assert.equal(watcherCount(obj, 'foo.bar.baz'), 0);
assert.equal(watcherCount(obj.foo, 'bar'), 0);
assert.equal(watcherCount(obj.foo, 'bar.baz'), 0);
assert.equal(watcherCount(obj.foo.bar, 'baz'), 0);
});

QUnit.test('chains with single character keys are watched correctly', function (assert) {
let obj = { a: { b: { c: 1 } } };

watch(obj, 'a.b.c');

assert.equal(watcherCount(obj, 'a'), 1);
assert.equal(watcherCount(obj, 'a.b'), 0);
assert.equal(watcherCount(obj, 'a.b.c'), 1);
assert.equal(watcherCount(obj.a, 'b'), 1);
assert.equal(watcherCount(obj.a, 'b.c'), 0);
assert.equal(watcherCount(obj.a.b, 'c'), 1);

unwatch(obj, 'a.b.c');

assert.equal(watcherCount(obj, 'a'), 0);
assert.equal(watcherCount(obj, 'a.b'), 0);
assert.equal(watcherCount(obj, 'a.b.c'), 0);
assert.equal(watcherCount(obj.a, 'b'), 0);
assert.equal(watcherCount(obj.a, 'b.c'), 0);
assert.equal(watcherCount(obj.a.b, 'c'), 0);
});

0 comments on commit 94f2b2f

Please sign in to comment.