Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that STATE configuration from static hints are properly configured with multiple levels of class inheritance #288

Merged
merged 3 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 19 additions & 2 deletions packages/metal-state/src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,13 @@ class State extends EventEmitter {
if (ctor !== State) {
let defineContext;
if (this.obj_ === this) {
defineContext = ctor.hasConfiguredState_ ? false : ctor.prototype;
ctor.hasConfiguredState_ = true;
const staticKey = State.STATE_STATIC_HINT_CONFIGURED;

ctor[staticKey] = ctor[staticKey] || {};

defineContext = ctor[staticKey][ctor.name] ? false :
ctor.prototype;
ctor[staticKey][ctor.name] = true;
}
this.configState(State.getStateStatic(ctor), defineContext);
}
Expand Down Expand Up @@ -676,8 +681,20 @@ class State extends EventEmitter {
}
}

/**
* Constant used as key on State instance for storing property definition.
* @type {!string}
*/
State.STATE_REF_KEY = '__METAL_STATE_REF_KEY__';

/**
* Constant used as key on class constructors that extend form State, stores
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible typo form -> from?

* which constructors have had their static STATE configured so that
* configuration of STATE is not repeated.
* @type {!string}
*/
State.STATE_STATIC_HINT_CONFIGURED = '__METAL_STATE_STATIC_HINT_CONFIGURED__';

/**
* Constants that represent the states that a state key can be in.
* @type {!Object}
Expand Down
26 changes: 26 additions & 0 deletions packages/metal-state/test/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,32 @@ describe('State', function() {
assert.strictEqual('foo2', test2.key1);
assert.strictEqual('foo1', test1.key1);
});

it('should configure static STATE with multiple levels of class inheritance', function() {
var Test = createTestClass();
Test.STATE = {
key1: {
}
};

class Child extends Test {
}
Child.STATE = {
key2: {
}
};

var test = new Test({
key1: 'foo1'
});
assert.strictEqual('foo1', test.key1);

var child = new Child({
key2: 'foo2'
});
assert.strictEqual('foo1', test.key1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be asserting child.key1?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if you change it to child.key1, the test will fail...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's shouldn't be asserting child.key1, key1 isn't being passed a value for Child. I just left this assertion there to ensure that the static hint configuration of Child doesn't affect the values of the parent it's inheriting from.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see! For some reason I assumed they had defaults!

assert.strictEqual('foo2', child.key2);
});
});

describe('Separate object', function() {
Expand Down