浏览代码

core: throw an error when one Plugin is `.use`d twice

We don't support that now, and will result in very confusing behavior
Kevin van Zonneveld 9 年之前
父节点
当前提交
a52db8501b
共有 5 个文件被更改,包括 52 次插入7 次删除
  1. 1 1
      CHANGELOG.md
  2. 9 0
      src/core/Core.js
  3. 23 5
      test/core.spec.js
  4. 1 1
      test/mocks/plugin-selecter1.js
  5. 18 0
      test/mocks/plugin-selecter2.js

+ 1 - 1
CHANGELOG.md

@@ -49,7 +49,7 @@ look at https://github.com/akiran/react-slick
 
 ## 0.0.3 (March 01, 2016)
 
-- [ ] core: throw an error when one Plugin is `.use`d twice. We don't support that now, and will result in very confusing behavior (@kvz)
+- [x] core: throw an error when one Plugin is `.use`d twice. We don't support that now, and will result in very confusing behavior (@kvz)
 - [ ] modal: Make sure modal renders under one dom node (@arturi)
 - [ ] modal: Make `ProgressBar` work with the new Modal (@kvz)
 - [ ] google: Convert `GoogleDrive` to adhere to `Dummy`'s format, so it's compatible with the new Modal (@hedgerh)

+ 9 - 0
src/core/Core.js

@@ -55,6 +55,15 @@ export default class Core {
       throw new Error('Your plugin must have a type')
     }
 
+    let existsPluginAlready = this.getPlugin(plugin.constructor.name)
+    if (existsPluginAlready) {
+      let msg = 'Uppy is currently limited to running one of every plugin. '
+      msg += 'Share your use case with us over at '
+      msg += 'https://github.com/transloadit/uppy/issues/ '
+      msg += 'if you want us to reconsider. '
+      throw new Error(msg)
+    }
+
     this.plugins[plugin.type].push(plugin)
 
     return this

+ 23 - 5
test/core.spec.js

@@ -10,7 +10,7 @@ test('core', function (t) {
 })
 
 test('use plugins', function (t) {
-  const SelecterPlugin = require('./mocks/plugin-selecter.js')
+  const SelecterPlugin = require('./mocks/plugin-selecter1.js')
   const uppy = new Uppy()
   uppy
     .use(SelecterPlugin)
@@ -19,18 +19,36 @@ test('use plugins', function (t) {
   t.end()
 })
 
+test('noDuplicates', function (t) {
+  const Selecter1Plugin = require('./mocks/plugin-selecter1.js')
+  const uppyTwoSelecters = new Uppy()
+  let err = ''
+  try {
+    uppyTwoSelecters
+    .use(Selecter1Plugin)
+    .use(Selecter1Plugin)
+    .run()
+  } catch (e) {
+    err = e.message
+  }
+
+  t.equal(err, 'Uppy is currently limited to running one of every plugin. Share your use case with us over at https://github.com/transloadit/uppy/issues/ if you want us to reconsider.', 'should throw error on use of duplicate plugin')
+  t.end()
+})
+
 test('autoProceed', function (t) {
-  const SelecterPlugin = require('./mocks/plugin-selecter.js')
+  const Selecter1Plugin = require('./mocks/plugin-selecter1.js')
+  const Selecter2Plugin = require('./mocks/plugin-selecter2.js')
 
   const uppyOneSelecter = new Uppy()
   uppyOneSelecter
-    .use(SelecterPlugin)
+    .use(Selecter1Plugin)
     .run()
 
   const uppyTwoSelecters = new Uppy()
   uppyTwoSelecters
-    .use(SelecterPlugin)
-    .use(SelecterPlugin)
+    .use(Selecter1Plugin)
+    .use(Selecter2Plugin)
     .run()
 
   t.equal(uppyOneSelecter.opts.autoProceed, true, 'should autoProceed if only one selecter is used')

+ 1 - 1
test/mocks/plugin-selecter.js → test/mocks/plugin-selecter1.js

@@ -3,7 +3,7 @@ const Plugin = require('../../src/plugins/Plugin.js')
 export default class TestSelector extends Plugin {
   constructor (core, opts) {
     super(core, opts)
-    this.type = 'selecter'
+    this.type = 'selecter1'
   }
 
   run (results) {

+ 18 - 0
test/mocks/plugin-selecter2.js

@@ -0,0 +1,18 @@
+const Plugin = require('../../src/plugins/Plugin.js')
+
+export default class TestSelector extends Plugin {
+  constructor (core, opts) {
+    super(core, opts)
+    this.type = 'selecter2'
+  }
+
+  run (results) {
+    this.core.log({
+      class: this.constructor.name,
+      method: 'run',
+      results: results
+    })
+
+    return Promise.resolve('success')
+  }
+}