Skip to content

Commit

Permalink
added tests for enumerability
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jun 10, 2016
1 parent 68dbee8 commit b7b7b63
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/action.ts
Expand Up @@ -6,7 +6,7 @@ import {ComputedValue} from "../core/computedvalue";
import {globalState} from "../core/globalstate";
import {createClassPropertyDecorator} from "../utils/decorators";

const actionFieldDecorator = createClassPropertyDecorator(
const actionDecorator = createClassPropertyDecorator(
function (target, key, value, args, originalDescriptor) {
const actionName = (args && args.length === 1) ? args[0] : (value.name || key || "<unnamed action>");
const wrappedAction = action(actionName, value);
Expand Down Expand Up @@ -44,7 +44,7 @@ export function action(arg1, arg2?, arg3?, arg4?): any {
if (arguments.length === 2 && typeof arg2 === "function")
return actionImplementation(arg1, arg2);

return actionFieldDecorator.apply(null, arguments);
return actionDecorator.apply(null, arguments);
}

export function actionImplementation(actionName: string, fn: Function): Function {
Expand Down
55 changes: 55 additions & 0 deletions test/babel/babel-tests.js
Expand Up @@ -461,5 +461,60 @@ test("reusing initializers", t => {
10
])
t.end();
})
test("enumerability", t => {
class A {
@observable a = 1; // enumerable, on proto
@computed get b () { return this.a } // non-enumerable, on proto
@action m() {} // non-enumerable, on proto
@action m2 = () => {}; // non-enumerable, on self
}
const a = new A();
// not initialized yet
let ownProps = Object.keys(a);
let props = [];
for (var key in a)
props.push(key);
t.deepEqual(ownProps, [
]);
t.deepEqual(props, [ // also 'a' would be ok
"a"
]);
t.equal(a.hasOwnProperty("a"), false); // true would be ok as well
t.equal(a.hasOwnProperty("b"), false);
t.equal(a.hasOwnProperty("m"), false);
t.equal(a.hasOwnProperty("m2"), false); // true would be ok as well
// after initialization
a.a;
a.b;
a.m;
a.m2;
ownProps = Object.keys(a);
props = [];
for (var key in a)
props.push(key);
t.deepEqual(ownProps, [ // also 'a' would be ok
]);

t.deepEqual(props, [
"a"
]);

t.equal(a.hasOwnProperty("a"), false); // true would be ok as well
t.equal(a.hasOwnProperty("b"), false);
t.equal(a.hasOwnProperty("m"), false);
t.equal(a.hasOwnProperty("m2"), true);


t.end();
})
58 changes: 57 additions & 1 deletion test/typescript-tests.ts
Expand Up @@ -734,4 +734,60 @@ test("reusing initializers", t => {
])

t.end();
})
})

test("enumerability", t => {
class A {
@observable a = 1; // enumerable, on proto
@computed get b () { return this.a } // non-enumerable, on proto
@action m() {} // non-enumerable, on proto
@action m2 = () => {}; // non-enumerable, on self
}

const a = new A();

// not initialized yet
let ownProps = Object.keys(a);
let props: string[] = [];
for (var key in a)
props.push(key);

t.deepEqual(ownProps, [
]);

t.deepEqual(props, [ // also 'a' would be ok
"a"
]);

t.equal(a.hasOwnProperty("a"), false); // true would be ok as well
t.equal(a.hasOwnProperty("b"), false);
t.equal(a.hasOwnProperty("m"), false);
t.equal(a.hasOwnProperty("m2"), true); // false would be ok as well

// after initialization
a.a;
a.b;
a.m;
a.m2;

ownProps = Object.keys(a);
props = [];
for (var key in a)
props.push(key);

t.deepEqual(ownProps, [ // also 'a' would be ok
]);

t.deepEqual(props, [
"a"
]);

t.equal(a.hasOwnProperty("a"), false); // true would be ok as well
t.equal(a.hasOwnProperty("b"), false);
t.equal(a.hasOwnProperty("m"), false);
t.equal(a.hasOwnProperty("m2"), true);


t.end();
})

0 comments on commit b7b7b63

Please sign in to comment.