refactor(jsdoc-core): make Dependencies#reset accept a string

This commit is contained in:
Jeff Williams 2021-10-22 10:09:32 -07:00
parent ad2cfd70ef
commit 004ce7392c
2 changed files with 25 additions and 4 deletions

View File

@ -1,3 +1,4 @@
const _ = require('lodash');
const Bottle = require('bottlejs');
/**
@ -25,7 +26,7 @@ class Dependencies {
registerClass(name, constructor, ...deps) {
this._bottle.service(name, constructor, ...deps);
// Remove the cached provider.
this._bottle.middleware(name, (_, next) => {
this._bottle.middleware(name, (provider, next) => {
this._bottle.resetProviders([name]);
next();
});
@ -40,7 +41,7 @@ class Dependencies {
this._bottle.serviceFactory(name, realFactory, ...deps);
// Remove the cached provider.
this._bottle.middleware(name, (_, next) => {
this._bottle.middleware(name, (provider, next) => {
this._bottle.resetProviders([name]);
next();
});
@ -65,7 +66,9 @@ class Dependencies {
}
reset(names) {
if (!Array.isArray(names)) {
if (_.isString(names)) {
names = [names];
} else if (!Array.isArray(names)) {
throw new Error('Must provide an array of provider names');
}

View File

@ -150,7 +150,25 @@ describe('@jsdoc/core/lib/dependencies', () => {
});
describe('reset', () => {
it('throws on non-array input', () => {
it('accepts a string', () => {
class Foo {}
container.registerClass('Foo', Foo);
expect(() => container.reset('Foo')).not.toThrow();
});
it('accepts an array of strings', () => {
class Foo {}
class Bar {}
container.registerClass('Foo', Foo);
container.registerClass('Bar', Bar);
expect(() => container.reset(['Foo', 'Bar'])).not.toThrow();
});
it('throws on non-array, non-string input', () => {
expect(() => container.reset()).toThrowError();
});