r-util-js/packages/common/test/input-rule.test.ts

167 lines
5.4 KiB
TypeScript
Raw Normal View History

2026-04-20 17:54:26 +08:00
import { test, expect, describe } from "vitest";
import { getValueOfRule, InputOptions } from "../src/input-rule";
describe("getValueOfRule - 文本模式 (text: true)", () => {
const textOpts: InputOptions = { text: true };
test("普通文本直接返回", () => {
expect(getValueOfRule("hello", { ...textOpts })).toBe("hello");
});
test("null 值返回空字符串", () => {
expect(getValueOfRule(null, { ...textOpts })).toBe("");
});
test("required + init非数字文本回退到 init", () => {
expect(getValueOfRule("abc", { ...textOpts, required: true, init: "默认" })).toBe("默认");
});
test("required 无 init非数字文本保持原值", () => {
expect(getValueOfRule("abc", { ...textOpts, required: true })).toBe("abc");
});
test("number 选项将结果转为数字", () => {
expect(getValueOfRule("123", { ...textOpts, number: true })).toBe(123);
});
test("pattern 匹配失败返回空字符串", () => {
expect(getValueOfRule("abc", { ...textOpts, pattern: "^\\d+$" })).toBe("");
});
test("pattern 匹配成功返回原值", () => {
expect(getValueOfRule("123", { ...textOpts, pattern: "^\\d+$" })).toBe("123");
});
});
describe("getValueOfRule - 数字模式", () => {
test("正常数字字符串返回字符串", () => {
expect(getValueOfRule("42", {})).toBe("42");
});
test("number 选项返回数字类型", () => {
expect(getValueOfRule("42", { number: true })).toBe(42);
});
test("非法输入 + required 回退到 init", () => {
expect(getValueOfRule("abc", { required: true, init: 10 })).toBe("10");
});
test("非法输入 + required 无 init 回退到 min", () => {
expect(getValueOfRule("abc", { required: true, min: 5 })).toBe("5");
});
test("非法输入 + required 无 init 无 min 回退到 0", () => {
expect(getValueOfRule("abc", { required: true })).toBe("0");
});
test("非法输入 + 非 required 返回空字符串", () => {
expect(getValueOfRule("abc", {})).toBe("");
});
test("null 值 + required 回退到 init", () => {
expect(getValueOfRule(null, { required: true, init: 0 })).toBe("0");
});
test("null 值 + 非 required 返回空字符串", () => {
expect(getValueOfRule(null, {})).toBe("");
});
});
describe("getValueOfRule - min/max 限制", () => {
test("值小于 min 时限制为 min", () => {
expect(getValueOfRule("3", { min: 5 })).toBe("5");
});
test("值大于 max 时限制为 max", () => {
expect(getValueOfRule("100", { max: 50 })).toBe("50");
});
test("值在 min/max 范围内保持不变", () => {
expect(getValueOfRule("25", { min: 0, max: 50 })).toBe("25");
});
test("负数受 min 限制", () => {
expect(getValueOfRule("-10", { min: 0 })).toBe("0");
});
});
describe("getValueOfRule - digits 小数位数", () => {
test("保留 2 位小数(四舍五入)", () => {
expect(getValueOfRule("3.456", { digits: 2 })).toBe("3.46");
});
test("保留 0 位小数", () => {
expect(getValueOfRule("3.6", { digits: 0 })).toBe("4");
});
test("floor 向下取整小数", () => {
expect(getValueOfRule("3.456", { digits: 2, floor: true })).toBe("3.45");
});
test("ceil 向上取整小数", () => {
expect(getValueOfRule("3.451", { digits: 2, ceil: true })).toBe("3.46");
});
test("keepDecimal 保留尾部零", () => {
expect(getValueOfRule("3", { digits: 2, keepDecimal: true })).toBe("3.00");
});
test("keepDecimal + number 返回数字类型", () => {
expect(getValueOfRule("3.10", { digits: 2, keepDecimal: true, number: true })).toBe(3.1);
});
});
describe("getValueOfRule - integer 整数模式", () => {
test("integer 四舍五入取整", () => {
expect(getValueOfRule("3.6", { integer: true })).toBe("4");
});
test("integer + floor 向下取整", () => {
expect(getValueOfRule("3.9", { integer: true, floor: true })).toBe("3");
});
test("integer + ceil 向上取整", () => {
expect(getValueOfRule("3.1", { integer: true, ceil: true })).toBe("4");
});
});
describe("getValueOfRule - noSign 不允许正负号", () => {
test("带正号的输入被视为非法", () => {
expect(getValueOfRule("+5", { noSign: true, required: true, init: 0 })).toBe("0");
});
test("带负号的输入被视为非法", () => {
expect(getValueOfRule("-5", { noSign: true, required: true, init: 0 })).toBe("0");
});
test("无符号数字正常通过", () => {
expect(getValueOfRule("5", { noSign: true })).toBe("5");
});
});
describe("getValueOfRule - callback", () => {
test("callback 接收最终处理后的值", () => {
let received: unknown;
getValueOfRule("42", { number: true }, (v) => { received = v; });
expect(received).toBe(42);
});
});
describe("getValueOfRule - 综合场景", () => {
test("限制输入 min:0, max:99999.99, required, digits:2", () => {
expect(getValueOfRule("100.456", { min: 0, max: 99999.99, required: true, digits: 2 })).toBe("100.46");
});
test("超出 max 时截断到 max", () => {
expect(getValueOfRule("100000", { min: 0, max: 99999.99, required: true, digits: 2 })).toBe("99999.99");
});
test("空输入 required 回退到 min", () => {
expect(getValueOfRule("", { min: 0, max: 99999.99, required: true, digits: 2 })).toBe("0");
});
test("负数输入限制到 min:0", () => {
expect(getValueOfRule("-5", { min: 0, max: 100, required: true })).toBe("0");
});
});