From c8ab31940a56a5beda4fd0ed8782ce9cb2bcdfc9 Mon Sep 17 00:00:00 2001 From: johnjbarton Date: Mon, 25 Jan 2021 16:25:27 -0800 Subject: [PATCH] fix(server): add unit test for plugin.js --- lib/plugin.js | 14 ++++----- test/unit/plugin.spec.js | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 test/unit/plugin.spec.js diff --git a/lib/plugin.js b/lib/plugin.js index c04e4a3c8..ad8ed718a 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -50,10 +50,9 @@ function resolve (plugins, emitter) { return modules } -const pluginInstances = new Map() - function createInstantiatePlugin (injector) { const emitter = injector.get('emitter') + const pluginInstances = new Map() return function instantiatePlugin (kind, name) { if (pluginInstances.has(name)) { return pluginInstances.get(name) @@ -64,18 +63,17 @@ function createInstantiatePlugin (injector) { p = injector.get(`${kind}:${name}`) if (!p) { log.error(`Failed to instantiate ${kind} ${name}`) - emitter.emit('load_error', `${kind}`, name) + emitter.emit('load_error', kind, name) } } catch (e) { if (e.message.includes(`No provider for "${kind}:${name}"`)) { - log.error(`Can not load "${name}", it is not registered!\n Perhaps you are missing some plugin?`) + log.error(`Cannot load "${name}", it is not registered!\n Perhaps you are missing some plugin?`) } else { - log.error(`Can not load "${name}"!\n ` + e.stack) + log.error(`Cannot load "${name}"!\n ` + e.stack) } - emitter.emit('load_error', `${kind}`, name) + emitter.emit('load_error', kind, name) } - - pluginInstances.set(name, p) + pluginInstances.set(name, p, `${kind}:${name}`) return p } } diff --git a/test/unit/plugin.spec.js b/test/unit/plugin.spec.js new file mode 100644 index 000000000..3ec9824d1 --- /dev/null +++ b/test/unit/plugin.spec.js @@ -0,0 +1,61 @@ +'use strict' + +const createInstantiatePlugin = require('../../lib/plugin').createInstantiatePlugin + +describe('plugin', () => { + describe('createInstantiatePlugin', () => { + it('creates the instantiatePlugin function', () => { + const fakeGet = sinon.stub() + const fakeInjector = { get: fakeGet } + + expect(typeof createInstantiatePlugin(fakeInjector)).to.be.equal('function') + expect(fakeGet).to.have.been.calledWith('emitter') + }) + + it('creates the instantiatePlugin function', () => { + const fakes = { + emitter: { emit: sinon.stub() } + } + const fakeInjector = { get: (id) => fakes[id] } + + const instantiatePlugin = createInstantiatePlugin(fakeInjector) + expect(typeof instantiatePlugin('kind', 'name')).to.be.equal('undefined') + expect(fakes.emitter.emit).to.have.been.calledWith('load_error', 'kind', 'name') + }) + + it('caches plugins', () => { + const fakes = { + emitter: { emit: sinon.stub() }, + 'kind:name': { my: 'plugin' } + } + const fakeInjector = { + get: (id) => { + return fakes[id] + } + } + + const instantiatePlugin = createInstantiatePlugin(fakeInjector) + expect(instantiatePlugin('kind', 'name')).to.be.equal(fakes['kind:name']) + fakeInjector.get = (id) => { throw new Error('failed to cache') } + expect(instantiatePlugin('kind', 'name')).to.be.equal(fakes['kind:name']) + }) + + it('errors if the injector errors', () => { + const fakes = { + emitter: { emit: sinon.stub() } + } + const fakeInjector = { + get: (id) => { + if (id in fakes) { + return fakes[id] + } + throw new Error('fail') + } + } + + const instantiatePlugin = createInstantiatePlugin(fakeInjector) + expect(typeof instantiatePlugin('unknown', 'name')).to.be.equal('undefined') + expect(fakes.emitter.emit).to.have.been.calledWith('load_error', 'unknown', 'name') + }) + }) +})