@@ -557,6 +557,78 @@ describe('Integration between Entity and AComponent', function()
557557 } ) ;
558558 } ) ;
559559
560+ describe ( 'Entity.deleteIfExist' , function ( )
561+ {
562+ it ( 'should delete the component' , function ( )
563+ {
564+ const e = Entity . createWorld ( ) ;
565+ e . add ( PositionComponent ) ;
566+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . true ;
567+ expect ( e . position . x ) . to . equal ( 0 ) ;
568+
569+ e . deleteIfExist ( PositionComponent ) ;
570+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . false ;
571+ expect ( e . position ) . to . be . undefined ;
572+ } ) ;
573+
574+ it ( 'should delete the component but others should still be available' , function ( )
575+ {
576+ const e = Entity . createWorld ( ) ;
577+ e . addMany ( [ PositionComponent , VelocityComponent ] ) ;
578+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . true ;
579+ expect ( e . position . x ) . to . equal ( 0 ) ;
580+ expect ( e . has ( [ VelocityComponent ] ) ) . to . be . true ;
581+ expect ( e . velocity . velocity ) . equal ( 0 ) ;
582+
583+ e . deleteIfExist ( PositionComponent ) ;
584+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . false ;
585+ expect ( e . position ) . to . be . undefined ;
586+ expect ( e . has ( [ VelocityComponent ] ) ) . to . be . true ;
587+ expect ( e . velocity . velocity ) . equal ( 0 ) ;
588+ } ) ;
589+
590+ it ( 'should delete the component when given a super class' , function ( )
591+ {
592+ const e = Entity . createWorld ( ) ;
593+ e . add ( SpriteComponent ) ;
594+ expect ( e . has ( [ DrawableComponent ] ) ) . to . be . true ;
595+ e . deleteIfExist ( DrawableComponent ) ;
596+ expect ( e . has ( [ DrawableComponent ] ) ) . to . be . false ;
597+ } ) ;
598+
599+ it ( 'should return this' , function ( )
600+ {
601+ const e = Entity . createWorld ( ) ;
602+ e . add ( SpriteComponent ) ;
603+ expect ( e . deleteIfExist ( SpriteComponent ) ) . to . equal ( e ) ;
604+ } ) ;
605+
606+ it ( 'should not throw if component is not available' , function ( )
607+ {
608+ const e = Entity . createWorld ( ) ;
609+
610+ e . deleteIfExist ( PositionComponent ) ;
611+ } ) ;
612+
613+ it ( 'should not throw on double-delete' , function ( )
614+ {
615+ const e = Entity . createWorld ( ) ;
616+ e . add ( PositionComponent ) ;
617+ e . deleteIfExist ( PositionComponent ) ;
618+ e . deleteIfExist ( PositionComponent ) ;
619+ } ) ;
620+
621+ it ( 'should throw if pass AComponent as it is a global super class' , function ( )
622+ {
623+ const e = Entity . createWorld ( ) ;
624+
625+ expect ( ( ) =>
626+ {
627+ e . deleteIfExist ( AComponent ) ;
628+ } ) . to . throw ( ) ;
629+ } ) ;
630+ } ) ;
631+
560632 describe ( 'Entity.deleteMany' , function ( )
561633 {
562634 it ( 'should delete all components' , function ( )
@@ -650,6 +722,94 @@ describe('Integration between Entity and AComponent', function()
650722 } ) ;
651723 } ) ;
652724
725+ describe ( 'Entity.deleteManyIfExist' , function ( )
726+ {
727+ it ( 'should delete all components' , function ( )
728+ {
729+ const e = Entity . createWorld ( ) ;
730+ e . addMany ( [ PositionComponent , VelocityComponent ] ) ;
731+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . true ;
732+ expect ( e . position . x ) . to . equal ( 0 ) ;
733+ expect ( e . has ( [ VelocityComponent ] ) ) . to . be . true ;
734+ expect ( e . velocity . velocity ) . equal ( 0 ) ;
735+
736+ e . deleteManyIfExist ( [ PositionComponent , VelocityComponent ] ) ;
737+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . false ;
738+ expect ( e . has ( [ VelocityComponent ] ) ) . to . be . false ;
739+ } ) ;
740+
741+ it ( 'should do nothing on passing empty-array' , function ( )
742+ {
743+ const e = Entity . createWorld ( ) ;
744+ e . addMany ( [ PositionComponent , VelocityComponent ] ) ;
745+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . true ;
746+ expect ( e . position . x ) . to . equal ( 0 ) ;
747+ expect ( e . has ( [ VelocityComponent ] ) ) . to . be . true ;
748+ expect ( e . velocity . velocity ) . equal ( 0 ) ;
749+
750+ e . deleteManyIfExist ( [ ] ) ;
751+ expect ( e . has ( [ PositionComponent ] ) ) . to . be . true ;
752+ expect ( e . position . x ) . to . equal ( 0 ) ;
753+ expect ( e . has ( [ VelocityComponent ] ) ) . to . be . true ;
754+ expect ( e . velocity . velocity ) . equal ( 0 ) ;
755+ } ) ;
756+
757+ it ( 'should not throw if at least one component is not available and delete nothing' , function ( )
758+ {
759+ const e1 = Entity . createWorld ( ) ;
760+ e1 . add ( PositionComponent ) ;
761+ e1 . deleteManyIfExist ( [ PositionComponent , VelocityComponent ] ) ;
762+ expect ( e1 . has ( [ PositionComponent ] ) ) . to . be . true ;
763+
764+ const e2 = Entity . createWorld ( ) ;
765+ e2 . deleteManyIfExist ( [ PositionComponent , VelocityComponent ] ) ;
766+ } ) ;
767+
768+ it ( 'should delete the component when given a super class' , function ( )
769+ {
770+ const e = Entity . createWorld ( ) ;
771+ e . add ( SpriteComponent ) ;
772+ expect ( e . has ( [ DrawableComponent ] ) ) . to . be . true ;
773+ e . deleteManyIfExist ( [ DrawableComponent ] ) ;
774+ expect ( e . has ( [ DrawableComponent ] ) ) . to . be . false ;
775+ } ) ;
776+
777+ it ( 'should return this' , function ( )
778+ {
779+ const e = Entity . createWorld ( ) ;
780+ e . add ( SpriteComponent ) ;
781+ expect ( e . deleteManyIfExist ( [ SpriteComponent ] ) ) . to . equal ( e ) ;
782+ } ) ;
783+
784+ it ( 'should throw on passing non-array' , function ( )
785+ {
786+ const e = Entity . createWorld ( ) ;
787+ expect ( ( ) =>
788+ {
789+ e . deleteManyIfExist ( 42 ) ;
790+ } ) . to . throw ( ) ;
791+ } ) ;
792+
793+ it ( 'should throw on passing non-components in array' , function ( )
794+ {
795+ const e = Entity . createWorld ( ) ;
796+ expect ( ( ) =>
797+ {
798+ e . deleteManyIfExist ( [ 42 ] ) ;
799+ } ) . to . throw ( ) ;
800+ } ) ;
801+
802+ it ( 'should throw if pass AComponent as it is a global super class' , function ( )
803+ {
804+ const e = Entity . createWorld ( ) ;
805+
806+ expect ( ( ) =>
807+ {
808+ e . deleteManyIfExist ( [ AComponent ] ) ;
809+ } ) . to . throw ( ) ;
810+ } ) ;
811+ } ) ;
812+
653813 describe ( '#destructor' , function ( )
654814 {
655815 it ( 'should be called on destructing entity with components' , function ( )
0 commit comments