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

feat(auto-init): return initialized components #1333

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
3 changes: 3 additions & 0 deletions packages/mdc-auto-init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function _emit(evtType, evtData, shouldBubble = false) {
* Auto-initializes all mdc components on a page.
*/
export default function mdcAutoInit(root = document, warn = CONSOLE_WARN) {
const components = [];
const nodes = root.querySelectorAll('[data-mdc-auto-init]');
for (let i = 0, node; (node = nodes[i]); i++) {
const ctorName = node.dataset.mdcAutoInit;
Expand All @@ -63,9 +64,11 @@ export default function mdcAutoInit(root = document, warn = CONSOLE_WARN) {
enumerable: false,
configurable: true,
});
components.push(component);
}

_emit('MDCAutoInit:End', {});
return components;
}

mdcAutoInit.register = function(componentName, Ctor, warn = CONSOLE_WARN) {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mdc-auto-init/mdc-auto-init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ test('#dispatches a MDCAutoInit:End event when all components are initialized -
assert.isOk(evt !== null);
assert.equal(evt.type, type);
});

test('#returns the initialized components', () => {
const root = setupTest();
const components = mdcAutoInit(root);

assert.equal(components.length, 1);
assert.isOk(components[0] instanceof FakeComponent);
});