|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { applyCssUnits } from "../helpers"; |
| 3 | +import { type CSSUnitMap } from "../types"; |
| 4 | + |
| 5 | +describe("applyCssUnits", () => { |
| 6 | + it("returns string values as-is", () => { |
| 7 | + expect(applyCssUnits("fontSize", "2em")).toBe("2em"); |
| 8 | + expect(applyCssUnits("color", "red")).toBe("red"); |
| 9 | + }); |
| 10 | + |
| 11 | + it('returns "0" as-is (without unit)', () => { |
| 12 | + expect(applyCssUnits("marginTop", 0)).toBe("0"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("applies default px unit to numeric values", () => { |
| 16 | + expect(applyCssUnits("marginTop", 10)).toBe("10px"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("uses custom unit string when provided", () => { |
| 20 | + expect(applyCssUnits("fontSize", 1.5, "em")).toBe("1.5em"); |
| 21 | + }); |
| 22 | + |
| 23 | + it("uses unit map when provided", () => { |
| 24 | + const unitMap: CSSUnitMap = { |
| 25 | + fontSize: "rem", |
| 26 | + marginTop: "%", |
| 27 | + }; |
| 28 | + expect(applyCssUnits("fontSize", 2, unitMap)).toBe("2rem"); |
| 29 | + expect(applyCssUnits("marginTop", 5, unitMap)).toBe("5%"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("falls back to default px if unit not found in map", () => { |
| 33 | + expect(applyCssUnits("paddingLeft", 8, {})).toBe("8px"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("omits unit for known unitless properties", () => { |
| 37 | + // @emotion/unitless is used to define unitless properties |
| 38 | + expect(applyCssUnits("lineHeight", 1.2)).toBe("1.2"); |
| 39 | + expect(applyCssUnits("zIndex", 2)).toBe("2"); |
| 40 | + expect(applyCssUnits("flex", 1)).toBe("1"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("throws if value is not string or number", () => { |
| 44 | + // @ts-expect-error - testing invalid input |
| 45 | + expect(() => applyCssUnits("fontSize", null)).toThrowError(); |
| 46 | + // @ts-expect-error - testing invalid input |
| 47 | + expect(() => applyCssUnits("fontSize", {})).toThrowError(); |
| 48 | + }); |
| 49 | +}); |
0 commit comments