mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
adding an extra injector spec
- added a spec for dependency graph resolution - also simplyfying cache presence check
This commit is contained in:
parent
65585a2d3c
commit
a709dc19b8
2 changed files with 27 additions and 1 deletions
|
|
@ -43,7 +43,7 @@ function createInjector(providerScope, providers, cache) {
|
|||
return function inject(value, scope, args){
|
||||
var returnValue, provider;
|
||||
if (isString(value)) {
|
||||
if (!cache.hasOwnProperty(value)) {
|
||||
if (!(value in cache)) {
|
||||
provider = providers[value];
|
||||
if (!provider) throw "Unknown provider for '"+value+"'.";
|
||||
cache[value] = inject(provider, providerScope);
|
||||
|
|
|
|||
|
|
@ -45,6 +45,32 @@ describe('injector', function(){
|
|||
expect(scope).toEqual({mi:'Mi', name:'Misko'});
|
||||
});
|
||||
|
||||
|
||||
it('should resolve dependency graph and instantiate all services just once', function(){
|
||||
var log = [];
|
||||
|
||||
// s1
|
||||
// / |\
|
||||
// / s2\
|
||||
// / / | \\
|
||||
// /s3 < s4 > s5
|
||||
// //
|
||||
// s6
|
||||
|
||||
|
||||
providers('s1', function(){ log.push('s1'); }, {$inject: ['s2', 's5', 's6']});
|
||||
providers('s2', function(){ log.push('s2'); }, {$inject: ['s3', 's4', 's5']});
|
||||
providers('s3', function(){ log.push('s3'); }, {$inject: ['s6']});
|
||||
providers('s4', function(){ log.push('s4'); }, {$inject: ['s3', 's5']});
|
||||
providers('s5', function(){ log.push('s5'); });
|
||||
providers('s6', function(){ log.push('s6'); });
|
||||
|
||||
inject('s1');
|
||||
|
||||
expect(log).toEqual(['s6', 's3', 's5', 's4', 's2', 's1']);
|
||||
});
|
||||
|
||||
|
||||
it('should provide usefull message if no provider', function(){
|
||||
assertThrows("Unknown provider for 'idontexist'.", function(){
|
||||
inject('idontexist');
|
||||
|
|
|
|||
Loading…
Reference in a new issue