|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { extractClassCandidates } from "./classCandidates"; |
| 3 | + |
| 4 | +function bases(source: string): string[] { |
| 5 | + return extractClassCandidates(source) |
| 6 | + .map((candidate) => candidate.base) |
| 7 | + .sort(); |
| 8 | +} |
| 9 | + |
| 10 | +describe("extractClassCandidates", () => { |
| 11 | + it("reads a plain className attribute", () => { |
| 12 | + expect(bases(`<div className="text-center border-dashed" />`)).toEqual([ |
| 13 | + "border-dashed", |
| 14 | + "text-center", |
| 15 | + ]); |
| 16 | + }); |
| 17 | + |
| 18 | + it("reads every string a class-building call is given", () => { |
| 19 | + const source = `<div className={cn("flex", open && "bg-surface", { "text-accent": on })} />`; |
| 20 | + |
| 21 | + expect(bases(source)).toEqual(["bg-surface", "flex", "text-accent"]); |
| 22 | + }); |
| 23 | + |
| 24 | + it("reads a class map that never touches a className literal", () => { |
| 25 | + // The shape Studio's primitives use: the utility only reaches markup |
| 26 | + // through a lookup, so an attribute-only reader would miss it entirely. |
| 27 | + const source = `const sizeStyles: Record<ButtonSize, string> = { |
| 28 | + sm: "h-7 px-2.5 rounded-button", |
| 29 | + };`; |
| 30 | + |
| 31 | + expect(bases(source)).toEqual(["h-7", "px-2.5", "rounded-button"]); |
| 32 | + }); |
| 33 | + |
| 34 | + it("reads a class map whose name says nothing about classes", () => { |
| 35 | + // The defect this replaced: candidate-ness was decided by the binding's |
| 36 | + // name, so the same classes were gated in `sizeStyles` and invisible in |
| 37 | + // `buttonSizes`. What makes them classes is where they are used. |
| 38 | + const source = `const fooSizes = { md: "h-ctl rounded-hologram" }; |
| 39 | + const Foo = ({ size }) => <span className={cn(fooSizes[size])} />;`; |
| 40 | + |
| 41 | + expect(bases(source)).toEqual(["h-ctl", "rounded-hologram"]); |
| 42 | + }); |
| 43 | + |
| 44 | + it("follows an identifier that contains a dollar sign", () => { |
| 45 | + const source = `const $cls = "flex-none";\nconst a = <div className={$cls} />;`; |
| 46 | + |
| 47 | + expect(bases(source)).toEqual(["flex-none"]); |
| 48 | + }); |
| 49 | + |
| 50 | + it("does not read a lookup key's default value as a class", () => { |
| 51 | + // `variant` is the key, not the class list, so the default it falls back |
| 52 | + // to is a map entry name and never reaches markup. |
| 53 | + const source = `const map = { ghost: "bg-transparent" }; |
| 54 | + const Foo = ({ variant = "ghost" }) => <span className={map[variant]} />;`; |
| 55 | + |
| 56 | + expect(bases(source)).toEqual(["bg-transparent"]); |
| 57 | + }); |
| 58 | + |
| 59 | + it("leaves a lookup table alone when nothing uses it as a class", () => { |
| 60 | + const source = `const labels = { md: "Medium size" }; |
| 61 | + const Foo = () => <span title={labels.md} />;`; |
| 62 | + |
| 63 | + expect(bases(source)).toEqual([]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("drops the template chunk that touches an interpolation", () => { |
| 67 | + expect(bases("<div className={`w-${i} flex`} />")).toEqual(["flex"]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("keeps a chunk that whitespace separates from the interpolation", () => { |
| 71 | + expect(bases("<div className={`${base} flex `} />")).toEqual(["flex"]); |
| 72 | + }); |
| 73 | + |
| 74 | + it("strips variant prefixes, including bracketed ones", () => { |
| 75 | + const source = `<div className="hover:bg-surface/50 md:group-data-[open]:text-accent" />`; |
| 76 | + |
| 77 | + expect(bases(source)).toEqual(["bg-surface/50", "text-accent"]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("flags bracketed arbitrary values so the sweep can count them", () => { |
| 81 | + const found = extractClassCandidates(`<div className="text-[11px] text-center" />`); |
| 82 | + |
| 83 | + expect(found.filter((candidate) => candidate.arbitrary).map((c) => c.raw)).toEqual([ |
| 84 | + "text-[11px]", |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("ignores strings that are not class lists", () => { |
| 89 | + const source = `const styles = getComputedStyle(el); |
| 90 | + const label = "Export video"; |
| 91 | + log("failed to open the project file");`; |
| 92 | + |
| 93 | + expect(bases(source)).toEqual([]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("does not read past the end of a class region", () => { |
| 97 | + const source = `<div className="flex" title="Export video" />`; |
| 98 | + |
| 99 | + expect(bases(source)).toEqual(["flex"]); |
| 100 | + }); |
| 101 | + |
| 102 | + it("survives an unterminated region without hanging", () => { |
| 103 | + expect(bases(`<div className={cn("flex",`)).toEqual(["flex"]); |
| 104 | + }); |
| 105 | +}); |
0 commit comments