Skip to content

Commit

Permalink
Merge pull request #16902 from smfoote/tracked-tests
Browse files Browse the repository at this point in the history
Tracked tests
  • Loading branch information
chadhietala authored Aug 20, 2018
2 parents 0d8fd80 + a9ac81d commit ee62e49
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { createWithDescriptors } from './support';
import { get, set, tracked } from '../..';

import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { AbstractTestCase, moduleFor } from 'internal-test-helpers';

if (EMBER_METAL_TRACKED_PROPERTIES) {
moduleFor(
'tracked getters',
'@tracked getters',
class extends AbstractTestCase {
['@test works without get'](assert) {
let count = 0;

class Count {
@tracked
get foo() {
count++;
return `computed foo`;
}
}

tracked(Count.prototype, 'foo', Object.getOwnPropertyDescriptor(Count.prototype, 'foo'));

let obj = new Count();

assert.equal(obj.foo, 'computed foo', 'should return value');
Expand All @@ -30,14 +28,13 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
let count = 0;

class Count {
@tracked
get foo() {
count++;
return `computed foo`;
}
}

tracked(Count.prototype, 'foo', Object.getOwnPropertyDescriptor(Count.prototype, 'foo'));

let obj = new Count();

assert.equal(get(obj, 'foo'), 'computed foo', 'should return value');
Expand All @@ -47,16 +44,19 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
['@test defining computed property should invoke property on set'](assert) {
let count = 0;

let obj = createWithDescriptors({
let obj = new class {
__foo = '';

@tracked
get foo() {
return this.__foo;
},
}

set foo(value) {
count++;
this.__foo = `computed ${value}`;
},
});
}
}();

assert.equal(set(obj, 'foo', 'bar'), 'bar', 'should return set value');
assert.equal(count, 1, 'should have invoked computed property');
Expand Down
44 changes: 0 additions & 44 deletions packages/ember-metal/tests/tracked/set_test.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/ember-metal/tests/tracked/set_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { AbstractTestCase, moduleFor } from 'internal-test-helpers';
import { get, set, setHasViews, tracked } from '../..';

import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';

if (EMBER_METAL_TRACKED_PROPERTIES) {
const createObj = () => {
class Obj {
@tracked string = 'string';
@tracked number = 23;
@tracked boolTrue = true;
@tracked boolFalse = false;
@tracked nullValue = null;
@tracked undefinedValue = undefined;
constructor() {
this.string = 'string';
this.number = 23;
this.boolTrue = true;
this.boolFalse = false;
this.nullValue = null;
this.undefinedValue = undefined;
}
}

return new Obj();
};

moduleFor(
'@tracked set',
class extends AbstractTestCase {
teardown() {
setHasViews(() => false);
}

['@test should set arbitrary properties on an object'](assert) {
let obj = createObj();

let newObj = new class {
@tracked undefinedValue = 'emberjs';

constructor() {
this.undefinedValue = 'emberjs';
}
}();

for (let key in obj) {
assert.equal(set(newObj, key, obj[key]), obj[key], 'should return value');
assert.equal(get(newObj, key), obj[key], 'should set value');
}
}

['@test should set a number key on an object'](assert) {
let obj = new class {
@tracked 1 = 'original';
constructor() {
this[1] = 'original';
}
}();

set(obj, '1', 'first');
assert.equal(obj[1], 'first');
}
}
);
}
25 changes: 0 additions & 25 deletions packages/ember-metal/tests/tracked/support.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
import { tracked } from '../..';

export function createTracked(values, proto = {}) {
function Class() {
for (let prop in values) {
this[prop] = values[prop];
}
}

for (let prop in values) {
Object.defineProperty(
proto,
prop,
tracked(proto, prop, {
enumerable: true,
configurable: true,
writable: true,
value: values[prop],
})
);
}

Class.prototype = proto;

return new Class();
}

export function createWithDescriptors(values) {
function Class() {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { computed, defineProperty, get, set, tracked } from '../..';

import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { tagForProperty } from '../..';
import { computed, defineProperty, get, set, tagForProperty, tracked } from '../..';

import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { AbstractTestCase, moduleFor } from 'internal-test-helpers';

if (EMBER_METAL_TRACKED_PROPERTIES) {
moduleFor(
'tracked get validation',
'@tracked get validation',
class extends AbstractTestCase {
[`@test validators for tracked getters with dependencies should invalidate when the dependencies invalidate`](
assert
) {
class Tracked {
constructor(first, last) {
@tracked first: string;
@tracked last: string;
constructor(first: string, last: string) {
this.first = first;
this.last = last;
}
}

track(Tracked, ['first', 'last'], {
@tracked(['first', 'last'])
get full() {
return `${this.first} ${this.last}`;
},
});
}
}

let obj = new Tracked('Tom', 'Dale');

Expand All @@ -49,17 +48,21 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
[`@test interaction with Ember object model (tracked property depending on Ember property)`](
assert
) {
interface NameInterface {
first: string;
last: string;
}
class Tracked {
constructor(name) {
@tracked name: NameInterface;
constructor(name: NameInterface) {
this.name = name;
}
}

track(Tracked, ['name'], {
@tracked('name')
get full() {
return `${get(this.name, 'first')} ${get(this.name, 'last')}`;
},
});
}
}

let tom = { first: 'Tom', last: 'Dale' };

Expand Down Expand Up @@ -88,7 +91,8 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
assert
) {
class EmberObject {
constructor(name) {
name: Name;
constructor(name: Name) {
this.name = name;
}
}
Expand All @@ -103,14 +107,14 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
);

class Name {
constructor(first, last) {
@tracked first: string;
@tracked last: string;
constructor(first: string, last: string) {
this.first = first;
this.last = last;
}
}

track(Name, ['first', 'last']);

let tom = new Name('Tom', 'Dale');
let obj = new EmberObject(tom);

Expand All @@ -134,7 +138,7 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
assert.equal(get(obj, 'full'), 'Thomas Dale');
snapshot = tag.value();

// assert.equal(tag.validate(snapshot), true);
assert.equal(tag.validate(snapshot), true);
}

['@test interaction with the Ember object model (paths going through tracked properties)'](
Expand Down

0 comments on commit ee62e49

Please sign in to comment.