You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { describe, expect, it } from "vitest";
import { cmToIn, inToCm, kgToLb, lbToKg, mToIn, tToLb, toAxelWholeInches, toAxelWholePounds, weightValueToLb, dimValueToIn, normalizeWeightUnit, normalizeDimUnit, weightUnitAmbiguousMessage } from "@/modules/quote/unit-converter";
describe("unit-converter", () => {
it("227kg → 500.45lbROUND_HALF_UPJS 浮点 227×2.20462", () => {
expect(kgToLb(227)).toBe(500.45);
});
it("122cm → 48.03in", () => {
expect(cmToIn(122)).toBe(48.03);
});
it("1.2m → 47.24in", () => {
expect(mToIn(1.2)).toBe(47.24);
});
it("0.5t → 1102.31lb", () => {
expect(tToLb(0.5)).toBe(1102.31);
});
it("120cm 转 axel 尺寸向上取整为 48in", () => {
expect(toAxelWholeInches(cmToIn(120))).toBe(48);
});
it("1.2m 转 axel 尺寸向上取整为 48in", () => {
expect(toAxelWholeInches(mToIn(1.2))).toBe(48);
});
it("500kg 转 axel 重量向上取整为 1103lb", () => {
expect(toAxelWholePounds(kgToLb(500))).toBe(1103);
});
it("0.5t 转 axel 重量向上取整为 1103lb", () => {
expect(toAxelWholePounds(tToLb(0.5))).toBe(1103);
});
it("500lb 不变", () => {
expect(kgToLb(500 / 2.20462)).toBeCloseTo(500, 0);
});
it("lbToKg / inToCm 互逆", () => {
expect(lbToKg(500)).toBe(226.8);
expect(inToCm(48)).toBe(121.92);
expect(kgToLb(lbToKg(500))).toBeCloseTo(500, 1);
expect(cmToIn(inToCm(48))).toBeCloseTo(48, 1);
});
it("normalizeWeightUnit 支持别名,拒绝 ton/tons 歧义", () => {
expect(normalizeWeightUnit("LBS")).toBe("lb");
expect(normalizeWeightUnit("tonne")).toBe("t");
expect(normalizeWeightUnit("tonnes")).toBe("t");
expect(normalizeWeightUnit("t")).toBe("t");
expect(normalizeWeightUnit("ton")).toBeNull();
expect(normalizeWeightUnit("tons")).toBeNull();
expect(normalizeWeightUnit("stone")).toBeNull();
expect(weightUnitAmbiguousMessage("ton")).toMatch(/歧义/);
});
it("normalizeDimUnit 支持别名", () => {
expect(normalizeDimUnit("INCH")).toBe("in");
expect(normalizeDimUnit("meter")).toBe("m");
expect(normalizeDimUnit("CM")).toBe("cm");
});
it("weightValueToLb / dimValueToIn", () => {
expect(weightValueToLb(500, "kg")).toBe(1102.31);
expect(weightValueToLb(0.5, "t")).toBe(1102.31);
expect(dimValueToIn(1.2, "m")).toBe(47.24);
});
});