-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created unit tests for function used for calculating avg value from a…
…n array.
- Loading branch information
1 parent
fbd06ce
commit 75cb365
Showing
2 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters