diff --git a/tests/ops.test.js b/tests/ops.test.js index 5d8644a1..dd4e08b8 100644 --- a/tests/ops.test.js +++ b/tests/ops.test.js @@ -1,5 +1,13 @@ import { describe, expect, it } from "@jest/globals"; -import { parseBoolean } from "../src/common/ops.js"; +import { + parseBoolean, + parseArray, + clampValue, + lowercaseTrim, + chunkArray, + parseEmojis, + dateDiff, +} from "../src/common/ops.js"; describe("Test ops.js", () => { it("should test parseBoolean", () => { @@ -19,4 +27,63 @@ describe("Test ops.js", () => { // @ts-ignore expect(parseBoolean(undefined)).toBe(undefined); }); + + it("should test parseArray", () => { + expect(parseArray("a,b,c")).toEqual(["a", "b", "c"]); + expect(parseArray("a, b, c")).toEqual(["a", " b", " c"]); // preserves spaces + expect(parseArray("")).toEqual([]); + // @ts-ignore + expect(parseArray(undefined)).toEqual([]); + }); + + it("should test clampValue", () => { + expect(clampValue(5, 1, 10)).toBe(5); + expect(clampValue(0, 1, 10)).toBe(1); + expect(clampValue(15, 1, 10)).toBe(10); + + // string inputs are coerced numerically by Math.min/Math.max + // @ts-ignore + expect(clampValue("7", 1, 10)).toBe(7); + + // non-numeric and NaN fall back to min + // @ts-ignore + expect(clampValue("abc", 1, 10)).toBe(1); + expect(clampValue(NaN, 2, 5)).toBe(2); + }); + + it("should test lowercaseTrim", () => { + expect(lowercaseTrim(" Hello World ")).toBe("hello world"); + expect(lowercaseTrim("already lower")).toBe("already lower"); + }); + + it("should test chunkArray", () => { + expect(chunkArray([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]); + expect(chunkArray([1, 2, 3, 4, 5], 1)).toEqual([[1], [2], [3], [4], [5]]); + expect(chunkArray([1, 2, 3, 4, 5], 10)).toEqual([[1, 2, 3, 4, 5]]); + }); + + it("should test parseEmojis", () => { + // unknown emoji name is stripped + expect(parseEmojis("Hello :nonexistent:")).toBe("Hello "); + // common emoji names should be replaced (at least token removed) + const out = parseEmojis("I :heart: OSS"); + expect(out).not.toContain(":heart:"); + expect(out.startsWith("I ")).toBe(true); + expect(out.endsWith(" OSS")).toBe(true); + + expect(() => parseEmojis("")).toThrow(/parseEmoji/); + // @ts-ignore + expect(() => parseEmojis()).toThrow(/parseEmoji/); + }); + + it("should test dateDiff", () => { + const a = new Date("2020-01-01T00:10:00Z"); + const b = new Date("2020-01-01T00:00:00Z"); + expect(dateDiff(a, b)).toBe(10); + + const c = new Date("2020-01-01T00:00:00Z"); + const d = new Date("2020-01-01T00:10:30Z"); + // rounds to nearest minute + expect(dateDiff(c, d)).toBe(-10); + }); });