clava 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tests/_utils.ts DELETED
@@ -1,178 +0,0 @@
1
- import { expect } from "vitest";
2
- import { type VariantProps, create, cv as cvBase } from "../src/index.ts";
3
- import type {
4
- AnyComponent,
5
- CVComponent,
6
- ComponentResult,
7
- HTMLObjProps,
8
- HTMLProps,
9
- JSXProps,
10
- StyleClassProps,
11
- StyleProperty,
12
- Variants,
13
- } from "../src/types.ts";
14
- import {
15
- htmlObjStyleToStyleValue,
16
- htmlStyleToStyleValue,
17
- isHTMLObjStyle,
18
- jsxStyleToStyleValue,
19
- } from "../src/utils.ts";
20
-
21
- const MODES = ["jsx", "html", "htmlObj"] as const;
22
- type Mode = (typeof MODES)[number] | null;
23
-
24
- export type HTMLProperties<T extends AnyComponent> = VariantProps<T> & {
25
- id?: string;
26
- class?: string;
27
- className?: string;
28
- style?: StyleProperty;
29
- };
30
-
31
- type ConfigParams = NonNullable<Parameters<typeof create>[0]>;
32
-
33
- interface Config {
34
- mode: Mode;
35
- transformClass?: ConfigParams["transformClass"];
36
- }
37
-
38
- const transformClass = {
39
- uppercase: (className: string) => className.toUpperCase(),
40
- } satisfies Record<string, Config["transformClass"]>;
41
-
42
- export const CONFIGS = {
43
- default: { mode: null },
44
- jsx: { mode: "jsx" },
45
- html: { mode: "html" },
46
- htmlObj: { mode: "htmlObj" },
47
- uppercase: {
48
- mode: null,
49
- transformClass: transformClass.uppercase,
50
- },
51
- } satisfies Record<string, Config>;
52
-
53
- export function getConfigMode<T extends Config>(config: T): T["mode"] {
54
- if (!("mode" in config)) return null;
55
- return config.mode;
56
- }
57
-
58
- export function getConfigTransformClass(config: Config) {
59
- if (!("transformClass" in config) || !config.transformClass) {
60
- return (className: string) => className;
61
- }
62
- return config.transformClass;
63
- }
64
-
65
- export function getConfigDescription(config: Config) {
66
- for (const [name, currentConfig] of Object.entries(CONFIGS)) {
67
- if (currentConfig !== config) continue;
68
- return name;
69
- }
70
- return "custom";
71
- }
72
-
73
- export function createCVFromConfig(
74
- config: Config,
75
- ): ReturnType<typeof create>["cv"] {
76
- const transformClass = getConfigTransformClass(config);
77
- const hasTransform = "transformClass" in config && config.transformClass;
78
- if (!hasTransform) {
79
- return cvBase;
80
- }
81
- const { cv } = create({ transformClass });
82
- return cv;
83
- }
84
-
85
- export function getModeComponent<
86
- M extends Mode,
87
- V extends Variants = {},
88
- const E extends AnyComponent[] = [],
89
- >(mode: M, component: CVComponent<V, E>) {
90
- if (!mode) {
91
- return component;
92
- }
93
- return component[mode];
94
- }
95
-
96
- function getClass(props: ComponentResult) {
97
- if ("class" in props) {
98
- return props.class;
99
- }
100
- return props.className;
101
- }
102
-
103
- export function getClassPropertyName(config: Config) {
104
- const mode = config.mode;
105
- if (mode === "jsx" || mode === null) return "className";
106
- return "class";
107
- }
108
-
109
- export function getExpectedPropsKeys(config: Config, ...variantKeys: string[]) {
110
- if (config.mode === null) {
111
- return ["class", "className", "style", ...variantKeys];
112
- }
113
- return [getClassPropertyName(config), "style", ...variantKeys];
114
- }
115
-
116
- export function assertDefaultProps(
117
- props: ComponentResult,
118
- ): asserts props is StyleClassProps {
119
- if (!("class" in props)) {
120
- expect.fail("Expected default props to have class");
121
- }
122
- if (!("style" in props) || typeof props.style !== "object") {
123
- expect.fail("Expected default props to have style");
124
- }
125
- }
126
-
127
- export function assertJSXProps(
128
- props: ComponentResult,
129
- ): asserts props is JSXProps {
130
- if (!("className" in props)) {
131
- expect.fail("Expected jsx props to have className");
132
- }
133
- if (!("style" in props) || typeof props.style !== "object") {
134
- expect.fail("Expected jsx props to have style");
135
- }
136
- }
137
-
138
- export function assertHTMLProps(
139
- props: ComponentResult,
140
- ): asserts props is HTMLProps {
141
- if (!("class" in props)) {
142
- expect.fail("Expected html props to have class");
143
- }
144
- if (!("style" in props) || typeof props.style !== "string") {
145
- expect.fail("Expected html props to have style");
146
- }
147
- }
148
-
149
- export function assertHTMLObjProps(
150
- props: ComponentResult,
151
- ): asserts props is HTMLObjProps {
152
- if (!("class" in props)) {
153
- expect.fail("Expected htmlObj props to have class");
154
- }
155
- if (!("style" in props) || typeof props.style !== "object") {
156
- expect.fail("Expected htmlObj props to have style");
157
- }
158
- }
159
-
160
- export function getStyle(props: Pick<ComponentResult, "style">) {
161
- if (typeof props.style === "string") {
162
- return htmlStyleToStyleValue(props.style);
163
- }
164
- if (typeof props.style === "object") {
165
- if (isHTMLObjStyle(props.style)) {
166
- return htmlObjStyleToStyleValue(props.style);
167
- }
168
- return jsxStyleToStyleValue(props.style);
169
- }
170
- return {};
171
- }
172
-
173
- export function getStyleClass(props: ComponentResult): Record<string, unknown> {
174
- return {
175
- ...getStyle(props),
176
- class: getClass(props),
177
- };
178
- }
@@ -1,93 +0,0 @@
1
- import { execFile } from "node:child_process";
2
- import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { dirname, join } from "node:path";
5
- import { fileURLToPath, pathToFileURL } from "node:url";
6
- import { promisify } from "node:util";
7
- import { withPackageBuildLock } from "test-utils/build-lock";
8
- import { build } from "vite";
9
- import { expect, test } from "vitest";
10
-
11
- const root = join(dirname(fileURLToPath(import.meta.url)), "..");
12
- const exec = promisify(execFile);
13
- let buildPromise: Promise<unknown> | undefined;
14
-
15
- function buildPackage() {
16
- buildPromise ??= exec("pnpm", ["--dir", root, "build"]);
17
- return buildPromise;
18
- }
19
-
20
- test("build preserves the production warning guard for consumers", async () => {
21
- await withPackageBuildLock(async () => {
22
- await buildPackage();
23
-
24
- const code = await readFile(join(root, "dist/index.js"), "utf8");
25
- expect(code).toMatch(
26
- /warnRefineLimit[\s\S]*?process\.env\.NODE_ENV === "production"[\s\S]*?console\.warn\(/,
27
- );
28
- });
29
- }, 60_000);
30
-
31
- test("vite removes warning logic from the production bundle", async () => {
32
- await withPackageBuildLock(async () => {
33
- await buildPackage();
34
-
35
- const tempDir = await mkdtemp(join(tmpdir(), "clava-vite-"));
36
- try {
37
- const entry = join(tempDir, "entry.js");
38
- const bundle = join(tempDir, "dist/bundle.js");
39
- const clavaUrl = pathToFileURL(join(root, "dist/index.js")).href;
40
-
41
- await writeFile(
42
- entry,
43
- `
44
- import { cv } from ${JSON.stringify(clavaUrl)};
45
-
46
- export const button = cv({
47
- variants: {
48
- tone: {
49
- primary: "primary",
50
- },
51
- },
52
- refine({ setVariants }) {
53
- setVariants({ tone: "primary" });
54
- },
55
- });
56
-
57
- button();
58
- `,
59
- );
60
-
61
- await build({
62
- configFile: false,
63
- logLevel: "silent",
64
- root: tempDir,
65
- mode: "production",
66
- define: {
67
- "process.env.NODE_ENV": JSON.stringify("production"),
68
- },
69
- build: {
70
- emptyOutDir: true,
71
- lib: {
72
- entry,
73
- fileName: () => "bundle.js",
74
- formats: ["es"],
75
- },
76
- minify: true,
77
- outDir: "dist",
78
- },
79
- });
80
-
81
- const code = await readFile(bundle, "utf8");
82
- expect(code).not.toContain("console.warn");
83
- expect(code).not.toContain("Clava: Maximum refine iterations exceeded");
84
- expect(code).not.toContain("Variant(s) that did not stabilize");
85
- expect(code).not.toContain("Latest variant changes before warning");
86
- expect(code).not.toContain("Component created at");
87
- expect(code).not.toContain("captureStackTrace");
88
- expect(code).not.toMatch(/\.warned\b|["']warned["']/);
89
- } finally {
90
- await rm(tempDir, { force: true, recursive: true });
91
- }
92
- });
93
- }, 60_000);
@@ -1,341 +0,0 @@
1
- import { describe, expect, expectTypeOf, test } from "vitest";
2
- import type {
3
- HTMLCSSProperties,
4
- JSXCSSProperties,
5
- StyleClassProps,
6
- StyleValue,
7
- } from "../src/types.ts";
8
- import {
9
- CONFIGS,
10
- assertDefaultProps,
11
- assertHTMLObjProps,
12
- assertHTMLProps,
13
- assertJSXProps,
14
- createCVFromConfig,
15
- getConfigDescription,
16
- getConfigMode,
17
- getConfigTransformClass,
18
- getModeComponent,
19
- getStyleClass,
20
- } from "./_utils.ts";
21
-
22
- for (const config of [CONFIGS.default, CONFIGS.uppercase]) {
23
- const mode = getConfigMode(config);
24
- const cv = createCVFromConfig(config);
25
- const cls = getConfigTransformClass(config);
26
-
27
- describe(getConfigDescription(config), () => {
28
- test("style has correct shape for mode", () => {
29
- const component = getModeComponent(
30
- mode,
31
- cv({ class: "base", style: { backgroundColor: "red" } }),
32
- );
33
- const props = component();
34
- assertDefaultProps(props);
35
- expect(props).not.toHaveProperty("className");
36
- expect(props.class).toBe(cls("base"));
37
- expect(props.style.backgroundColor).toBe("red");
38
- expectTypeOf(props).toEqualTypeOf<StyleClassProps>();
39
- expectTypeOf(props.style).toEqualTypeOf<StyleValue>();
40
- });
41
-
42
- test("no argument still returns normalized shape", () => {
43
- const component = getModeComponent(mode, cv());
44
- const props = component();
45
- assertDefaultProps(props);
46
- expect(props).not.toHaveProperty("className");
47
- expect(props.class).toBe("");
48
- expect(props.style).toEqual({});
49
- expectTypeOf(props).toEqualTypeOf<StyleClassProps>();
50
- expectTypeOf(props.style).toEqualTypeOf<StyleValue>();
51
- });
52
- });
53
- }
54
-
55
- for (const config of [CONFIGS.jsx]) {
56
- const mode = getConfigMode(config);
57
- const cv = createCVFromConfig(config);
58
- const cls = getConfigTransformClass(config);
59
-
60
- describe(getConfigDescription(config), () => {
61
- test("style has correct shape for mode", () => {
62
- const component = getModeComponent(
63
- mode,
64
- cv({ class: "base", style: { backgroundColor: "red" } }),
65
- );
66
- const props = component();
67
- assertJSXProps(props);
68
- expect(props).not.toHaveProperty("class");
69
- expect(props.className).toBe(cls("base"));
70
- expect(props.style.backgroundColor).toBe("red");
71
- expectTypeOf(props.style).toEqualTypeOf<JSXCSSProperties>();
72
- });
73
-
74
- test("no argument still returns jsx shape", () => {
75
- const component = getModeComponent(mode, cv());
76
- const props = component();
77
- assertJSXProps(props);
78
- expect(props).not.toHaveProperty("class");
79
- expect(props.className).toBe("");
80
- expect(props.style).toEqual({});
81
- expectTypeOf(props.style).toEqualTypeOf<JSXCSSProperties>();
82
- });
83
- });
84
- }
85
-
86
- for (const config of [CONFIGS.html]) {
87
- const mode = getConfigMode(config);
88
- const cv = createCVFromConfig(config);
89
- const cls = getConfigTransformClass(config);
90
-
91
- describe(getConfigDescription(config), () => {
92
- test("style has correct shape for mode", () => {
93
- const component = getModeComponent(
94
- mode,
95
- cv({ class: "base", style: { backgroundColor: "red" } }),
96
- );
97
- const props = component();
98
- assertHTMLProps(props);
99
- expect(props).not.toHaveProperty("className");
100
- expect(props.class).toBe(cls("base"));
101
- expect(props.style).toBe("background-color: red;");
102
- });
103
-
104
- test("no argument still returns html shape", () => {
105
- const component = getModeComponent(mode, cv());
106
- const props = component();
107
- assertHTMLProps(props);
108
- expect(props).not.toHaveProperty("className");
109
- expect(props.class).toBe("");
110
- expect(props.style).toBe("");
111
- });
112
- });
113
- }
114
-
115
- for (const config of [CONFIGS.htmlObj]) {
116
- const mode = getConfigMode(config);
117
- const cv = createCVFromConfig(config);
118
- const cls = getConfigTransformClass(config);
119
-
120
- describe(getConfigDescription(config), () => {
121
- test("style has correct shape for mode", () => {
122
- const component = getModeComponent(
123
- mode,
124
- cv({ class: "base", style: { backgroundColor: "red" } }),
125
- );
126
- const props = component();
127
- assertHTMLObjProps(props);
128
- expect(props).not.toHaveProperty("className");
129
- expect(props.class).toBe(cls("base"));
130
- expect(props.style["background-color"]).toBe("red");
131
- expectTypeOf(props.style).toEqualTypeOf<HTMLCSSProperties>();
132
- });
133
-
134
- test("no argument still returns htmlObj shape", () => {
135
- const component = getModeComponent(mode, cv());
136
- const props = component();
137
- assertHTMLObjProps(props);
138
- expect(props).not.toHaveProperty("className");
139
- expect(props.class).toBe("");
140
- expect(props.style).toEqual({});
141
- expectTypeOf(props.style).toEqualTypeOf<HTMLCSSProperties>();
142
- });
143
- });
144
- }
145
-
146
- for (const config of Object.values(CONFIGS)) {
147
- const mode = getConfigMode(config);
148
- const cv = createCVFromConfig(config);
149
- const cls = getConfigTransformClass(config);
150
-
151
- describe(getConfigDescription(config), () => {
152
- test("no argument", () => {
153
- const component = getModeComponent(mode, cv());
154
- const props = component();
155
- expect(getStyleClass(props)).toEqual({ class: "" });
156
- });
157
-
158
- test("null class", () => {
159
- const component = getModeComponent(mode, cv({ class: null }));
160
- const props = component();
161
- expect(getStyleClass(props)).toEqual({ class: "" });
162
- });
163
-
164
- test("empty array class", () => {
165
- const component = getModeComponent(mode, cv({ class: [] }));
166
- const props = component();
167
- expect(getStyleClass(props)).toEqual({ class: "" });
168
- });
169
-
170
- test("string class", () => {
171
- const component = getModeComponent(mode, cv({ class: "foo bar" }));
172
- const props = component();
173
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar") });
174
- });
175
-
176
- test("nested array class", () => {
177
- const component = getModeComponent(
178
- mode,
179
- cv({ class: ["foo", ["bar", ["baz"]]] }),
180
- );
181
- const props = component();
182
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar baz") });
183
- });
184
-
185
- test("nested array class with falsy values", () => {
186
- const component = getModeComponent(
187
- mode,
188
- cv({ class: ["foo", null, ["bar", false, ["baz", 0]]] }),
189
- );
190
- const props = component();
191
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar baz") });
192
- });
193
-
194
- test("merge class from props", () => {
195
- const component = getModeComponent(mode, cv({ class: "foo bar" }));
196
- const props = component({ class: "baz qux" });
197
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar baz qux") });
198
- });
199
-
200
- test("merge className from props", () => {
201
- const component = getModeComponent(mode, cv({ class: "foo bar" }));
202
- const props = component({ className: "baz qux" });
203
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar baz qux") });
204
- });
205
-
206
- test("merge class and className from props", () => {
207
- const component = getModeComponent(mode, cv({ class: "foo bar" }));
208
- const props = component({ class: "baz qux", className: "quux corge" });
209
- expect(getStyleClass(props)).toEqual({
210
- class: cls("foo bar baz qux quux corge"),
211
- });
212
- });
213
-
214
- test("merge null class from props", () => {
215
- const component = getModeComponent(mode, cv({ class: "foo bar" }));
216
- const props = component({ class: null });
217
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar") });
218
- });
219
-
220
- test("merge null className from props", () => {
221
- const component = getModeComponent(mode, cv({ class: "foo bar" }));
222
- const props = component({ className: null });
223
- expect(getStyleClass(props)).toEqual({ class: cls("foo bar") });
224
- });
225
-
226
- test("empty style", () => {
227
- const component = getModeComponent(mode, cv({ style: {} }));
228
- const props = component();
229
- expect(getStyleClass(props)).toEqual({ class: "" });
230
- });
231
-
232
- test("style with properties", () => {
233
- const component = getModeComponent(
234
- mode,
235
- cv({ style: { backgroundColor: "red", fontSize: "16px" } }),
236
- );
237
- const props = component();
238
- expect(getStyleClass(props)).toEqual({
239
- class: "",
240
- backgroundColor: "red",
241
- fontSize: "16px",
242
- });
243
- });
244
-
245
- test("style does not accept numbers", () => {
246
- const component = getModeComponent(
247
- mode,
248
- cv({
249
- style: {
250
- backgroundColor: "red",
251
- // @ts-expect-error
252
- fontSize: 16,
253
- },
254
- }),
255
- );
256
- const props = component();
257
- expect(getStyleClass(props)).toEqual({
258
- class: "",
259
- backgroundColor: "red",
260
- fontSize: expect.toBeOneOf(["16", "16px"]),
261
- });
262
- });
263
-
264
- test("style with custom property", () => {
265
- const component = getModeComponent(
266
- mode,
267
- cv({ style: { backgroundColor: "red", "--custom-var": "value" } }),
268
- );
269
- const props = component();
270
- expect(getStyleClass(props)).toEqual({
271
- class: "",
272
- backgroundColor: "red",
273
- "--custom-var": "value",
274
- });
275
- });
276
-
277
- test("merge style from props", () => {
278
- const component = getModeComponent(
279
- mode,
280
- cv({ style: { backgroundColor: "red" } }),
281
- );
282
- const props = component({ style: { fontSize: "16px" } });
283
- expect(getStyleClass(props)).toEqual({
284
- class: "",
285
- backgroundColor: "red",
286
- fontSize: "16px",
287
- });
288
- });
289
-
290
- test("merge jsx style from props", () => {
291
- const component = getModeComponent(
292
- mode,
293
- cv({ style: { backgroundColor: "red" } }),
294
- );
295
- const props = component({ style: { fontSize: 16 } });
296
- expect(getStyleClass(props)).toEqual({
297
- class: "",
298
- backgroundColor: "red",
299
- fontSize: "16px",
300
- });
301
- });
302
-
303
- test("merge html style from props", () => {
304
- const component = getModeComponent(
305
- mode,
306
- cv({ style: { backgroundColor: "red" } }),
307
- );
308
- const props = component({ style: "font-size: 16px" });
309
- expect(getStyleClass(props)).toEqual({
310
- class: "",
311
- backgroundColor: "red",
312
- fontSize: "16px",
313
- });
314
- });
315
-
316
- test("merge htmlObj style from props", () => {
317
- const component = getModeComponent(
318
- mode,
319
- cv({ style: { backgroundColor: "red" } }),
320
- );
321
- const props = component({ style: { "font-size": 16 } });
322
- expect(getStyleClass(props)).toEqual({
323
- class: "",
324
- backgroundColor: "red",
325
- fontSize: "16px",
326
- });
327
- });
328
-
329
- test("merge null style from props", () => {
330
- const component = getModeComponent(
331
- mode,
332
- cv({ style: { backgroundColor: "red" } }),
333
- );
334
- const props = component({ style: null });
335
- expect(getStyleClass(props)).toEqual({
336
- class: "",
337
- backgroundColor: "red",
338
- });
339
- });
340
- });
341
- }