-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): add unit test for plugin.js
- Loading branch information
1 parent
1df33f8
commit 0bc3a18
Showing
2 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |