Skip to content

Commit 2c66bad

Browse files
Issue #6 Write tests for CollectionUtils
Added tests for collection-utils methods replaceelementAt and length
2 parents 12af5cc + 758e72f commit 2c66bad

2 files changed

Lines changed: 133 additions & 11 deletions

File tree

src/utilities/collection-utils.test.ts

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { CollectionUtils } from "./collection-utils";
33
import { ResultRecord } from "../view-models/result-record";
44

55
describe("CollectionUtils", () => {
6+
// -----------------------------------------------------------------------------------------
7+
// #region hasValues
8+
// -----------------------------------------------------------------------------------------
9+
610
describe("hasValues", () => {
711
test("when collections param is an array and has elements, it returns true", (): void => {
812
// Arrange
@@ -108,7 +112,13 @@ describe("CollectionUtils", () => {
108112
// Assert
109113
expect(result).toBeTrue();
110114
});
111-
}); // end hasValues
115+
});
116+
117+
// #endregion hasValues
118+
119+
// -----------------------------------------------------------------------------------------
120+
// #region isEmpty
121+
// -----------------------------------------------------------------------------------------
112122

113123
describe("isEmpty", () => {
114124
test(`when collections param is an array and has elements, it returns false`, (): void => {
@@ -213,7 +223,13 @@ describe("CollectionUtils", () => {
213223
// Assert
214224
expect(result).toBeFalse();
215225
});
216-
}); // end isEmpty()
226+
});
227+
228+
// #endregion isEmpty
229+
230+
// -----------------------------------------------------------------------------------------
231+
// #region isNotEmpty
232+
// -----------------------------------------------------------------------------------------
217233

218234
describe("#isNotEmpty", () => {
219235
test(`when collections param is an array and has elements, it returns true`, (): void => {
@@ -320,7 +336,54 @@ describe("CollectionUtils", () => {
320336
// Assert
321337
expect(result).toBeTrue();
322338
});
323-
}); // end isNotEmpty
339+
});
340+
341+
// #endregion isNotEmpty
342+
343+
// -----------------------------------------------------------------------------------------
344+
// #region length
345+
// -----------------------------------------------------------------------------------------
346+
347+
describe("#length", () => {
348+
test(`when collections param is an array and has elements, it returns the length`, (): void => {
349+
// Arrange
350+
const array = ["test", "test"];
351+
352+
// Act
353+
const result = CollectionUtils.length(array);
354+
355+
// Assert
356+
expect(result).toBe(array.length);
357+
});
358+
359+
test(`when collections param is null, it returns -1`, (): void => {
360+
// Arrange
361+
const array = null;
362+
363+
// Act
364+
const result = CollectionUtils.length(array);
365+
366+
// Assert
367+
expect(result).toBe(-1);
368+
});
369+
370+
test(`when collections param is a list and has elements, it returns the length`, (): void => {
371+
// Arrange
372+
const list = List(["test","test"]);
373+
374+
// Act
375+
const result = CollectionUtils.length(list);
376+
377+
// Assert
378+
expect(result).toBe(list.size);
379+
});
380+
})
381+
382+
// #endregion length
383+
384+
// -----------------------------------------------------------------------------------------
385+
// #region equalsBy
386+
// -----------------------------------------------------------------------------------------
324387

325388
describe("#equalsBy", () => {
326389
type TestType = { id: number };
@@ -386,6 +449,12 @@ describe("CollectionUtils", () => {
386449
});
387450
});
388451

452+
// #endregion equalsBy
453+
454+
// -----------------------------------------------------------------------------------------
455+
// #region removeElementAt
456+
// -----------------------------------------------------------------------------------------
457+
389458
describe("#removeElementAt", () => {
390459
it("when index i < 0, returns source array", () => {
391460
// Arrange
@@ -425,6 +494,12 @@ describe("CollectionUtils", () => {
425494
});
426495
});
427496

497+
// #endregion removeElementAt
498+
499+
// -----------------------------------------------------------------------------------------
500+
// #region replaceElementAt
501+
// -----------------------------------------------------------------------------------------
502+
428503
describe("#replaceElementAt", () => {
429504
it("Replaces element at specified index and returns a new array", () => {
430505
// Arrange
@@ -435,12 +510,58 @@ describe("CollectionUtils", () => {
435510
const result = CollectionUtils.replaceElementAt(arr, 1, "replaced");
436511

437512
// Assert
438-
const equals = CollectionUtils.equalsBy(
439-
(s: string) => s,
440-
result,
441-
expected
442-
);
443-
expect(equals).toBe(true);
513+
expect(result).not.toBe(arr);
514+
expect(result).toStrictEqual(expected);
444515
});
516+
517+
it("When source array has no values it, then returns the source array", () => {
518+
// Arrange
519+
const arr = [];
520+
521+
// Act
522+
const result = CollectionUtils.replaceElementAt(arr, 1, "replaced");
523+
524+
// Assert
525+
expect(result).toBe(arr);
526+
})
527+
528+
it("When source array has 1 item it, then returns new array with 1 item", () => {
529+
// Arrange
530+
const arr = ["test"];
531+
const expected = ["replaced-test"];
532+
533+
// Act
534+
const result = CollectionUtils.replaceElementAt(arr, 2, "replaced-test");
535+
536+
// Assert
537+
expect(result).not.toBe(arr);
538+
expect(result).toStrictEqual(expected);
539+
})
540+
541+
it("When index is last element of source it, then returns new array with value at the end", () => {
542+
// Arrange
543+
const arr = ["zero", "one", "two", "three", "four"];
544+
const expected = ["zero", "one", "two", "three", "replaced"];
545+
546+
// Act
547+
const result = CollectionUtils.replaceElementAt(arr, 4, "replaced");
548+
549+
// Assert
550+
expect(result).not.toBe(arr);
551+
expect(result).toStrictEqual(expected);
552+
})
553+
554+
it("When index i < 0, then returns the source array", () => {
555+
// Arrange
556+
const arr = ["zero", "one", "two", "three", "four"];
557+
558+
// Act
559+
const result = CollectionUtils.replaceElementAt(arr, -1, "replaced");
560+
561+
// Assert
562+
expect(result).toBe(arr);
563+
})
445564
});
565+
566+
// #endregion replaceElementAt
446567
});

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)