tests: implement more test cases for operations (#4587)
Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
1 parent
93e48939d5
commit
516472f17a
1 file changed
+68
-1
+68
-1
@@ -1,5 +1,13 @@
|
|||||||
import { describe, expect, it } from "@jest/globals";
|
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", () => {
|
describe("Test ops.js", () => {
|
||||||
it("should test parseBoolean", () => {
|
it("should test parseBoolean", () => {
|
||||||
@@ -19,4 +27,63 @@ describe("Test ops.js", () => {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
expect(parseBoolean(undefined)).toBe(undefined);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user