@@ -322,6 +322,40 @@ describe("CollectionUtils", () => {
322322 } ) ;
323323 } ) ; // end isNotEmpty
324324
325+ describe ( "#length" , ( ) => {
326+ test ( `when collections param is an array and has elements, it returns the length` , ( ) : void => {
327+ // Arrange
328+ const array = [ "test" , "test" ] ;
329+
330+ // Act
331+ const result = CollectionUtils . length ( array ) ;
332+
333+ // Assert
334+ expect ( result ) . toBe ( 2 ) ;
335+ } ) ;
336+
337+ test ( `when collections param is null, it returns -1` , ( ) : void => {
338+ // Arrange
339+ const array = null ;
340+
341+ // Act
342+ const result = CollectionUtils . length ( array ) ;
343+
344+ // Assert
345+ expect ( result ) . toBe ( - 1 ) ;
346+ } ) ;
347+
348+ test ( `when collections param is a list and has elements, it returns the length` , ( ) : void => {
349+ // Arrange
350+ const list = List ( [ "test" , "test" ] ) ;
351+
352+ // Act
353+ const result = CollectionUtils . length ( list ) ;
354+
355+ // Assert
356+ expect ( result ) . toBe ( 2 ) ;
357+ } ) ;
358+ } )
325359 describe ( "#equalsBy" , ( ) => {
326360 type TestType = { id : number } ;
327361 const selector = ( t : TestType ) => t . id ;
@@ -442,5 +476,61 @@ describe("CollectionUtils", () => {
442476 ) ;
443477 expect ( equals ) . toBe ( true ) ;
444478 } ) ;
479+
480+ it ( "When source array has no values it, then returns the source array" , ( ) => {
481+ // Arrange
482+ const arr = [ ] ;
483+
484+ // Act
485+ const result = CollectionUtils . replaceElementAt ( arr , 1 , "replaced" ) ;
486+
487+ // Assert
488+ expect ( result ) . toBe ( arr ) ;
489+ } )
490+
491+ it ( "When source array has 1 item it, then returns new array with 1 item" , ( ) => {
492+ // Arrange
493+ const arr = [ "test" ] ;
494+ const expected = [ "replaced-test" ] ;
495+
496+ // Act
497+ const result = CollectionUtils . replaceElementAt ( arr , 2 , "replaced-test" ) ;
498+
499+ // Assert
500+ const equals = CollectionUtils . equalsBy (
501+ ( s : string ) => s ,
502+ result ,
503+ expected
504+ ) ;
505+ expect ( equals ) . toBe ( true ) ;
506+ } )
507+
508+ it ( "When index is last element of source it, then returns new array with value at the end" , ( ) => {
509+ // Arrange
510+ const arr = [ "zero" , "one" , "two" , "three" , "four" ] ;
511+ const expected = [ "zero" , "one" , "two" , "three" , "replaced" ] ;
512+
513+ // Act
514+ const result = CollectionUtils . replaceElementAt ( arr , 4 , "replaced" ) ;
515+
516+ // Assert
517+ const equals = CollectionUtils . equalsBy (
518+ ( s : string ) => s ,
519+ result ,
520+ expected
521+ ) ;
522+ expect ( equals ) . toBe ( true ) ;
523+ } )
524+
525+ it ( "When index i < 0, then returns the source array" , ( ) => {
526+ // Arrange
527+ const arr = [ "zero" , "one" , "two" , "three" , "four" ] ;
528+
529+ // Act
530+ const result = CollectionUtils . replaceElementAt ( arr , - 1 , "replaced" ) ;
531+
532+ // Assert
533+ expect ( result ) . toBe ( arr ) ;
534+ } )
445535 } ) ;
446536} ) ;
0 commit comments