Skip to content

Commit

Permalink
[CLEANUP] Convert ember-metal tests over to new style
Browse files Browse the repository at this point in the history
Apart of emberjs#15988
  • Loading branch information
thoov committed Mar 16, 2018
1 parent 0931537 commit ce4065d
Show file tree
Hide file tree
Showing 18 changed files with 2,157 additions and 2,112 deletions.
141 changes: 72 additions & 69 deletions packages/ember-metal/tests/alias_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,105 @@ import {
removeObserver,
tagFor
} from '..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

let obj, count;

QUnit.module('ember-metal/alias', {
function incrementCount() {
count++;
}

moduleFor('ember-metal/alias', class extends AbstractTestCase {
beforeEach() {
obj = { foo: { faz: 'FOO' } };
count = 0;
},
}

afterEach() {
obj = null;
}
});

function incrementCount() {
count++;
}
['@test should proxy get to alt key'](assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
assert.equal(get(obj, 'bar'), 'FOO');
}

QUnit.test('should proxy get to alt key', function(assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
assert.equal(get(obj, 'bar'), 'FOO');
});
['@test should proxy set to alt key'](assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
set(obj, 'bar', 'BAR');
assert.equal(get(obj, 'foo.faz'), 'BAR');
}

QUnit.test('should proxy set to alt key', function(assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
set(obj, 'bar', 'BAR');
assert.equal(get(obj, 'foo.faz'), 'BAR');
});
['@test old dependent keys should not trigger property changes'](assert) {
let obj1 = Object.create(null);
defineProperty(obj1, 'foo', null, null);
defineProperty(obj1, 'bar', alias('foo'));
defineProperty(obj1, 'baz', alias('foo'));
defineProperty(obj1, 'baz', alias('bar')); // redefine baz
addObserver(obj1, 'baz', incrementCount);

QUnit.test('old dependent keys should not trigger property changes', function(assert) {
let obj1 = Object.create(null);
defineProperty(obj1, 'foo', null, null);
defineProperty(obj1, 'bar', alias('foo'));
defineProperty(obj1, 'baz', alias('foo'));
defineProperty(obj1, 'baz', alias('bar')); // redefine baz
addObserver(obj1, 'baz', incrementCount);
set(obj1, 'foo', 'FOO');
assert.equal(count, 1);

set(obj1, 'foo', 'FOO');
assert.equal(count, 1);
removeObserver(obj1, 'baz', incrementCount);

removeObserver(obj1, 'baz', incrementCount);
set(obj1, 'foo', 'OOF');
assert.equal(count, 1);
}

set(obj1, 'foo', 'OOF');
assert.equal(count, 1);
});
[`@test inheriting an observer of the alias from the prototype then
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);

QUnit.test(`inheriting an observer of the alias from the prototype then
redefining the alias on the instance to another property dependent on same key
does not call the observer twice`, function(assert) {
let obj1 = Object.create(null);
meta(obj1).proto = obj1;

meta(obj1).proto = obj1;
defineProperty(obj1, 'foo', null, null);
defineProperty(obj1, 'bar', alias('foo'));
defineProperty(obj1, 'baz', alias('foo'));
addObserver(obj1, 'baz', incrementCount);

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

let obj2 = Object.create(obj1);
defineProperty(obj2, 'baz', alias('bar')); // override baz
set(obj2, 'foo', 'FOO');
assert.equal(count, 1);

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

removeObserver(obj2, 'baz', incrementCount);
set(obj2, 'foo', 'OOF');
assert.equal(count, 1);
}

set(obj2, 'foo', 'OOF');
assert.equal(count, 1);
});
['@test an observer of the alias works if added after defining the alias'](assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
addObserver(obj, 'bar', incrementCount);
assert.ok(isWatching(obj, 'foo.faz'));
set(obj, 'foo.faz', 'BAR');
assert.equal(count, 1);
}

QUnit.test('an observer of the alias works if added after defining the alias', function(assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
addObserver(obj, 'bar', incrementCount);
assert.ok(isWatching(obj, 'foo.faz'));
set(obj, 'foo.faz', 'BAR');
assert.equal(count, 1);
});
['@test an observer of the alias works if added before defining the alias'](assert) {
addObserver(obj, 'bar', incrementCount);
defineProperty(obj, 'bar', alias('foo.faz'));
assert.ok(isWatching(obj, 'foo.faz'));
set(obj, 'foo.faz', 'BAR');
assert.equal(count, 1);
}

QUnit.test('an observer of the alias works if added before defining the alias', function(assert) {
addObserver(obj, 'bar', incrementCount);
defineProperty(obj, 'bar', alias('foo.faz'));
assert.ok(isWatching(obj, 'foo.faz'));
set(obj, 'foo.faz', 'BAR');
assert.equal(count, 1);
});
['@test object with alias is dirtied if interior object of alias is set after consumption'](assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
get(obj, 'bar');

QUnit.test('object with alias is dirtied if interior object of alias is set after consumption', function(assert) {
defineProperty(obj, 'bar', alias('foo.faz'));
get(obj, 'bar');
let tag = tagFor(obj);
let tagValue = tag.value();
set(obj, 'foo.faz', 'BAR');

let tag = tagFor(obj);
let tagValue = tag.value();
set(obj, 'foo.faz', 'BAR');
assert.ok(!tag.validate(tagValue), 'setting the aliased key should dirty the object');
}

assert.ok(!tag.validate(tagValue), 'setting the aliased key should dirty the object');
['@test setting alias on self should fail assertion']() {
expectAssertion(() => defineProperty(obj, 'bar', alias('bar')), 'Setting alias \'bar\' on self');
}
});

QUnit.test('setting alias on self should fail assertion', function() {
expectAssertion(() => defineProperty(obj, 'bar', alias('bar')), 'Setting alias \'bar\' on self');
});
162 changes: 82 additions & 80 deletions packages/ember-metal/tests/cache_test.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,95 @@
import { Cache } from '..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

QUnit.module('Cache');
moduleFor('Cache', class extends AbstractTestCase {
['@test basic'](assert) {
let cache = new Cache(100, key => key.toUpperCase());

QUnit.test('basic', function(assert) {
let cache = new Cache(100, key => key.toUpperCase());
assert.equal(cache.get('foo'), 'FOO');
assert.equal(cache.get('bar'), 'BAR');
assert.equal(cache.get('foo'), 'FOO');
}

assert.equal(cache.get('foo'), 'FOO');
assert.equal(cache.get('bar'), 'BAR');
assert.equal(cache.get('foo'), 'FOO');
});

QUnit.test('explicit sets', function(assert) {
let cache = new Cache(100, key => key.toUpperCase());
['@test explicit sets'](assert) {
let cache = new Cache(100, key => key.toUpperCase());

assert.equal(cache.get('foo'), 'FOO');
assert.equal(cache.get('foo'), 'FOO');

assert.equal(cache.set('foo', 'FOO!!!'), 'FOO!!!');
assert.equal(cache.set('foo', 'FOO!!!'), 'FOO!!!');

assert.equal(cache.get('foo'), 'FOO!!!');
assert.equal(cache.get('foo'), 'FOO!!!');

assert.strictEqual(cache.set('foo', undefined), undefined);
assert.strictEqual(cache.set('foo', undefined), undefined);

assert.strictEqual(cache.get('foo'), undefined);
});

QUnit.test('caches computation correctly', function(assert) {
let count = 0;
let cache = new Cache(100, key => {
count++;
return key.toUpperCase();
});

assert.equal(count, 0);
cache.get('foo');
assert.equal(count, 1);
cache.get('bar');
assert.equal(count, 2);
cache.get('bar');
assert.equal(count, 2);
cache.get('foo');
assert.equal(count, 2);
});
assert.strictEqual(cache.get('foo'), undefined);
}

QUnit.test('caches computation correctly with custom cache keys', function(assert) {
let count = 0;
let cache = new Cache(
100,
obj => {
['@test caches computation correctly'](assert) {
let count = 0;
let cache = new Cache(100, key => {
count++;
return obj.value.toUpperCase();
},
obj => obj.key
);

assert.equal(count, 0);
cache.get({ key: 'foo', value: 'foo' });
assert.equal(count, 1);
cache.get({ key: 'bar', value: 'bar' });
assert.equal(count, 2);
cache.get({ key: 'bar', value: 'bar' });
assert.equal(count, 2);
cache.get({ key: 'foo', value: 'foo' });
assert.equal(count, 2);
return key.toUpperCase();
});

assert.equal(count, 0);
cache.get('foo');
assert.equal(count, 1);
cache.get('bar');
assert.equal(count, 2);
cache.get('bar');
assert.equal(count, 2);
cache.get('foo');
assert.equal(count, 2);
}

['@test caches computation correctly with custom cache keys'](assert) {
let count = 0;
let cache = new Cache(
100,
obj => {
count++;
return obj.value.toUpperCase();
},
obj => obj.key
);

assert.equal(count, 0);
cache.get({ key: 'foo', value: 'foo' });
assert.equal(count, 1);
cache.get({ key: 'bar', value: 'bar' });
assert.equal(count, 2);
cache.get({ key: 'bar', value: 'bar' });
assert.equal(count, 2);
cache.get({ key: 'foo', value: 'foo' });
assert.equal(count, 2);
}

['@test handles undefined value correctly'](assert) {
let count = 0;
let cache = new Cache(100, () => { count++; });

assert.equal(count, 0);
assert.strictEqual(cache.get('foo'), undefined);
assert.equal(count, 1);
assert.strictEqual(cache.get('bar'), undefined);
assert.equal(count, 2);
assert.strictEqual(cache.get('bar'), undefined);
assert.equal(count, 2);
assert.strictEqual(cache.get('foo'), undefined);
assert.equal(count, 2);
}

['@test continues working after reaching cache limit'](assert) {
let cache = new Cache(3, key => key.toUpperCase());

cache.get('a');
cache.get('b');
cache.get('c');

assert.equal(cache.get('d'), 'D');
assert.equal(cache.get('a'), 'A');
assert.equal(cache.get('b'), 'B');
assert.equal(cache.get('c'), 'C');
}
});

QUnit.test('handles undefined value correctly', function(assert) {
let count = 0;
let cache = new Cache(100, () => { count++; });

assert.equal(count, 0);
assert.strictEqual(cache.get('foo'), undefined);
assert.equal(count, 1);
assert.strictEqual(cache.get('bar'), undefined);
assert.equal(count, 2);
assert.strictEqual(cache.get('bar'), undefined);
assert.equal(count, 2);
assert.strictEqual(cache.get('foo'), undefined);
assert.equal(count, 2);
});

QUnit.test('continues working after reaching cache limit', function(assert) {
let cache = new Cache(3, key => key.toUpperCase());

cache.get('a');
cache.get('b');
cache.get('c');

assert.equal(cache.get('d'), 'D');
assert.equal(cache.get('a'), 'A');
assert.equal(cache.get('b'), 'B');
assert.equal(cache.get('c'), 'C');
});
Loading

0 comments on commit ce4065d

Please sign in to comment.