diff options
| author | Misko Hevery | 2010-10-08 17:30:13 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-10-12 16:33:06 -0700 |
| commit | d9abfe8a7e488be8725f56077527b16f7c79546a (patch) | |
| tree | 67089c5d2059e7a56afab0fec19dbce76fdab798 /test/InjectorSpec.js | |
| parent | fbfd160316de1b99e7afa4102c7fae2ee5b9c1f5 (diff) | |
| download | angular.js-d9abfe8a7e488be8725f56077527b16f7c79546a.tar.bz2 | |
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
Diffstat (limited to 'test/InjectorSpec.js')
| -rw-r--r-- | test/InjectorSpec.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js new file mode 100644 index 00000000..ba0f27f7 --- /dev/null +++ b/test/InjectorSpec.js @@ -0,0 +1,71 @@ +describe('injector', function(){ + var providers; + var cache; + var inject; + var scope; + + beforeEach(function(){ + providers = extensionMap({}, 'providers'); + cache = {}; + scope = {}; + inject = createInjector(scope, providers, cache); + }); + + it("should return same instance from calling provider", function(){ + providers('text', function(){ return scope.name; }); + scope.name = 'abc'; + expect(inject('text')).toEqual('abc'); + expect(cache.text).toEqual('abc'); + scope.name = 'deleted'; + expect(inject('text')).toEqual('abc'); + }); + + it("should return an array of instances", function(){ + cache.a = 0; + providers('b', function(){return 2;}); + expect(inject(['a', 'b'])).toEqual([0,2]); + }); + + it("should call function", function(){ + providers('a', function(){return 1;}); + providers('b', function(){return 2;}); + var args; + function fn(a, b, c, d) { + args = [this, a, b, c, d]; + } + fn.$inject = ['a', 'b']; + inject(fn, {name:"this"}, 3, 4); + expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); + }); + + it('should inject providers', function(){ + providers('a', function(){return this.mi = 'Mi';}); + providers('b', function(mi){return this.name = mi+'sko';}, {$inject:['a']}); + expect(inject('b')).toEqual('Misko'); + expect(scope).toEqual({mi:'Mi', name:'Misko'}); + }); + + it('should provide usefull message if no provider', function(){ + assertThrows("Unknown provider for 'idontexist'.", function(){ + inject('idontexist'); + }); + }); + + it('should autostart eager services', function(){ + var log = ''; + providers('eager', function(){log += 'eager;';}, {$creation: 'eager'}); + inject(); + expect(log).toEqual('eager;'); + expect(scope.eager).not.toBeDefined(); + }); + + + it('should return a list of published objects', function(){ + var log = ''; + providers('eager', function(){log += 'eager;'; return 'pub'; }, {$creation: 'eager-published'}); + inject(); + expect(log).toEqual('eager;'); + expect(scope.eager).toEqual('pub'); + + }); +});
\ No newline at end of file |
