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,548 +0,0 @@
1
- import { describe, expect, expectTypeOf, test } from "vitest";
2
- import { type Variant, cv as cvBase } from "../src/index.ts";
3
- import {
4
- CONFIGS,
5
- createCVFromConfig,
6
- getConfigDescription,
7
- getConfigMode,
8
- getConfigTransformClass,
9
- getExpectedPropsKeys,
10
- getModeComponent,
11
- getStyle,
12
- } from "./_utils.ts";
13
-
14
- for (const config of Object.values(CONFIGS)) {
15
- const mode = getConfigMode(config);
16
- const cv = createCVFromConfig(config);
17
- const cls = getConfigTransformClass(config);
18
-
19
- describe(getConfigDescription(config), () => {
20
- test("class method", () => {
21
- const component = getModeComponent(
22
- mode,
23
- cv({ class: "foo", variants: { size: { sm: "sm", lg: "lg" } } }),
24
- );
25
- const className = component.class({ size: "lg" });
26
- expect(className).toBe(cls("foo lg"));
27
- });
28
-
29
- test("style method", () => {
30
- const component = getModeComponent(
31
- mode,
32
- cv({ style: { backgroundColor: "red" } }),
33
- );
34
- const style = component.style();
35
- expect(getStyle({ style })).toEqual({ backgroundColor: "red" });
36
- });
37
-
38
- test("getVariants returns variant values", () => {
39
- const component = getModeComponent(
40
- mode,
41
- cv({
42
- variants: { size: { sm: "sm", lg: "lg" } },
43
- defaultVariants: { size: "sm" },
44
- }),
45
- );
46
- const variants = component.getVariants({ size: "lg" });
47
- expect(variants).toEqual({ size: "lg" });
48
- });
49
-
50
- test("getVariants returns default variants", () => {
51
- const component = getModeComponent(
52
- mode,
53
- cv({
54
- variants: { size: { sm: "sm", lg: "lg" } },
55
- defaultVariants: { size: "sm" },
56
- }),
57
- );
58
- const variants = component.getVariants();
59
- expect(variants).toEqual({ size: "sm" });
60
- });
61
-
62
- test("getVariants omits undefined defaultVariants", () => {
63
- const component = getModeComponent(
64
- mode,
65
- cv({
66
- variants: { size: { sm: "sm", lg: "lg" } },
67
- defaultVariants: { size: undefined },
68
- }),
69
- );
70
- const variants = component.getVariants();
71
- expect(variants).toStrictEqual({});
72
- expect(Object.hasOwn(variants, "size")).toBe(false);
73
- });
74
-
75
- test("getVariants undefined defaultVariants clear inherited defaultVariants", () => {
76
- const base = cv({
77
- variants: { size: { sm: "sm", lg: "lg" } },
78
- defaultVariants: { size: "sm" },
79
- });
80
- const component = getModeComponent(
81
- mode,
82
- cv({ extend: [base], defaultVariants: { size: undefined } }),
83
- );
84
- const variants = component.getVariants();
85
- expect(variants).toStrictEqual({});
86
- expect(Object.hasOwn(variants, "size")).toBe(false);
87
- });
88
-
89
- test("getVariants returns variants set by refine setVariants", () => {
90
- const component = getModeComponent(
91
- mode,
92
- cv({
93
- variants: {
94
- size: { sm: "sm", lg: "lg" },
95
- color: { red: "red", blue: "blue" },
96
- },
97
- refine: ({ variants, setVariants }) => {
98
- if (variants.size === "lg") {
99
- setVariants({ color: "red" });
100
- }
101
- },
102
- }),
103
- );
104
- const variants = component.getVariants({ size: "lg" });
105
- expect(variants).toEqual({ size: "lg", color: "red" });
106
- });
107
-
108
- test("getVariants re-runs when refine changes variants", () => {
109
- const component = getModeComponent(
110
- mode,
111
- cv({
112
- variants: {
113
- size: { sm: "sm", lg: "lg" },
114
- color: { red: "red", blue: "blue" },
115
- },
116
- refine: ({ variants, setVariants }) => {
117
- if (variants.size === "lg") {
118
- setVariants({ color: "red" });
119
- }
120
- if (variants.color === "red") {
121
- setVariants({ size: "sm" });
122
- }
123
- },
124
- }),
125
- );
126
- const variants = component.getVariants({ size: "lg" });
127
- expect(variants).toEqual({ size: "sm", color: "red" });
128
- });
129
-
130
- test("getVariants re-runs when computed defaultVariants change variants", () => {
131
- const component = getModeComponent(
132
- mode,
133
- cv({
134
- variants: {
135
- size: { sm: "sm", lg: "lg" },
136
- color: { red: "red", blue: "blue" },
137
- },
138
- defaultVariants: {
139
- color: () => "red" as const,
140
- size: ({ defaultValue, variants }) =>
141
- variants.color === "red" ? "lg" : defaultValue,
142
- },
143
- }),
144
- );
145
- const variants = component.getVariants();
146
- expect(variants).toEqual({ size: "lg", color: "red" });
147
- });
148
-
149
- test("getVariants returns computed defaultVariants", () => {
150
- const component = getModeComponent(
151
- mode,
152
- cv({
153
- variants: {
154
- size: { sm: "sm", lg: "lg" },
155
- color: { red: "red", blue: "blue" },
156
- },
157
- defaultVariants: {
158
- color: ({ defaultValue, variants }) =>
159
- variants.size === "lg" ? "blue" : defaultValue,
160
- },
161
- }),
162
- );
163
- const variants = component.getVariants({ size: "lg" });
164
- expect(variants).toEqual({ size: "lg", color: "blue" });
165
- });
166
-
167
- test("getVariants computed defaultVariants do not override props", () => {
168
- const component = getModeComponent(
169
- mode,
170
- cv({
171
- variants: {
172
- size: { sm: "sm", lg: "lg" },
173
- color: { red: "red", blue: "blue" },
174
- },
175
- defaultVariants: {
176
- color: () => "blue" as const,
177
- },
178
- }),
179
- );
180
- const variants = component.getVariants({ color: "red" });
181
- expect(variants).toEqual({ color: "red" });
182
- });
183
-
184
- test("getVariants setVariants overrides props", () => {
185
- const component = getModeComponent(
186
- mode,
187
- cv({
188
- variants: {
189
- size: { sm: "sm", lg: "lg" },
190
- color: { red: "red", blue: "blue" },
191
- },
192
- refine: ({ setVariants }) => {
193
- setVariants({ color: "blue" });
194
- },
195
- }),
196
- );
197
- const variants = component.getVariants({ color: "red" });
198
- expect(variants).toEqual({ color: "blue" });
199
- });
200
-
201
- test("getVariants picks up computed defaultVariants from extended component", () => {
202
- const base = cv({
203
- variants: { size: { sm: "sm", lg: "lg" } },
204
- defaultVariants: {
205
- size: () => "lg" as const,
206
- },
207
- });
208
- const component = getModeComponent(
209
- mode,
210
- cv({
211
- extend: [base],
212
- variants: { color: { red: "red", blue: "blue" } },
213
- defaultVariants: { color: "red" },
214
- }),
215
- );
216
- const variants = component.getVariants();
217
- expect(variants).toEqual({ size: "lg", color: "red" });
218
- });
219
-
220
- test("getVariants picks up computed defaultVariants from grandparent component", () => {
221
- const grandparent = cv({
222
- variants: { size: { sm: "sm", lg: "lg" } },
223
- defaultVariants: {
224
- size: () => "lg" as const,
225
- },
226
- });
227
- const parent = cv({ extend: [grandparent] });
228
- const component = getModeComponent(
229
- mode,
230
- cv({
231
- extend: [parent],
232
- variants: { color: { red: "red", blue: "blue" } },
233
- defaultVariants: { color: "red" },
234
- }),
235
- );
236
- const variants = component.getVariants();
237
- expect(variants).toEqual({ size: "lg", color: "red" });
238
- });
239
-
240
- test("getVariants re-runs when base component refine changes variants", () => {
241
- const base = cv({
242
- variants: { size: { sm: "sm", lg: "lg" }, active: "" },
243
- defaultVariants: { size: "sm" },
244
- refine: ({ variants, setVariants }) => {
245
- if (variants.active) {
246
- setVariants({ size: "lg" });
247
- }
248
- },
249
- });
250
- const component = getModeComponent(
251
- mode,
252
- cv({
253
- extend: [base],
254
- variants: { color: { red: "red", blue: "blue" } },
255
- refine: ({ variants, setVariants }) => {
256
- if (variants.size === "lg") {
257
- setVariants({ color: "red" });
258
- }
259
- },
260
- }),
261
- );
262
- const variants = component.getVariants({ active: true });
263
- expect(variants).toEqual({ size: "lg", active: true, color: "red" });
264
- });
265
-
266
- test("getVariants preserves computed defaultVariants after a setVariants re-run", () => {
267
- const base = cv({
268
- variants: {
269
- size: { sm: "sm", lg: "lg" },
270
- active: "",
271
- mode: { on: "on" },
272
- },
273
- defaultVariants: {
274
- size: ({ defaultValue, variants }) =>
275
- variants.mode === "on" ? "lg" : defaultValue,
276
- },
277
- refine: ({ variants, setVariants }) => {
278
- if (variants.active) {
279
- setVariants({ mode: "on" });
280
- }
281
- },
282
- });
283
- const component = getModeComponent(mode, cv({ extend: [base] }));
284
- const variants = component.getVariants({ active: true });
285
- expect(variants).toEqual({ size: "lg", active: true, mode: "on" });
286
- });
287
-
288
- test("getVariants setVariants uses the latest pending value", () => {
289
- const component = getModeComponent(
290
- mode,
291
- cv({
292
- variants: { size: { sm: "sm", lg: "lg" } },
293
- defaultVariants: { size: "sm" },
294
- refine: ({ setVariants }) => {
295
- setVariants({ size: "lg" });
296
- setVariants({ size: "sm" });
297
- },
298
- }),
299
- );
300
- const variants = component.getVariants();
301
- expect(variants).toEqual({ size: "sm" });
302
- });
303
-
304
- test("getVariants child setVariants keeps overriding base computed defaultVariants across re-runs", () => {
305
- const base = cv({
306
- variants: { color: { red: "red", blue: "blue" } },
307
- defaultVariants: {
308
- color: () => "blue" as const,
309
- },
310
- });
311
- const component = getModeComponent(
312
- mode,
313
- cv({
314
- extend: [base],
315
- variants: { size: { sm: "sm", lg: "lg" } },
316
- defaultVariants: { size: "sm" },
317
- refine: ({ variants, setVariants }) => {
318
- if (variants.size === "sm") {
319
- setVariants({ color: "red" });
320
- }
321
- },
322
- }),
323
- );
324
- const variants = component.getVariants();
325
- expect(variants).toEqual({ color: "red", size: "sm" });
326
- });
327
-
328
- test("getVariants setVariants sticks across computed default re-runs", () => {
329
- const base = cv({
330
- variants: { color: { red: "red", blue: "blue" } },
331
- defaultVariants: {
332
- color: () => "blue" as const,
333
- },
334
- });
335
- const component = getModeComponent(
336
- mode,
337
- cv({
338
- extend: [base],
339
- variants: { color: { red: "red", blue: "blue" }, done: "" },
340
- refine: ({ variants, setVariants }) => {
341
- if (!variants.done) {
342
- setVariants({ color: "red", done: true });
343
- }
344
- },
345
- }),
346
- );
347
- const variants = component.getVariants();
348
- expect(variants).toEqual({ color: "red", done: true });
349
- });
350
-
351
- test("getVariants base computed defaultVariants can override child static defaults after a re-run", () => {
352
- const base = cv({
353
- variants: {
354
- size: { sm: "sm", lg: "lg" },
355
- active: "",
356
- mode: { on: "on" },
357
- },
358
- defaultVariants: {
359
- size: ({ defaultValue, variants }) =>
360
- variants.mode === "on" ? "lg" : defaultValue,
361
- },
362
- refine: ({ variants, setVariants }) => {
363
- if (variants.active) {
364
- setVariants({ mode: "on" });
365
- }
366
- },
367
- });
368
- const component = getModeComponent(
369
- mode,
370
- cv({ extend: [base], defaultVariants: { size: "sm" } }),
371
- );
372
- const variants = component.getVariants({ active: true });
373
- expect(variants).toEqual({ size: "lg", active: true, mode: "on" });
374
- });
375
-
376
- test("getVariants setVariants from earlier extends overrides computed defaultVariants from later extends", () => {
377
- const first = cv({
378
- variants: { color: { red: "first-red", blue: "first-blue" } },
379
- refine: ({ setVariants }) => {
380
- setVariants({ color: "red" });
381
- },
382
- });
383
- const second = cv({
384
- variants: { color: { red: "second-red", blue: "second-blue" } },
385
- defaultVariants: {
386
- color: () => "blue" as const,
387
- },
388
- });
389
- const component = getModeComponent(mode, cv({ extend: [first, second] }));
390
- const variants = component.getVariants();
391
- expect(variants).toEqual({ color: "red" });
392
- });
393
-
394
- test("getVariants computed defaultVariants from later extends override earlier extends", () => {
395
- const first = cv({
396
- variants: { color: { red: "first-red", blue: "first-blue" } },
397
- defaultVariants: {
398
- color: () => "red" as const,
399
- },
400
- });
401
- const second = cv({
402
- variants: { color: { red: "second-red", blue: "second-blue" } },
403
- defaultVariants: {
404
- color: () => "blue" as const,
405
- },
406
- });
407
- const component = getModeComponent(mode, cv({ extend: [first, second] }));
408
- const variants = component.getVariants();
409
- expect(variants).toEqual({ color: "blue" });
410
- });
411
-
412
- test("getVariants computed defaultVariants do not override stable setVariants on later passes", () => {
413
- const base = cv({
414
- variants: { color: { red: "base-red", blue: "base-blue" } },
415
- refine: ({ setVariants }) => {
416
- setVariants({ color: "red" });
417
- },
418
- });
419
- const component = getModeComponent(
420
- mode,
421
- cv({
422
- extend: [base],
423
- variants: { color: { red: "child-red", blue: "child-blue" } },
424
- defaultVariants: {
425
- color: ({ defaultValue, variants }) =>
426
- variants.color === "red" ? "blue" : defaultValue,
427
- },
428
- }),
429
- );
430
- const variants = component.getVariants();
431
- expect(variants).toEqual({ color: "red" });
432
- });
433
-
434
- test("getVariants computed defaultVariants do not override setVariants from a previous pass", () => {
435
- const component = getModeComponent(
436
- mode,
437
- cv({
438
- variants: {
439
- color: { red: "red", blue: "blue" },
440
- done: "",
441
- },
442
- defaultVariants: {
443
- color: ({ defaultValue, variants }) =>
444
- variants.done ? "blue" : defaultValue,
445
- },
446
- refine: ({ variants, setVariants }) => {
447
- if (!variants.done) {
448
- setVariants({ color: "red", done: true });
449
- }
450
- },
451
- }),
452
- );
453
- const variants = component.getVariants();
454
- expect(variants).toEqual({ color: "red", done: true });
455
- });
456
-
457
- test("variantKeys property", () => {
458
- const component = getModeComponent(
459
- mode,
460
- cv({
461
- variants: { size: { sm: "sm" }, color: { red: "red" } },
462
- }),
463
- );
464
- expectTypeOf(component.variantKeys).toEqualTypeOf<("size" | "color")[]>();
465
- expect(component.variantKeys).toEqual(["size", "color"]);
466
- });
467
-
468
- test("propKeys property", () => {
469
- const component = getModeComponent(
470
- mode,
471
- cv({ variants: { size: { sm: "sm" }, color: { red: "red" } } }),
472
- );
473
- expectTypeOf(component.propKeys).toExtend<
474
- ("class" | "className" | "style" | "size" | "color")[]
475
- >();
476
- expect(component.propKeys).toEqual(
477
- getExpectedPropsKeys(config, "size", "color"),
478
- );
479
- });
480
-
481
- test("propKeys on different modes", () => {
482
- const component = getModeComponent(
483
- mode,
484
- cv({ variants: { size: { sm: "sm" } } }),
485
- );
486
- expect(component.propKeys).toEqual(getExpectedPropsKeys(config, "size"));
487
- });
488
- });
489
- }
490
-
491
- test("propKeys are mode-specific", () => {
492
- const component = cvBase({
493
- variants: { size: { sm: "sm", md: "md" } },
494
- });
495
-
496
- expectTypeOf(component.propKeys).toEqualTypeOf<
497
- ("class" | "className" | "style" | "size")[]
498
- >();
499
- expectTypeOf(component.jsx.propKeys).toEqualTypeOf<
500
- ("className" | "style" | "size")[]
501
- >();
502
- expectTypeOf(component.html.propKeys).toEqualTypeOf<
503
- ("class" | "style" | "size")[]
504
- >();
505
- expectTypeOf(component.htmlObj.propKeys).toEqualTypeOf<
506
- ("class" | "style" | "size")[]
507
- >();
508
-
509
- expect(component.propKeys).toEqual(["class", "className", "style", "size"]);
510
- expect(component.jsx.propKeys).toEqual(["className", "style", "size"]);
511
- expect(component.html.propKeys).toEqual(["class", "style", "size"]);
512
- expect(component.htmlObj.propKeys).toEqual(["class", "style", "size"]);
513
- });
514
-
515
- describe("Variant utility type", () => {
516
- test("matches variant keys from another component", () => {
517
- const base = cvBase({
518
- variants: { foo: { sm: "foo-sm", lg: "foo-lg" } },
519
- });
520
- const component = cvBase({
521
- extend: [base],
522
- variants: {
523
- bar: {
524
- sm: "bar-sm",
525
- lg: "bar-lg",
526
- } satisfies Variant<typeof base, "foo">,
527
- },
528
- });
529
- expect(component({ bar: "sm" }).class).toContain("bar-sm");
530
- });
531
-
532
- test("rejects invalid variant keys", () => {
533
- const base = cvBase({
534
- variants: { foo: { sm: "foo-sm", lg: "foo-lg" } },
535
- });
536
- cvBase({
537
- extend: [base],
538
- variants: {
539
- bar: {
540
- sm: "bar-sm",
541
- lg: "bar-lg",
542
- // @ts-expect-error
543
- xl: "bar-xl",
544
- } satisfies Variant<typeof base, "foo">,
545
- },
546
- });
547
- });
548
- });