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.
@@ -1,421 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { create } from "../src/index.ts";
3
- import {
4
- CONFIGS,
5
- createCVFromConfig,
6
- getConfigDescription,
7
- getConfigMode,
8
- getConfigTransformClass,
9
- getModeComponent,
10
- getStyleClass,
11
- } from "./_utils.ts";
12
-
13
- for (const config of Object.values(CONFIGS)) {
14
- const mode = getConfigMode(config);
15
- const cv = createCVFromConfig(config);
16
- const cls = getConfigTransformClass(config);
17
-
18
- describe(getConfigDescription(config), () => {
19
- test("extend single component", () => {
20
- const base = cv({ class: "base", variants: { size: { sm: "sm" } } });
21
- const component = getModeComponent(
22
- mode,
23
- cv({ extend: [base], class: "extended" }),
24
- );
25
- const props = component({ size: "sm" });
26
- expect(getStyleClass(props)).toEqual({ class: cls("base extended sm") });
27
- });
28
-
29
- test("extend multiple components", () => {
30
- const base1 = cv({ class: "base1" });
31
- const base2 = cv({ class: "base2" });
32
- const component = getModeComponent(
33
- mode,
34
- cv({ extend: [base1, base2], class: "extended" }),
35
- );
36
- const props = component();
37
- expect(getStyleClass(props)).toEqual({
38
- class: cls("base1 base2 extended"),
39
- });
40
- });
41
-
42
- test("extend with variant merging", () => {
43
- const base = cv({ variants: { size: { sm: "base-sm", lg: "base-lg" } } });
44
- const component = getModeComponent(
45
- mode,
46
- cv({ extend: [base], variants: { size: { sm: "extended-sm" } } }),
47
- );
48
- const props = component({ size: "sm" });
49
- expect(getStyleClass(props)).toEqual({
50
- class: cls("base-sm extended-sm"),
51
- });
52
- });
53
-
54
- test("extend with variant merging setting base variant", () => {
55
- const base = cv({ variants: { size: { sm: "base-sm", lg: "base-lg" } } });
56
- const component = getModeComponent(
57
- mode,
58
- cv({
59
- extend: [base],
60
- variants: { size: { sm: "extended-sm" } },
61
- }),
62
- );
63
- const props = component({ size: "lg" });
64
- expect(getStyleClass(props)).toEqual({ class: cls("base-lg") });
65
- });
66
-
67
- test("extend can disable whole variant with null", () => {
68
- const base = cv({
69
- variants: { size: { sm: "base-sm", lg: "base-lg" } },
70
- defaultVariants: { size: "sm" },
71
- });
72
- const component = getModeComponent(
73
- mode,
74
- cv({
75
- extend: [base],
76
- variants: { size: null },
77
- defaultVariants: {
78
- // @ts-expect-error disabled variant cannot be set
79
- size:
80
- // no error
81
- "lg",
82
- },
83
- }),
84
- );
85
- const props = component({
86
- // @ts-expect-error disabled variant cannot be set
87
- size:
88
- // no error
89
- "lg",
90
- });
91
- expect(getStyleClass(props)).toEqual({ class: "" });
92
- });
93
-
94
- test("extend can disable variant value with null", () => {
95
- const base = cv({
96
- variants: { size: { sm: "base-sm", lg: "base-lg" } },
97
- defaultVariants: { size: "sm" },
98
- });
99
- const component = getModeComponent(
100
- mode,
101
- cv({
102
- extend: [base],
103
- variants: { size: { sm: null } },
104
- defaultVariants: {
105
- // @ts-expect-error disabled variant value cannot be set
106
- size:
107
- // no error
108
- "sm",
109
- },
110
- }),
111
- );
112
- const disabledProps = component({
113
- // @ts-expect-error disabled variant value cannot be set
114
- size:
115
- // no error
116
- "sm",
117
- });
118
- expect(getStyleClass(disabledProps)).toEqual({ class: "" });
119
- const enabledProps = component({ size: "lg" });
120
- expect(getStyleClass(enabledProps)).toEqual({ class: cls("base-lg") });
121
- });
122
-
123
- test("extend disabled variant value accepts valid defaultVariants", () => {
124
- const base = cv({
125
- variants: {
126
- size: {
127
- sm: { class: "base-sm", style: { fontSize: "12px" } },
128
- lg: { class: "base-lg", style: { fontSize: "16px" } },
129
- },
130
- },
131
- });
132
- const component = getModeComponent(
133
- mode,
134
- cv({
135
- extend: [base],
136
- variants: { size: { sm: null } },
137
- defaultVariants: { size: "lg" },
138
- }),
139
- );
140
- const props = component();
141
- expect(getStyleClass(props)).toEqual({
142
- class: cls("base-lg"),
143
- fontSize: "16px",
144
- });
145
- });
146
-
147
- test("extend disabled variant value with computed defaultVariants", () => {
148
- const base = cv({
149
- variants: {
150
- size: {
151
- sm: { class: "base-sm", style: { fontSize: "12px" } },
152
- lg: { class: "base-lg", style: { fontSize: "16px" } },
153
- },
154
- },
155
- });
156
- const validComponent = getModeComponent(
157
- mode,
158
- cv({
159
- extend: [base],
160
- variants: { size: { sm: null } },
161
- defaultVariants: {
162
- size: () => "lg" as const,
163
- },
164
- }),
165
- );
166
- expect(getStyleClass(validComponent())).toEqual({
167
- class: cls("base-lg"),
168
- fontSize: "16px",
169
- });
170
-
171
- const invalidComponent = getModeComponent(
172
- mode,
173
- cv({
174
- extend: [base],
175
- variants: { size: { sm: null } },
176
- defaultVariants: {
177
- // @ts-expect-error disabled variant value cannot be set
178
- size: () =>
179
- // no error
180
- "sm",
181
- },
182
- }),
183
- );
184
- expect(getStyleClass(invalidComponent())).toEqual({ class: "" });
185
- });
186
-
187
- test("extend filters disabled values from inherited computed defaultVariants", () => {
188
- const base = cv({
189
- variants: {
190
- size: {
191
- sm: { class: "base-sm", style: { fontSize: "12px" } },
192
- lg: { class: "base-lg", style: { fontSize: "16px" } },
193
- },
194
- },
195
- defaultVariants: {
196
- size: () => "sm" as const,
197
- },
198
- });
199
- const component = getModeComponent(
200
- mode,
201
- cv({
202
- extend: [base],
203
- variants: {
204
- size: { sm: null },
205
- color: { red: "red", blue: "blue" },
206
- },
207
- defaultVariants: {
208
- size: "lg",
209
- color: ({ variants }) => (variants.size === "lg" ? "blue" : "red"),
210
- },
211
- }),
212
- );
213
- expect(getStyleClass(component())).toEqual({
214
- class: cls("base-lg blue"),
215
- fontSize: "16px",
216
- });
217
- expect(component.getVariants()).toEqual({
218
- size: "lg",
219
- color: "blue",
220
- });
221
- });
222
-
223
- test("extend disabled variant value with refine setVariants", () => {
224
- const base = cv({
225
- variants: {
226
- size: {
227
- sm: { class: "base-sm", style: { fontSize: "12px" } },
228
- lg: { class: "base-lg", style: { fontSize: "16px" } },
229
- },
230
- },
231
- });
232
- const validComponent = getModeComponent(
233
- mode,
234
- cv({
235
- extend: [base],
236
- variants: { size: { sm: null } },
237
- refine: ({ setVariants }) => {
238
- setVariants({ size: "lg" });
239
- },
240
- }),
241
- );
242
- expect(getStyleClass(validComponent())).toEqual({
243
- class: cls("base-lg"),
244
- fontSize: "16px",
245
- });
246
-
247
- const invalidComponent = getModeComponent(
248
- mode,
249
- cv({
250
- extend: [base],
251
- variants: { size: { sm: null } },
252
- refine: ({ setVariants }) => {
253
- setVariants({
254
- // @ts-expect-error disabled variant value cannot be set
255
- size:
256
- // no error
257
- "sm",
258
- });
259
- },
260
- }),
261
- );
262
- expect(getStyleClass(invalidComponent())).toEqual({ class: "" });
263
- });
264
-
265
- test("extend inherits defaultVariants", () => {
266
- const base = cv({
267
- variants: { size: { sm: "sm", lg: "lg" } },
268
- defaultVariants: { size: "sm" },
269
- });
270
- const component = getModeComponent(mode, cv({ extend: [base] }));
271
- const props = component();
272
- expect(getStyleClass(props)).toEqual({ class: cls("sm") });
273
- });
274
-
275
- test("extend override defaultVariants", () => {
276
- const base = cv({
277
- variants: { size: { sm: "sm", lg: "lg" } },
278
- defaultVariants: { size: "sm" },
279
- });
280
- const component = getModeComponent(
281
- mode,
282
- cv({ extend: [base], defaultVariants: { size: "lg" } }),
283
- );
284
- const props = component();
285
- expect(getStyleClass(props)).toEqual({ class: cls("lg") });
286
- });
287
-
288
- test("extend jsx modal component preserves style", () => {
289
- const base = cv({
290
- class: "base",
291
- style: { backgroundColor: "red" },
292
- });
293
- const component = getModeComponent(
294
- mode,
295
- cv({ extend: [base.jsx], class: "extended" }),
296
- );
297
- const props = component();
298
- expect(getStyleClass(props)).toEqual({
299
- class: cls("base extended"),
300
- backgroundColor: "red",
301
- });
302
- });
303
-
304
- test("extend html modal component preserves style", () => {
305
- const base = cv({
306
- class: "base",
307
- style: { backgroundColor: "red" },
308
- });
309
- const component = getModeComponent(
310
- mode,
311
- cv({ extend: [base.html], class: "extended" }),
312
- );
313
- const props = component();
314
- expect(getStyleClass(props)).toEqual({
315
- class: cls("base extended"),
316
- backgroundColor: "red",
317
- });
318
- });
319
-
320
- test("extend htmlObj modal component preserves style", () => {
321
- const base = cv({
322
- class: "base",
323
- style: { backgroundColor: "red" },
324
- });
325
- const component = getModeComponent(
326
- mode,
327
- cv({ extend: [base.htmlObj], class: "extended" }),
328
- );
329
- const props = component();
330
- expect(getStyleClass(props)).toEqual({
331
- class: cls("base extended"),
332
- backgroundColor: "red",
333
- });
334
- });
335
- });
336
- }
337
-
338
- describe("non-idempotent transformClass", () => {
339
- // A non-idempotent transform: prefixing each word with `tw-`. Applying it
340
- // twice yields `tw-tw-foo`, so the engine must invoke it exactly once per
341
- // class word — even when extend chains pipe extended base classes back into
342
- // a parent's `clsx` and through the same transform at render time.
343
- const { cv } = create({
344
- transformClass: (className) =>
345
- className
346
- .split(" ")
347
- .filter(Boolean)
348
- .map((word) => `tw-${word}`)
349
- .join(" "),
350
- });
351
-
352
- test("base class is transformed exactly once across single extend", () => {
353
- const base = cv({ class: "base" });
354
- const component = cv({ extend: [base], class: "extended" });
355
- expect(component().class).toBe("tw-base tw-extended");
356
- });
357
-
358
- test("base class is transformed exactly once across multi-level extend", () => {
359
- const base = cv({ class: "base" });
360
- const middle = cv({ extend: [base], class: "middle" });
361
- const top = cv({
362
- extend: [middle],
363
- class: "top",
364
- variants: { size: { sm: "sm" } },
365
- });
366
- expect(top({ size: "sm" }).class).toBe("tw-base tw-middle tw-top tw-sm");
367
- });
368
- });
369
-
370
- function toUpperCase(className: string) {
371
- return className.toUpperCase();
372
- }
373
-
374
- function toLowerCase(className: string) {
375
- return className.toLowerCase();
376
- }
377
-
378
- describe("extend across `create()` factories", () => {
379
- // The extend's own `transformClass` must apply to its own classes even when
380
- // the extending component comes from a different `create()` call. The
381
- // optimized compute path bypasses the public-component round-trip, so it
382
- // detects mixed-factory extends by reference identity and runs the extend's
383
- // transform on its contribution before joining.
384
- const { cv: cvUpper } = create({ transformClass: toUpperCase });
385
- const { cv: cvDefault } = create();
386
-
387
- test("extend's transformClass applies to its base class", () => {
388
- const base = cvUpper({ class: "base" });
389
- const component = cvDefault({ extend: [base], class: "extended" });
390
- expect(component().class).toBe("BASE extended");
391
- });
392
-
393
- test("extend's transformClass applies to its variant classes", () => {
394
- const base = cvUpper({
395
- class: "base",
396
- variants: { size: { sm: "sm", lg: "lg" } },
397
- });
398
- const component = cvDefault({ extend: [base], class: "extended" });
399
- expect(component({ size: "sm" }).class).toBe("BASE extended SM");
400
- });
401
-
402
- test("extend's transformClass cascades through grandparent chain", () => {
403
- const grandparent = cvUpper({ class: "grandparent" });
404
- const parent = cvUpper({ extend: [grandparent], class: "parent" });
405
- const component = cvDefault({ extend: [parent], class: "child" });
406
- expect(component().class).toBe("GRANDPARENT PARENT child");
407
- });
408
-
409
- test("parent's transformClass applies on top of extend's transformed output", () => {
410
- const { cv: cvLower } = create({ transformClass: toLowerCase });
411
- const base = cvUpper({
412
- class: "base",
413
- variants: { size: { sm: "sm" } },
414
- });
415
- const component = cvLower({ extend: [base], class: "child" });
416
- // The extend uppercases its own contribution, then the parent's
417
- // transformClass runs on the joined string and lowercases everything —
418
- // mirrors main's `parentTransform(clsx(extTransform(extOutput), …))`.
419
- expect(component({ size: "sm" }).class).toBe("base child sm");
420
- });
421
- });