@@ -56,6 +56,35 @@ function factory1(v1) {
5656 } ;
5757}
5858
59+ function ClassWithNamedConstructor ( ) {
60+
61+ }
62+
63+ ClassWithNamedConstructor . createInstance = function ( ) {
64+ var instance = new ClassWithNamedConstructor ( ) ;
65+ instance . createInstanceCalled = true ;
66+ instance . createInstanceArgs = Array . prototype . slice . call ( arguments ) ;
67+ instance . thisSetInCreateInstance = this ;
68+
69+ return instance ;
70+ } ;
71+
72+ ClassWithNamedConstructor . createInstancePromise = function ( ) {
73+ return {
74+ then : function ( f ) {
75+ f ( ClassWithNamedConstructor . createInstance ( ) ) ;
76+ }
77+ } ;
78+ } ;
79+
80+ ClassWithNamedConstructor . createInstanceReject = function ( ) {
81+ return {
82+ then : function ( f , reject ) {
83+ reject ( new Error ( 'test error' ) ) ;
84+ }
85+ } ;
86+ } ;
87+
5988buster . testCase ( 'lib/plugin/basePlugin' , {
6089 'module factory' : {
6190 'should use module exports value as component' : function ( ) {
@@ -506,6 +535,91 @@ buster.testCase('lib/plugin/basePlugin', {
506535 } ,
507536 fail
508537 ) ;
538+ } ,
539+ 'constructorName' : {
540+ 'should use static named constructor if constructorName is set' : function ( ) {
541+ return createContext ( {
542+ test : {
543+ create : {
544+ module : ClassWithNamedConstructor ,
545+ constructorName : 'createInstance'
546+ }
547+ }
548+ } ) . then ( function ( wired ) {
549+ assert ( 'test' in wired ) ;
550+ assert ( wired . test . createInstanceCalled ) ;
551+ } ) ;
552+ } ,
553+ 'should pass arguments to named constructor' : function ( ) {
554+ return createContext ( {
555+ test : {
556+ create : {
557+ module : ClassWithNamedConstructor ,
558+ constructorName : 'createInstance' ,
559+ args : [ 'foo' , 'bar' , 1 , 1.2 ]
560+ }
561+ }
562+ } ) . then ( function ( wired ) {
563+ assert ( 'test' in wired ) ;
564+ assert . equals ( wired . test . createInstanceArgs , [ 'foo' , 'bar' , 1 , 1.2 ] ) ;
565+ } ) ;
566+ } ,
567+ 'should work with module loaded by reference' : function ( ) {
568+ return createContext ( {
569+ reference : {
570+ module : ClassWithNamedConstructor ,
571+ } ,
572+ test : {
573+ create : {
574+ module : { $ref : 'reference' } ,
575+ constructorName : 'createInstance'
576+ }
577+ }
578+ } ) . then ( function ( wired ) {
579+ assert ( 'test' in wired ) ;
580+ assert ( wired . test . createInstanceCalled ) ;
581+ } ) ;
582+ } ,
583+ 'this in named constructor should be the constructor on which it is called' : function ( ) {
584+ return createContext ( {
585+ test : {
586+ create : {
587+ module : ClassWithNamedConstructor ,
588+ constructorName : 'createInstance' ,
589+ }
590+ }
591+ } ) . then ( function ( wired ) {
592+ assert ( 'test' in wired ) ;
593+ assert . equals ( wired . test . thisSetInCreateInstance , ClassWithNamedConstructor ) ;
594+ } ) ;
595+ } ,
596+ 'should support constructors returning promises or promise-like objects' : function ( ) {
597+ return createContext ( {
598+ test : {
599+ create : {
600+ module : ClassWithNamedConstructor ,
601+ constructorName : 'createInstancePromise' ,
602+ }
603+ }
604+ } ) . then ( function ( wired ) {
605+ assert ( 'test' in wired ) ;
606+ assert ( wired . test instanceof ClassWithNamedConstructor ) ;
607+ } ) ;
608+ } ,
609+ 'should reject if named constructor rejects' : function ( ) {
610+ return createContext ( {
611+ test : {
612+ create : {
613+ module : ClassWithNamedConstructor ,
614+ constructorName : 'createInstanceReject' ,
615+ }
616+ }
617+ } ) . then ( function ( ) {
618+ throw new Error ( 'Unexpected success' ) ;
619+ } , function ( err ) {
620+ assert . equals ( err . message , 'test error' ) ;
621+ } ) ;
622+ }
509623 }
510624 } ,
511625
0 commit comments