Skip to content

Commit

Permalink
Created unit tests for function used for calculating avg value from a…
Browse files Browse the repository at this point in the history
…n array.
  • Loading branch information
allanchua101 committed Dec 28, 2023
1 parent fbd06ce commit 75cb365
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
69 changes: 69 additions & 0 deletions cli-tests/helpers/reducers/avg-by-prop.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { getAverageByProp } from "#helpers/reducers/avg-by-prop.mjs";
const FUNCTIONS = [
{
runtime: "nodejs18.x",
memorySize: 1024,
ephemeralStorage: {
size: 512,
},
},
{
runtime: "nodejs18.x",
memorySize: 1024,
ephemeralStorage: {
size: 512,
},
},
{
runtime: "nodejs20.x",
memorySize: 128,
ephemeralStorage: {
size: 512,
},
},
{
runtime: "nodejs20.x",
memorySize: 128,
ephemeralStorage: {
size: 512,
},
},
];

describe("getAverageByProp", () => {
test.each([
["memorySize", 576],
["ephemeralStorage.size", 512],
])(
"Should return the average value for the specified dotNotation.",
(dotNotation, expected) => {
const result = getAverageByProp(FUNCTIONS, dotNotation);

expect(result).toEqual(expected);
}
);

test.each([[null], [undefined], [""], [false], [true], [1], [522], [54]])(
"Should throw an error for bad dot notation entries %#",
(dotNotation) => {
expect(() => {
getAverageByProp(FUNCTIONS, dotNotation);
}).toThrow("JSON dot notation is required.");
}
);

test.each([[null], [undefined], [""], [false], [true], [1], [522], [54]])(
"Should throw an invalid array error %#",
(nonArrayInput) => {
expect(() => {
getAverageByProp(nonArrayInput, "memorySize");
}).toThrow("Invalid array");
}
);

it("Should return 0 for empty arrays", () => {
const result = getAverageByProp([], "ephemeralStorage.size");

expect(result).toEqual(0);
});
});
14 changes: 9 additions & 5 deletions cli/helpers/reducers/avg-by-prop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ import { getNestedProperty } from "../json/get-nested-property.mjs";
* @function getAverageByProp
* @description Method used for retrieving the average value for the specified property
* @param {object[]} array List of input objects
* @param {string} property JSON notation path of property that will be used for average calculation.
* @param {string} dotNotation JSON notation path of property that will be used for average calculation.
* @returns {number}
*/
export function getAverageByProp(array, property) {
export function getAverageByProp(array, dotNotation) {
if (!Array.isArray(array)) {
throw new Error("Invalid array");
}

if (typeof property !== "string") {
throw new Error("Property input should be a string.");
if (typeof dotNotation !== "string") {
throw new Error("JSON dot notation is required.");
}

if (!dotNotation) {
throw new Error("JSON dot notation is required.");
}

if (array && array.length <= 0) {
return 0;
}

const total = (array || []).reduce(
(total, item) => total + (getNestedProperty(item, property) || 0),
(total, item) => total + (getNestedProperty(item, dotNotation) || 0),
0
);

Expand Down

0 comments on commit 75cb365

Please sign in to comment.