Skip to content

Commit

Permalink
Merge pull request #16034 from thoov/ember-metal-test-part-1
Browse files Browse the repository at this point in the history
[CLEANUP] Convert ember-metal tests to new style (Part 1)
  • Loading branch information
rwjblue committed Dec 30, 2017
2 parents d52eb87 + 0a07296 commit b6d4618
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 245 deletions.
254 changes: 127 additions & 127 deletions packages/ember-metal/tests/meta_test.js
@@ -1,155 +1,155 @@
import {
meta
} from '..';
import { meta } from '..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

QUnit.module('Ember.meta');
moduleFor('Ember.meta', class extends AbstractTestCase {
['@test should return the same hash for an object'](assert) {
let obj = {};

QUnit.test('should return the same hash for an object', function() {
let obj = {};
meta(obj).foo = 'bar';

meta(obj).foo = 'bar';

equal(meta(obj).foo, 'bar', 'returns same hash with multiple calls to Ember.meta()');
});

QUnit.test('meta is not enumerable', function () {
let proto, obj, props, prop;
proto = { foo: 'bar' };
meta(proto);
obj = Object.create(proto);
meta(obj);
obj.bar = 'baz';
props = [];
for (prop in obj) {
props.push(prop);
assert.equal(meta(obj).foo, 'bar', 'returns same hash with multiple calls to Ember.meta()');
}
deepEqual(props.sort(), ['bar', 'foo']);
if (typeof JSON !== 'undefined' && 'stringify' in JSON) {
try {
JSON.stringify(obj);
} catch (e) {
ok(false, 'meta should not fail JSON.stringify');

['@test meta is not enumerable'](assert) {
let proto, obj, props, prop;
proto = { foo: 'bar' };
meta(proto);
obj = Object.create(proto);
meta(obj);
obj.bar = 'baz';
props = [];
for (prop in obj) {
props.push(prop);
}
assert.deepEqual(props.sort(), ['bar', 'foo']);
if (typeof JSON !== 'undefined' && 'stringify' in JSON) {
try {
JSON.stringify(obj);
} catch (e) {
assert.ok(false, 'meta should not fail JSON.stringify');
}
}
}
});

QUnit.test('meta.listeners basics', function(assert) {
let t = {};
let m = meta({});
m.addToListeners('hello', t, 'm', 0);
let matching = m.matchingListeners('hello');
assert.equal(matching.length, 3);
assert.equal(matching[0], t);
m.removeFromListeners('hello', t, 'm');
matching = m.matchingListeners('hello');
assert.equal(matching, undefined);
});
['@test meta.listeners basics'](assert) {
let t = {};
let m = meta({});
m.addToListeners('hello', t, 'm', 0);
let matching = m.matchingListeners('hello');
assert.equal(matching.length, 3);
assert.equal(matching[0], t);
m.removeFromListeners('hello', t, 'm');
matching = m.matchingListeners('hello');
assert.equal(matching, undefined);
}

QUnit.test('meta.listeners inheritance', function(assert) {
let target = {};
let parent = {};
let parentMeta = meta(parent);
parentMeta.addToListeners('hello', target, 'm', 0);

let child = Object.create(parent);
let m = meta(child);

let matching = m.matchingListeners('hello');
assert.equal(matching.length, 3);
assert.equal(matching[0], target);
assert.equal(matching[1], 'm');
assert.equal(matching[2], 0);
m.removeFromListeners('hello', target, 'm');
matching = m.matchingListeners('hello');
assert.equal(matching, undefined);
matching = parentMeta.matchingListeners('hello');
assert.equal(matching.length, 3);
});
['@test meta.listeners inheritance'](assert) {
let target = {};
let parent = {};
let parentMeta = meta(parent);
parentMeta.addToListeners('hello', target, 'm', 0);

let child = Object.create(parent);
let m = meta(child);

let matching = m.matchingListeners('hello');
assert.equal(matching.length, 3);
assert.equal(matching[0], target);
assert.equal(matching[1], 'm');
assert.equal(matching[2], 0);
m.removeFromListeners('hello', target, 'm');
matching = m.matchingListeners('hello');
assert.equal(matching, undefined);
matching = parentMeta.matchingListeners('hello');
assert.equal(matching.length, 3);
}

QUnit.test('meta.listeners deduplication', function(assert) {
let t = {};
let m = meta({});
m.addToListeners('hello', t, 'm', 0);
m.addToListeners('hello', t, 'm', 0);
let matching = m.matchingListeners('hello');
assert.equal(matching.length, 3);
assert.equal(matching[0], t);
});
['@test meta.listeners deduplication'](assert) {
let t = {};
let m = meta({});
m.addToListeners('hello', t, 'm', 0);
m.addToListeners('hello', t, 'm', 0);
let matching = m.matchingListeners('hello');
assert.equal(matching.length, 3);
assert.equal(matching[0], t);
}

QUnit.test('meta.writeWatching issues useful error after destroy', function() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);
['@test meta.writeWatching issues useful error after destroy']() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);

targetMeta.destroy();
targetMeta.destroy();

expectAssertion(() => {
targetMeta.writeWatching('hello', 1);
}, 'Cannot update watchers for `hello` on `<special-sauce:123>` after it has been destroyed.');
});
expectAssertion(() => {
targetMeta.writeWatching('hello', 1);
}, 'Cannot update watchers for `hello` on `<special-sauce:123>` after it has been destroyed.');
}

QUnit.test('meta.writableTag issues useful error after destroy', function() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);
['@test meta.writableTag issues useful error after destroy']() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);

targetMeta.destroy();
targetMeta.destroy();

