From 9740a61f54503368215273ba8c525f32e17feb04 Mon Sep 17 00:00:00 2001 From: Travis Kaufman Date: Wed, 8 Feb 2017 14:51:03 -0500 Subject: [PATCH] fix(base): Ensure this.root_ is available within getDefaultFoundation() Fixes #242 --- packages/mdc-base/component.js | 6 ++++-- test/unit/mdc-base/component.test.js | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/mdc-base/component.js b/packages/mdc-base/component.js index f63c4d91771..54718d7d66e 100644 --- a/packages/mdc-base/component.js +++ b/packages/mdc-base/component.js @@ -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(); } diff --git a/test/unit/mdc-base/component.test.js b/test/unit/mdc-base/component.test.js index 79f86f01f5f..8f7ed9229a4 100644 --- a/test/unit/mdc-base/component.test.js +++ b/test/unit/mdc-base/component.test.js @@ -33,6 +33,7 @@ class FakeComponent extends MDCComponent { return td.object({ isDefaultFoundation: true, init: () => {}, + rootElementAtTimeOfCall: this.root_, }); } @@ -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(); +});