@@ -712,7 +712,7 @@ describe("Definition Tests", () => {
712712 assert . strictEqual ( entity . stats . get ( 'hp' ) ?. value , 500 ) ;
713713 } ) ;
714714
715- it ( "should exclude parent props from encode method (2) " , ( ) => {
715+ it ( "should auto-initialize Schema instances with default values " , ( ) => {
716716 const AnotherRandomSchema = schema ( {
717717 value : { type : 'string' , default : 'world' , } ,
718718 } ) ;
@@ -730,10 +730,79 @@ describe("Definition Tests", () => {
730730 } ,
731731 } ) ;
732732
733- const entity = new StatSchema ( 5 ) ;
734- console . log ( entity . random . value , entity . random . anotherRandom . value ) ;
733+ assert . doesNotThrow ( ( ) => {
734+ const entity = new StatSchema ( 5 ) ;
735+ assert . strictEqual ( entity . value , 5 ) ;
736+ assert . strictEqual ( entity . random . value , 'hello' ) ;
737+ assert . strictEqual ( entity . random . anotherRandom . value , 'world' ) ;
738+
739+ } )
735740 } )
736741
742+ describe ( "extends" , ( ) => {
743+ it ( "should not call initialize automatically when creating an instance of inherited Schema (1)" , ( ) => {
744+ let childSchemaInitializeCallCount = 0 ;
745+
746+ const ChildSchema = schema ( {
747+ name : 'string' ,
748+ initialize ( props : { name : string } ) {
749+ this . name = props . name ;
750+ childSchemaInitializeCallCount ++ ;
751+ } ,
752+ } ) ;
753+
754+ const ParentSchema = ChildSchema . extends ( {
755+ id : 'string' ,
756+ initialize ( props : { id : string } ) {
757+ ChildSchema . prototype . initialize . call ( this , {
758+ name : 'Jim' ,
759+ } ) ;
760+ this . id = props . id ;
761+ } ,
762+ } ) ;
763+
764+ assert . doesNotThrow ( ( ) => {
765+ const parent = new ParentSchema ( { id : 'parent' } ) ;
766+ assert . strictEqual ( parent . id , 'parent' ) ;
767+ assert . strictEqual ( parent . name , 'Jim' ) ;
768+ } ) ;
769+
770+ assert . strictEqual ( childSchemaInitializeCallCount , 1 ) ;
771+ } )
772+
773+ it ( "should not call initialize automatically when creating an instance of inherited Schema (2)" , ( ) => {
774+ const EntitySchema = schema ( {
775+ id : 'string' ,
776+ name : 'string' ,
777+ initialize ( props : { id : string ; name : string } ) {
778+ this . id = props . id ;
779+ this . name = props . name ;
780+ } ,
781+ } ) ;
782+
783+ const PlayerSchema = EntitySchema . extends ( {
784+ initialize ( props : any ) {
785+ EntitySchema . prototype . initialize . call ( this , {
786+ id : props . public_id ,
787+ name : props . username ,
788+ } ) ;
789+ } ,
790+ } ) ;
791+
792+ const player = new EntitySchema ( { id : '1' , name : 'test' } ) ;
793+ assert . strictEqual ( player . id , '1' ) ;
794+ assert . strictEqual ( player . name , 'test' ) ;
795+
796+ assert . doesNotThrow ( ( ) => {
797+ const player = new PlayerSchema ( { id : 1 , public_id : '1' , username : 'test' } ) ;
798+ assert . strictEqual ( player . id , '1' ) ;
799+ assert . strictEqual ( player . name , 'test' ) ;
800+ } ) ;
801+ } ) ;
802+
803+ } ) ;
804+
805+
737806 } ) ;
738807
739808} ) ;
0 commit comments