expectAssertion(() => {
targetMeta.writableTag(() => {});
}, 'Cannot create a new tag for `<special-sauce:123>` after it has been destroyed.');
});
expectAssertion(() => {
targetMeta.writableTag(() => {});
}, 'Cannot create a new tag for `<special-sauce:123>` after it has been destroyed.');
}

QUnit.test('meta.writableChainWatchers issues useful error after destroy', function() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);
['@test meta.writableChainWatchers issues useful error after destroy']() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);

targetMeta.destroy();
targetMeta.destroy();

expectAssertion(() => {
targetMeta.writableChainWatchers(() => {});
}, 'Cannot create a new chain watcher for `<special-sauce:123>` after it has been destroyed.');
});
expectAssertion(() => {
targetMeta.writableChainWatchers(() => {});
}, 'Cannot create a new chain watcher for `<special-sauce:123>` after it has been destroyed.');
}

QUnit.test('meta.writableChains issues useful error after destroy', function() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);
['@test meta.writableChains issues useful error after destroy']() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);

targetMeta.destroy();
targetMeta.destroy();

expectAssertion(() => {
targetMeta.writableChains(() => {});
}, 'Cannot create a new chains for `<special-sauce:123>` after it has been destroyed.');
});
expectAssertion(() => {
targetMeta.writableChains(() => {});
}, 'Cannot create a new chains for `<special-sauce:123>` after it has been destroyed.');
}

QUnit.test('meta.writeValues issues useful error after destroy', function() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);
['@test meta.writeValues issues useful error after destroy']() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);

targetMeta.destroy();
targetMeta.destroy();

expectAssertion(() => {
targetMeta.writeValues('derp', 'ohai');
}, 'Cannot set the value of `derp` on `<special-sauce:123>` after it has been destroyed.');
});
expectAssertion(() => {
targetMeta.writeValues('derp', 'ohai');
}, 'Cannot set the value of `derp` on `<special-sauce:123>` after it has been destroyed.');
}

QUnit.test('meta.writeDeps issues useful error after destroy', function() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);
['@test meta.writeDeps issues useful error after destroy']() {
let target = {
toString() { return '<special-sauce:123>'; }
};
let targetMeta = meta(target);

targetMeta.destroy();
targetMeta.destroy();

expectAssertion(() => {
targetMeta.writeDeps('derp', 'ohai', 1);
}, 'Cannot modify dependent keys for `ohai` on `<special-sauce:123>` after it has been destroyed.');
expectAssertion(() => {
targetMeta.writeDeps('derp', 'ohai', 1);
}, 'Cannot modify dependent keys for `ohai` on `<special-sauce:123>` after it has been destroyed.');
}
});

68 changes: 35 additions & 33 deletions packages/ember-metal/tests/performance_test.js
Expand Up @@ -8,6 +8,7 @@ import {
endPropertyChanges,
addObserver
} from '..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

/*
This test file is designed to capture performance regressions related to
Expand All @@ -16,50 +17,51 @@ import {
bugs that cause them to get evaluated more than necessary should be put here.
*/

QUnit.module('Computed Properties - Number of times evaluated');
moduleFor('Computed Properties - Number of times evaluated', class extends AbstractTestCase {
['@test computed properties that depend on multiple properties should run only once per run loop'](assert) {
let obj = { a: 'a', b: 'b', c: 'c' };
let cpCount = 0;
let obsCount = 0;

QUnit.test('computed properties that depend on multiple properties should run only once per run loop', function() {
let obj = { a: 'a', b: 'b', c: 'c' };
let cpCount = 0;
let obsCount = 0;
defineProperty(obj, 'abc', computed(function(key) {
cpCount++;
return 'computed ' + key;
}).property('a', 'b', 'c'));

defineProperty(obj, 'abc', computed(function(key) {
cpCount++;
return 'computed ' + key;
}).property('a', 'b', 'c'));
get(obj, 'abc');

get(obj, 'abc');
cpCount = 0;

cpCount = 0;
addObserver(obj, 'abc', function() {
obsCount++;
});

addObserver(obj, 'abc', function() {
obsCount++;
});
beginPropertyChanges();
set(obj, 'a', 'aa');
set(obj, 'b', 'bb');
set(obj, 'c', 'cc');
endPropertyChanges();

beginPropertyChanges();
set(obj, 'a', 'aa');
set(obj, 'b', 'bb');
set(obj, 'c', 'cc');
endPropertyChanges();
get(obj, 'abc');

get(obj, 'abc');
assert.equal(cpCount, 1, 'The computed property is only invoked once');
assert.equal(obsCount, 1, 'The observer is only invoked once');
}

equal(cpCount, 1, 'The computed property is only invoked once');
equal(obsCount, 1, 'The observer is only invoked once');
});

QUnit.test('computed properties are not executed if they are the last segment of an observer chain pain', function() {
let foo = { bar: { baz: { } } };
['@test computed properties are not executed if they are the last segment of an observer chain pain'](assert) {
let foo = { bar: { baz: { } } };

let count = 0;
let count = 0;

defineProperty(foo.bar.baz, 'bam', computed(function() {
count++;
}));
defineProperty(foo.bar.baz, 'bam', computed(function() {
count++;
}));

addObserver(foo, 'bar.baz.bam', function() {});
addObserver(foo, 'bar.baz.bam', function() {});

propertyDidChange(get(foo, 'bar.baz'), 'bam');
propertyDidChange(get(foo, 'bar.baz'), 'bam');

equal(count, 0, 'should not have recomputed property');
assert.equal(count, 0, 'should not have recomputed property');
}
});

0 comments on commit b6d4618

Please sign in to comment.