Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
fix(base): Ensure this.root_ is available within getDefaultFoundation…
Browse files Browse the repository at this point in the history
…() (#279)

Fixes #242
  • Loading branch information
traviskaufman authored Feb 10, 2017
1 parent acccc9e commit c637cb6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/mdc-base/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export default class MDCComponent {
return new MDCComponent(root, new MDCFoundation());
}

constructor(root, foundation = this.getDefaultFoundation(), ...args) {
constructor(root, foundation = undefined, ...args) {
this.root_ = root;
this.initialize(...args);
this.foundation_ = foundation;
// Note that we initialize foundation here and not within the constructor's default param so that
// this.root_ is defined and can be used within the foundation class.
this.foundation_ = foundation === undefined ? this.getDefaultFoundation() : foundation;
this.foundation_.init();
this.initialSyncWithDOM();
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mdc-base/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class FakeComponent extends MDCComponent {
return td.object({
isDefaultFoundation: true,
init: () => {},
rootElementAtTimeOfCall: this.root_,
});
}

Expand Down Expand Up @@ -163,3 +164,10 @@ test('#emit dispatches a custom event with the supplied data', (t) => {
t.deepEqual(evt.detail, data);
t.end();
});

test('(regression) ensures that this.root_ is available for use within getDefaultFoundation()', (t) => {
const root = document.createElement('div');
const f = new FakeComponent(root);
t.equal(f.foundation.rootElementAtTimeOfCall, root);
t.end();
});

0 comments on commit c637cb6

Please sign in to comment.