This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +32
-6
lines changed
2 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -429,12 +429,15 @@ function createInjector(modulesToLoad) {
429
429
}
430
430
}
431
431
432
- function instantiate ( Type , locals ) {
433
- var Constructor = function ( ) { } ,
434
- instance ;
432
+ function instantiate ( Type , locals ) {
433
+ var Constructor = function ( ) { } ,
434
+ instance , returnedValue ;
435
+
435
436
Constructor . prototype = Type . prototype ;
436
437
instance = new Constructor ( ) ;
437
- return invoke ( Type , instance , locals ) || instance ;
438
+ returnedValue = invoke ( Type , instance , locals ) ;
439
+
440
+ return isObject ( returnedValue ) ? returnedValue : instance ;
438
441
}
439
442
440
443
return {
Original file line number Diff line number Diff line change @@ -606,8 +606,12 @@ describe('injector', function() {
606
606
607
607
608
608
it ( 'should allow constructor to return different object' , function ( ) {
609
- var t = $injector . instantiate ( function ( ) { return 'ABC' ; } ) ;
610
- expect ( t ) . toBe ( 'ABC' ) ;
609
+ var obj = { } ;
610
+ var Class = function ( ) {
611
+ return obj ;
612
+ } ;
613
+
614
+ expect ( $injector . instantiate ( Class ) ) . toBe ( obj ) ;
611
615
} ) ;
612
616
613
617
@@ -616,6 +620,25 @@ describe('injector', function() {
616
620
$injector . instantiate ( function ( ) { throw 'MyError' ; } ) ;
617
621
} ) . toThrow ( 'MyError' ) ;
618
622
} ) ;
623
+
624
+
625
+ it ( 'should return instance if constructor returns non-object value' , function ( ) {
626
+ var A = function ( ) {
627
+ return 10 ;
628
+ } ;
629
+
630
+ var B = function ( ) {
631
+ return 'some-string' ;
632
+ } ;
633
+
634
+ var C = function ( ) {
635
+ return undefined ;
636
+ } ;
637
+
638
+ expect ( $injector . instantiate ( A ) instanceof A ) . toBe ( true ) ;
639
+ expect ( $injector . instantiate ( B ) instanceof B ) . toBe ( true ) ;
640
+ expect ( $injector . instantiate ( C ) instanceof C ) . toBe ( true ) ;
641
+ } ) ;
619
642
} ) ;
620
643
621
644
describe ( 'protection modes' , function ( ) {
You can’t perform that action at this time.
0 commit comments