Skip to content
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
82 changes: 42 additions & 40 deletions packages/ember-metal/tests/watching/is_watching_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
removeObserver,
isWatching
} from '../..';

QUnit.module('isWatching');
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

function testObserver(assert, setup, teardown, key = 'key') {
let obj = {};
Expand All @@ -22,47 +21,50 @@ function testObserver(assert, setup, teardown, key = 'key') {
assert.equal(isWatching(obj, key), false, 'isWatching is false after observers are removed');
}

QUnit.test('isWatching is true for regular local observers', function(assert) {
testObserver(assert, (obj, key, fn) => {
Mixin.create({
didChange: observer(key, fn)
}).apply(obj);
}, (obj, key, fn) => removeObserver(obj, key, obj, fn));
});
moduleFor('isWatching', class extends AbstractTestCase {
['@test isWatching is true for regular local observers'](assert) {
testObserver(assert, (obj, key, fn) => {
Mixin.create({
didChange: observer(key, fn)
}).apply(obj);
}, (obj, key, fn) => removeObserver(obj, key, obj, fn));
}

QUnit.test('isWatching is true for nonlocal observers', function(assert) {
testObserver(assert, (obj, key, fn) => {
addObserver(obj, key, obj, fn);
}, (obj, key, fn) => removeObserver(obj, key, obj, fn));
});
['@test isWatching is true for nonlocal observers'](assert) {
testObserver(assert, (obj, key, fn) => {
addObserver(obj, key, obj, fn);
}, (obj, key, fn) => removeObserver(obj, key, obj, fn));
}

QUnit.test('isWatching is true for chained observers', function(assert) {
testObserver(assert, function(obj, key, fn) {
addObserver(obj, key + '.bar', obj, fn);
}, function(obj, key, fn) {
removeObserver(obj, key + '.bar', obj, fn);
});
});
['@test isWatching is true for chained observers'](assert) {
testObserver(assert, function(obj, key, fn) {
addObserver(obj, key + '.bar', obj, fn);
}, function(obj, key, fn) {
removeObserver(obj, key + '.bar', obj, fn);
});
}

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

QUnit.test('isWatching is true for chained computed properties', function(assert) {
testObserver(assert, (obj, key, fn) => {
defineProperty(obj, 'computed', computed(fn).property(key + '.bar'));
get(obj, 'computed');
}, obj => defineProperty(obj, 'computed', 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');
}, obj => defineProperty(obj, 'computed', null));
}

// can't watch length on Array - it is special...
// But you should be able to watch a length property of an object
QUnit.test('isWatching is true for \'length\' property on object', function(assert) {
testObserver(assert, (obj, key, fn) => {
defineProperty(obj, 'length', null, '26.2 miles');
addObserver(obj, 'length', obj, fn);
}, (obj, key, fn) => removeObserver(obj, 'length', obj, fn), 'length');
// can't watch length on Array - it is special...
// But you should be able to watch a length property of an object
['@test isWatching is true for \'length\' property on object'](assert) {
testObserver(assert, (obj, key, fn) => {
defineProperty(obj, 'length', null, '26.2 miles');
addObserver(obj, 'length', obj, fn);
}, (obj, key, fn) => removeObserver(obj, 'length', obj, fn), 'length');
}
});

185 changes: 94 additions & 91 deletions packages/ember-metal/tests/watching/unwatch_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { testBoth } from 'internal-test-helpers';
import {
watch,
unwatch,
Expand All @@ -7,110 +6,114 @@ import {
computed,
set
} from '../..';
import {
moduleFor,
AbstractTestCase
} from 'internal-test-helpers';

let willCount, didCount;

QUnit.module('unwatch', {
beforeEach() {
willCount = didCount = 0;
}
});

function addListeners(obj, keyPath) {
addListener(obj, keyPath + ':before', () => willCount++);
addListener(obj, keyPath + ':change', () => didCount++);
}

testBoth('unwatching a computed property - regular get/set', function(get, set, assert) {
let obj = {};

defineProperty(obj, 'foo', computed({
get() {
return this.__foo;
},
set(keyName, value) {
this.__foo = value;
return this.__foo;
}
}));
addListeners(obj, 'foo');

watch(obj, 'foo');
set(obj, 'foo', 'bar');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
});
moduleFor('unwatch', class extends AbstractTestCase {
beforeEach() {
willCount = didCount = 0;
}

['@test unwatching a computed property - regular get/set'](assert) {
let obj = {};

defineProperty(obj, 'foo', computed({
get() {
return this.__foo;
},
set(keyName, value) {
this.__foo = value;
return this.__foo;
}
}));
addListeners(obj, 'foo');

watch(obj, 'foo');
set(obj, 'foo', 'bar');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
}

testBoth('unwatching a regular property - regular get/set', function(get, set, assert) {
let obj = { foo: 'BIFF' };
addListeners(obj, 'foo');
['@test unwatching a regular property - regular get/set'](assert) {
let obj = { foo: 'BIFF' };
addListeners(obj, 'foo');

watch(obj, 'foo');
set(obj, 'foo', 'bar');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');
watch(obj, 'foo');
set(obj, 'foo', 'bar');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
});
unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
}

QUnit.test('unwatching should be nested', function(assert) {
let obj = { foo: 'BIFF' };
addListeners(obj, 'foo');

watch(obj, 'foo');
watch(obj, 'foo');
set(obj, 'foo', 'bar');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 1, 'should NOT have invoked willCount');
assert.equal(didCount, 1, 'should NOT have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
});
['@test unwatching should be nested'](assert) {
let obj = { foo: 'BIFF' };
addListeners(obj, 'foo');

testBoth('unwatching "length" property on an object', function(get, set, assert) {
let obj = { foo: 'RUN' };
addListeners(obj, 'length');

// Can watch length when it is undefined
watch(obj, 'length');
set(obj, 'length', '10k');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

// Should stop watching despite length now being defined (making object 'array-like')
unwatch(obj, 'length');
willCount = didCount = 0;
set(obj, 'length', '5k');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
});
watch(obj, 'foo');
watch(obj, 'foo');
set(obj, 'foo', 'bar');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 1, 'should NOT have invoked willCount');
assert.equal(didCount, 1, 'should NOT have invoked didCount');

unwatch(obj, 'foo');
willCount = didCount = 0;
set(obj, 'foo', 'BAZ');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
}

testBoth('unwatching should not destroy non MANDATORY_SETTER descriptor', function(get, set, assert) {
let obj = { get foo() { return 'RUN'; } };
['@test unwatching "length" property on an object'](assert) {
let obj = { foo: 'RUN' };
addListeners(obj, 'length');

assert.equal(obj.foo, 'RUN', 'obj.foo');
watch(obj, 'foo');
assert.equal(obj.foo, 'RUN', 'obj.foo after watch');
unwatch(obj, 'foo');
assert.equal(obj.foo, 'RUN', 'obj.foo after unwatch');
// Can watch length when it is undefined
watch(obj, 'length');
set(obj, 'length', '10k');
assert.equal(willCount, 1, 'should have invoked willCount');
assert.equal(didCount, 1, 'should have invoked didCount');

// Should stop watching despite length now being defined (making object 'array-like')
unwatch(obj, 'length');
willCount = didCount = 0;
set(obj, 'length', '5k');
assert.equal(willCount, 0, 'should NOT have invoked willCount');
assert.equal(didCount, 0, 'should NOT have invoked didCount');
}

['@test unwatching should not destroy non MANDATORY_SETTER descriptor'](assert) {
let obj = { get foo() { return 'RUN'; } };

assert.equal(obj.foo, 'RUN', 'obj.foo');
watch(obj, 'foo');
assert.equal(obj.foo, 'RUN', 'obj.foo after watch');
unwatch(obj, 'foo');
assert.equal(obj.foo, 'RUN', 'obj.foo after unwatch');
}
});

Loading