|
189 | 189 | expect(i).toEqual(2);
|
190 | 190 | expect(j).toEqual(1);
|
191 | 191 | });
|
192 |
| - it('allows for sub containers to re-initiate as well', function() { |
| 192 | + it('allows to propagate the reset to the providers that depend on the selected service names', function() { |
| 193 | + var i = 0; |
| 194 | + var j = 0; |
| 195 | + var k = 0; |
| 196 | + var l = 0; |
| 197 | + var b = new Bottle(); |
| 198 | + var FirstService= function() { i = ++i; }; |
| 199 | + var SecondProvider = function() { |
| 200 | + j = ++j; |
| 201 | + this.$get = function(container) { return { _first: container.First }; }; |
| 202 | + }; |
| 203 | + var ThirdFactory = function(container) { k = ++k; return { _first: container.First }; }; |
| 204 | + b.factory('Zero', function () { l = ++l; return 0; }); |
| 205 | + b.service('First', FirstService, 'Zero'); |
| 206 | + b.provider('Second', SecondProvider); |
| 207 | + b.factory('Third', ThirdFactory); |
| 208 | + expect(b.container.Second._first instanceof FirstService).toBe(true); |
| 209 | + expect(b.container.Third._first instanceof FirstService).toBe(true); |
| 210 | + expect(i).toEqual(1); |
| 211 | + expect(j).toEqual(1); |
| 212 | + expect(k).toEqual(1); |
| 213 | + expect(l).toEqual(1); |
| 214 | + b.resetProviders(['First'], true); |
| 215 | + expect(b.container.Second._first instanceof FirstService).toBe(true); |
| 216 | + expect(b.container.Third._first instanceof FirstService).toBe(true); |
| 217 | + expect(i).toEqual(2); |
| 218 | + expect(j).toEqual(2); |
| 219 | + expect(k).toEqual(2); |
| 220 | + expect(l).toEqual(1); |
| 221 | + b.resetProviders(['Zero'], true); |
| 222 | + expect(b.container.Second._first instanceof FirstService).toBe(true); |
| 223 | + expect(b.container.Third._first instanceof FirstService).toBe(true); |
| 224 | + expect(i).toEqual(3); |
| 225 | + expect(j).toEqual(3); |
| 226 | + expect(k).toEqual(3); |
| 227 | + expect(l).toEqual(2); |
| 228 | + }); |
| 229 | + it('will cleanup service dependents if a service is redefined', function() { |
| 230 | + var i = 0; |
| 231 | + var j = 0; |
| 232 | + var k = 0; |
| 233 | + var b = new Bottle(); |
| 234 | + var FirstService= function() { i = ++i; }; |
| 235 | + var SecondService = function() { j = ++j; }; |
| 236 | + var ThirdService = function() { k = ++k; }; |
| 237 | + b.service('First', FirstService); |
| 238 | + b.service('Thing.Second', SecondService, 'First'); |
| 239 | + b.service('Third', ThirdService, 'Thing.Second'); |
| 240 | + expect(b.container.First instanceof FirstService).toBe(true); |
| 241 | + expect(b.container.Thing.Second instanceof SecondService).toBe(true); |
| 242 | + expect(b.container.Third instanceof ThirdService).toBe(true); |
| 243 | + expect(i).toEqual(1); |
| 244 | + expect(j).toEqual(1); |
| 245 | + expect(k).toEqual(1); |
| 246 | + b.resetProviders(['Thing.Second'], true); |
| 247 | + // Redefined to have no dependencies |
| 248 | + b.service('Thing.Second', SecondService); |
| 249 | + expect(b.container.First instanceof FirstService).toBe(true); |
| 250 | + expect(b.container.Thing.Second instanceof SecondService).toBe(true); |
| 251 | + expect(b.container.Third instanceof ThirdService).toBe(true); |
| 252 | + expect(i).toEqual(1); |
| 253 | + expect(j).toEqual(2); |
| 254 | + expect(k).toEqual(2); |
| 255 | + // No propagation will happen given that no service depends on First anymore |
| 256 | + b.resetProviders(['First'], true); |
| 257 | + expect(b.container.First instanceof FirstService).toBe(true); |
| 258 | + expect(b.container.Thing.Second instanceof SecondService).toBe(true); |
| 259 | + expect(b.container.Third instanceof ThirdService).toBe(true); |
| 260 | + expect(i).toEqual(2); |
| 261 | + expect(j).toEqual(2); |
| 262 | + expect(k).toEqual(2); |
| 263 | + }); |
| 264 | + it('allows for deep sub containers to re-initiate as well', function() { |
193 | 265 | var i = 0;
|
194 | 266 | var b = new Bottle();
|
195 | 267 | var ThingProvider = function() { i = ++i; this.$get = function() { return this; }; };
|
196 |
| - b.provider('Thing.Something', ThingProvider); |
197 |
| - expect(b.container.Thing.Something instanceof ThingProvider).toBe(true); |
| 268 | + b.provider('Thing.In.Something', ThingProvider); |
| 269 | + expect(b.container.Thing.In.Something instanceof ThingProvider).toBe(true); |
198 | 270 | // Intentionally calling twice to prove the construction is cached until reset
|
199 |
| - expect(b.container.Thing.Something instanceof ThingProvider).toBe(true); |
| 271 | + expect(b.container.Thing.In.Something instanceof ThingProvider).toBe(true); |
200 | 272 | b.resetProviders();
|
201 |
| - expect(b.container.Thing.Something instanceof ThingProvider).toBe(true); |
| 273 | + expect(b.container.Thing.In.Something instanceof ThingProvider).toBe(true); |
202 | 274 | expect(i).toEqual(2);
|
203 | 275 | });
|
204 | 276 | it('will not break if a nested container has multiple children', function() {
|
|
0 commit comments