@@ -449,18 +449,76 @@ describe('FormComponent test suite', () => {
449449 } ) ) ;
450450
451451 it ( 'should dispatch FormChangeAction when an item has been removed from an array' , inject ( [ FormBuilderService ] , ( service : FormBuilderService ) => {
452- formComp . removeItem ( new Event ( 'click' ) , formComp . formModel [ 0 ] as DynamicFormArrayModel , 0 ) ;
452+ // Add a second item first so we can actually remove one (the last item is never removed, only cleared)
453+ formComp . insertItem ( new Event ( 'click' ) , formComp . formModel [ 0 ] as DynamicFormArrayModel , 1 ) ;
454+ ( store . dispatch as jasmine . Spy ) . calls . reset ( ) ;
455+
456+ formComp . removeItem ( new Event ( 'click' ) , formComp . formModel [ 0 ] as DynamicFormArrayModel , 1 ) ;
453457
454458 expect ( store . dispatch ) . toHaveBeenCalledWith ( new FormChangeAction ( 'testFormArray' , service . getValueFromModel ( formComp . formModel ) ) ) ;
455459 } ) ) ;
456460
457461 it ( 'should emit removeArrayItem Event when an item has been removed from an array' , inject ( [ FormBuilderService ] , ( service : FormBuilderService ) => {
462+ // Add a second item first so we can actually remove one
463+ formComp . insertItem ( new Event ( 'click' ) , formComp . formModel [ 0 ] as DynamicFormArrayModel , 1 ) ;
458464 spyOn ( formComp . removeArrayItem , 'emit' ) ;
459465
460- formComp . removeItem ( new Event ( 'click' ) , formComp . formModel [ 0 ] as DynamicFormArrayModel , 0 ) ;
466+ formComp . removeItem ( new Event ( 'click' ) , formComp . formModel [ 0 ] as DynamicFormArrayModel , 1 ) ;
461467
462468 expect ( formComp . removeArrayItem . emit ) . toHaveBeenCalled ( ) ;
463469 } ) ) ;
470+
471+ it ( 'should not remove the last item from the array, but clear its value instead' , inject ( [ FormBuilderService ] , ( service : FormBuilderService ) => {
472+ const arrayModel = formComp . formModel [ 0 ] as DynamicFormArrayModel ;
473+
474+ // Verify there is exactly 1 item in the array
475+ expect ( arrayModel . groups . length ) . toBe ( 1 ) ;
476+
477+ // Attempt to remove the last (and only) item
478+ formComp . removeItem ( new Event ( 'click' ) , arrayModel , 0 ) ;
479+
480+ // The array should still have 1 item (not removed, just cleared)
481+ expect ( arrayModel . groups . length ) . toBe ( 1 ) ;
482+ } ) ) ;
483+
484+ it ( 'should clear the value of the last item when trying to remove it' , inject ( [ FormBuilderService ] , ( service : FormBuilderService ) => {
485+ const arrayModel = formComp . formModel [ 0 ] as DynamicFormArrayModel ;
486+ const formArrayControl = formComp . formGroup . get ( service . getPath ( arrayModel ) ) ;
487+
488+ // Set a value on the input
489+ const inputControl = formArrayControl . get ( [ 0 , 'bootstrapArrayGroupInput' ] ) ;
490+ inputControl . setValue ( 'test value' ) ;
491+ expect ( inputControl . value ) . toBe ( 'test value' ) ;
492+
493+ // Attempt to remove the last item - should clear value instead
494+ formComp . removeItem ( new Event ( 'click' ) , arrayModel , 0 ) ;
495+
496+ // Value should be cleared
497+ expect ( inputControl . value ) . toBeNull ( ) ;
498+ // But the array should still have 1 group
499+ expect ( arrayModel . groups . length ) . toBe ( 1 ) ;
500+ } ) ) ;
501+
502+ it ( 'should allow removing items when there are multiple items in the array' , inject ( [ FormBuilderService ] , ( service : FormBuilderService ) => {
503+ const arrayModel = formComp . formModel [ 0 ] as DynamicFormArrayModel ;
504+
505+ // Add more items
506+ formComp . insertItem ( new Event ( 'click' ) , arrayModel , 1 ) ;
507+ formComp . insertItem ( new Event ( 'click' ) , arrayModel , 2 ) ;
508+ expect ( arrayModel . groups . length ) . toBe ( 3 ) ;
509+
510+ // Remove one item - should actually remove it
511+ formComp . removeItem ( new Event ( 'click' ) , arrayModel , 2 ) ;
512+ expect ( arrayModel . groups . length ) . toBe ( 2 ) ;
513+
514+ // Remove another
515+ formComp . removeItem ( new Event ( 'click' ) , arrayModel , 1 ) ;
516+ expect ( arrayModel . groups . length ) . toBe ( 1 ) ;
517+
518+ // Try to remove the last one - should NOT remove it
519+ formComp . removeItem ( new Event ( 'click' ) , arrayModel , 0 ) ;
520+ expect ( arrayModel . groups . length ) . toBe ( 1 ) ;
521+ } ) ) ;
464522 } ) ;
465523} ) ;
466524
0 commit comments