diff --git a/packages/mdc-auto-init/index.js b/packages/mdc-auto-init/index.js index 92880a0b52c..630d934abb6 100644 --- a/packages/mdc-auto-init/index.js +++ b/packages/mdc-auto-init/index.js @@ -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; @@ -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) { diff --git a/test/unit/mdc-auto-init/mdc-auto-init.test.js b/test/unit/mdc-auto-init/mdc-auto-init.test.js index 860b84448b6..6e0739488f9 100644 --- a/test/unit/mdc-auto-init/mdc-auto-init.test.js +++ b/test/unit/mdc-auto-init/mdc-auto-init.test.js @@ -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); +});