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

fix(base): Ensure this.root_ is available within getDefaultFoundation() #279

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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();
});