|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | + |
| 3 | +import { stringifyCSSProperties } from "../stringifyCSSProperties"; |
| 4 | + |
| 5 | +describe("stringifyCSSProperties", () => { |
| 6 | + it("returns string", () => { |
| 7 | + expect(stringifyCSSProperties({ color: "teal" })).toBeTypeOf("string"); |
| 8 | + }); |
| 9 | + |
| 10 | + it("returns empty string for empty object input", () => { |
| 11 | + expect(stringifyCSSProperties({})).toBe(""); |
| 12 | + }); |
| 13 | + |
| 14 | + it("throws error for string input", () => { |
| 15 | + //@ts-ignore |
| 16 | + expect(() => stringifyCSSProperties("")).toThrowError( |
| 17 | + "Invalid input: 'cssProperties' must be an object." |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it("throws error for 'null' input", () => { |
| 22 | + //@ts-ignore |
| 23 | + expect(() => stringifyCSSProperties(null)).toThrowError( |
| 24 | + "Invalid input: 'cssProperties' must be an object." |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it("throws error for wrong CSS-value", () => { |
| 29 | + expect(() => |
| 30 | + //@ts-ignore |
| 31 | + stringifyCSSProperties({ color: { color: "teal" } }) |
| 32 | + ).toThrowError( |
| 33 | + "Invalid input: value of 'cssProperties' must be string or number." |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("doesn't change string CSS-value", () => { |
| 38 | + const expected = "color:teal; margin:20rem; padding:5px 10px;"; |
| 39 | + const actual = stringifyCSSProperties({ |
| 40 | + color: "teal", |
| 41 | + margin: "20rem", |
| 42 | + padding: "5px 10px", |
| 43 | + }); |
| 44 | + |
| 45 | + expect(actual).toBe(expected); |
| 46 | + }); |
| 47 | + |
| 48 | + it("converts CSS-prop name from camel to kebab case", () => { |
| 49 | + const expected = |
| 50 | + "margin-bottom:20px; background-color:teal; border-radius:30rem; font-family:sans-serif;"; |
| 51 | + const actual = stringifyCSSProperties({ |
| 52 | + marginBottom: "20px", |
| 53 | + backgroundColor: "teal", |
| 54 | + borderRadius: "30rem", |
| 55 | + fontFamily: "sans-serif", |
| 56 | + }); |
| 57 | + |
| 58 | + expect(actual).toBe(expected); |
| 59 | + }); |
| 60 | + |
| 61 | + it("adds 'px' to numeric CSS-value", () => { |
| 62 | + const expected = "margin:20px; padding:5px;"; |
| 63 | + const actual = stringifyCSSProperties({ margin: 20, padding: 5 }); |
| 64 | + |
| 65 | + expect(actual).toBe(expected); |
| 66 | + }); |
| 67 | + |
| 68 | + it("doesn't add 'px' to unitless CSS-prop and '0' CSS-value", () => { |
| 69 | + const expected = "z-index:20; flex:1; opacity:0.5; margin:0; padding:0;"; |
| 70 | + const actual = stringifyCSSProperties({ |
| 71 | + zIndex: 20, |
| 72 | + flex: 1, |
| 73 | + opacity: 0.5, |
| 74 | + margin: 0, |
| 75 | + padding: 0, |
| 76 | + }); |
| 77 | + |
| 78 | + expect(actual).toBe(expected); |
| 79 | + }); |
| 80 | +}); |
0 commit comments