Skip to content

Commit c1fdf04

Browse files
author
Said Shah
committed
Added tests for collection-utils methods replaceelementAt and length
1 parent 12af5cc commit c1fdf04

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

src/utilities/collection-utils.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

src/utilities/collection-utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ const _removeElementAt = <T>(source: Array<T>, index: number): Array<T> => {
155155

156156
/**
157157
* Returns a NEW array with the element at the specified index
158-
* replaced with the specified value. Since it returns a new array,
158+
* replaced with the specified value if the index provided is
159+
* greater than zero, else it returns the source array. Since it returns a new array,
159160
* this can be safely used as the value for a React.SetStateAction
160161
* i.e. setMyArray(CollectionUtils.replaceElementAt(myArray, index, newValue));
161162
* @param source
@@ -167,7 +168,7 @@ const _replaceElementAt = <T>(
167168
index: number,
168169
value: T
169170
): Array<T> => {
170-
if (source.length === 0) {
171+
if (source.length === 0 || index < 0) {
171172
return source;
172173
}
173174
if (source.length === 1) {

0 commit comments

Comments
 (0)