Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlundberg committed Sep 6, 2024
1 parent 6e0866c commit 8294a00
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
48 changes: 40 additions & 8 deletions __tests__/getMean.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
import getMean from "../src/lib/getMean";
import { FAKE_SESSION } from "../data/FAKE_SESSION";

test("Should get the mean from an array of solves", () => {
expect(getMean([])).toBe(0);
expect(getMean([...FAKE_SESSION])).toBe(13658.98076923077);
expect(getMean([...FAKE_SESSION].slice(0, 5))).toBe(13861.6);
expect(getMean([...FAKE_SESSION].slice(5, 12))).toBe(15207.42857142857);
expect(getMean([...FAKE_SESSION].slice(0, 12))).toBe(14646.666666666666);
expect(getMean([...FAKE_SESSION].slice(0, 50))).toBe(13738.22);
expect(getMean()).toBe(0);
describe("Should get the mean from an array of solves", () => {
test("Empty array", () => {
expect(getMean([])).toBe(0);
});

test("Full session, undetermined range of values", () => {
expect(getMean([...FAKE_SESSION])).toBe(13658.98076923077);
});

test("Session, 5 values", () => {
expect(getMean([...FAKE_SESSION].slice(0, 5))).toBe(13861.6);
});

test("Session, 12 values", () => {
expect(getMean([...FAKE_SESSION].slice(0, 12))).toBe(14646.666666666666);
});

test("Session, 50 values", () => {
expect(getMean([...FAKE_SESSION].slice(0, 50))).toBe(13738.22);
});

test("Empty usage of the function", () => {
expect(getMean()).toBe(0);
});

test("Passing a string as parameter", () => {
expect(getMean("string")).toBe(0);
});

test("Passing a number as parameter", () => {
expect(getMean(123)).toBe(0);
});

test("Passing a null as parameter", () => {
expect(getMean(null)).toBe(0);
});

test("Passing a undefined as parameter", () => {
expect(getMean(undefined)).toBe(0);
});
});
5 changes: 4 additions & 1 deletion src/lib/getMean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { Solve } from "@/interfaces/Solve";
* @returns {number} The mean of solve times. Returns 0 if there are no solves.
*/
export default function getMean(solves: Solve[]): number {
if (!solves) return 0;
if (!solves || typeof solves === "string" || typeof solves === "number") {
return 0;
}

const n = solves.length;

// If there are no solves, the mean is 0.
Expand Down

0 comments on commit 8294a00

Please sign in to comment.