From 5d2dc5524c681f53c606a54cd132a7f901adf743 Mon Sep 17 00:00:00 2001 From: Thomas O'Brien Date: Thu, 15 Feb 2018 10:24:03 -0800 Subject: [PATCH] add [names] option to resetProviders --- src/Bottle/reset-providers.js | 19 ++++++++++++------- test/spec/provider.spec.js | 20 +++++++++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Bottle/reset-providers.js b/src/Bottle/reset-providers.js index fe34a55..974819f 100644 --- a/src/Bottle/reset-providers.js +++ b/src/Bottle/reset-providers.js @@ -11,18 +11,23 @@ var removeProviderMap = function resetProvider(name) { }; /** - * Resets all providers on a bottle instance. + * Resets providers on a bottle instance. If 'names' array is provided, only the named providers will be reset. * + * @param Array names * @return void */ -var resetProviders = function resetProviders() { - var providers = this.originalProviders; - Object.keys(this.originalProviders).forEach(function resetPrvider(provider) { - var parts = provider.split(DELIMITER); +var resetProviders = function resetProviders(names) { + var tempProviders = this.originalProviders; + + Object.keys(this.originalProviders).forEach(function resetProvider(originalProviderName) { + if (Array.isArray(names) && names.indexOf(originalProviderName) === -1) { + return; + } + var parts = originalProviderName.split(DELIMITER); if (parts.length > 1) { parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0])); } - removeProviderMap.call(this, provider); - this.provider(provider, providers[provider]); + removeProviderMap.call(this, originalProviderName); + this.provider(originalProviderName, tempProviders[originalProviderName]); }, this); }; diff --git a/test/spec/provider.spec.js b/test/spec/provider.spec.js index ef196dc..5efdeb6 100644 --- a/test/spec/provider.spec.js +++ b/test/spec/provider.spec.js @@ -123,7 +123,7 @@ expect(b.container.Util.ThingProvider).toBeDefined(); expect(b.container.Util.Thing).toBeDefined(); }); - + it('does not log an error if a service is added to a nested bottle with initialized services', function() { var b = new Bottle(); var Thing = function() {}; @@ -171,6 +171,24 @@ expect(b.container.Thing instanceof ThingProvider).toBe(true); expect(i).toEqual(2); }); + it('allows for selectively resetting providers by name', function() { + var i = 0; + var j = 0; + var b = new Bottle(); + var FirstProvider = function() { i = ++i; this.$get = function() { return this; }; }; + var SecondProvider = function() { j = ++j; this.$get = function() { return this; }; }; + b.provider('First', FirstProvider); + b.provider('Second', SecondProvider); + expect(b.container.First instanceof FirstProvider).toBe(true); + expect(b.container.Second instanceof SecondProvider).toBe(true); + expect(i).toEqual(1); + expect(j).toEqual(1); + b.resetProviders(['First']); + expect(b.container.First instanceof FirstProvider).toBe(true); + expect(b.container.Second instanceof SecondProvider).toBe(true); + expect(i).toEqual(2); + expect(j).toEqual(1); + }); it('allows for sub containers to re-initiate as well', function() { var i = 0; var b = new Bottle();