Skip to content

Commit

Permalink
feat(auto-init): return initialized components (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
blikblum authored and kfranqueiro committed Jul 17, 2018
1 parent 608e46a commit 19955bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
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);
});

0 comments on commit 19955bf

Please sign in to comment.