Skip to content

Commit

Permalink
fix(server): add unit test for plugin.js
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjbarton committed Jan 26, 2021
1 parent 1df33f8 commit 0bc3a18
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?`)
} else {
log.error(`Can not 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
}
}
Expand Down
61 changes: 61 additions & 0 deletions test/unit/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
})

0 comments on commit 0bc3a18

Please sign in to comment.