diff --git a/packages/utils/testing/src/lib/plugin.js b/packages/utils/testing/src/lib/plugin.js index 115f1a9a0..e42e6034c 100644 --- a/packages/utils/testing/src/lib/plugin.js +++ b/packages/utils/testing/src/lib/plugin.js @@ -1,16 +1,25 @@ class Plugins { constructor() { this.plugin = new Plugin(); + this.plugins = []; } - createPlugin() { - return this.plugin; + createPlugin(name) { + let plugin = new Plugin({ name }); + this.plugins.push(plugin); + return plugin; } emitAndRunActionsForEvent(name, options, callback) { + this.runActionsForEvent(name, options, callback); + } + + runActionsForEvent(name, options, callback) { const listeners = this.plugin.getListeners(name); if (listeners) { listeners.forEach(fn => fn(options, callback)); + } else { + callback(null, options); } } @@ -20,12 +29,27 @@ class Plugins { teardown() { this.plugin.listeners = {}; + this.plugins.forEach(plugin => plugin.teardown()); + } + + getPluginsProperty(pluginType, prop, childProp) { + let plugins = this.plugins.filter(plugin => plugin.has(pluginType)); + + let properties = plugins.map(plugin => { + if (childProp) { + return plugin[prop][childProp]; + } + return plugin[prop]; + }); + return properties.length > 0 ? properties.reduce((a,b) => { return a.concat(b); }) || [] : []; } } class Plugin { constructor() { this.listeners = {}; + this.pluginTypes = []; + this.compilers = []; } getListeners(name) { @@ -38,6 +62,24 @@ class Plugin { } this.listeners[name].push(action); } + + has(pluginType) { + return this.pluginTypes.indexOf(pluginType) >= 0; + } + + addPluginType(pluginType) { + this.pluginTypes.push(pluginType); + this.pluginTypes = Array.from(new Set(this.pluginTypes)); + } + + registerCompiler(extension, cb) { + this.compilers.push({extension: extension, cb: cb}); + this.addPluginType('compilers'); + } + + teardown() { + this.compilers = []; + } } module.exports = Plugins;