clava 0.6.0 → 0.6.2

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,401 +0,0 @@
1
- import { describe, expectTypeOf, test } from "vitest";
2
- import { cv } from "../src/index.ts";
3
-
4
- const callback = (value: string) => value.toUpperCase();
5
-
6
- describe("variants type inference", () => {
7
- test("function variant infers parameter type as prop type", () => {
8
- const button = cv({
9
- variants: {
10
- size: (value: number) => `size-${value}`,
11
- },
12
- });
13
- expectTypeOf(button.getVariants()).branded.toEqualTypeOf<{
14
- size?: number;
15
- }>();
16
- button({ size: 3 });
17
- button({
18
- // @ts-expect-error string is not assignable to number
19
- size: "lg",
20
- });
21
- });
22
-
23
- test("method variant infers optional parameter type as prop type", () => {
24
- const grid = cv({
25
- variants: {
26
- columns(value?: number) {
27
- return `columns-${value}`;
28
- },
29
- },
30
- });
31
- expectTypeOf(grid.getVariants()).branded.toEqualTypeOf<{
32
- columns?: number;
33
- }>();
34
- grid({ columns: 3 });
35
- grid({ columns: undefined });
36
- grid({
37
- // @ts-expect-error string is not assignable to number
38
- columns: "3",
39
- });
40
- });
41
-
42
- test("function variant infers union parameter type", () => {
43
- const button = cv({
44
- variants: {
45
- tone: (value: "info" | "warn") => `tone-${value}`,
46
- },
47
- });
48
- expectTypeOf(button.getVariants()).branded.toEqualTypeOf<{
49
- tone?: "info" | "warn";
50
- }>();
51
- button({ tone: "info" });
52
- button({ tone: "warn" });
53
- button({
54
- // @ts-expect-error "danger" is not in the union
55
- tone: "danger",
56
- });
57
- });
58
-
59
- test("function variant infers boolean parameter type", () => {
60
- const button = cv({
61
- variants: {
62
- active: (value: boolean) => (value ? "active" : "idle"),
63
- },
64
- });
65
- expectTypeOf(button.getVariants()).branded.toEqualTypeOf<{
66
- active?: boolean;
67
- }>();
68
- button({ active: true });
69
- button({ active: false });
70
- button({
71
- // @ts-expect-error string is not assignable to boolean
72
- active: "yes",
73
- });
74
- });
75
-
76
- test("function variant accepts nullable parameter", () => {
77
- const c = cv({
78
- variants: {
79
- color: (value: string | null) => (value ? `c-${value}` : "c-default"),
80
- },
81
- });
82
- expectTypeOf(c.getVariants()).branded.toEqualTypeOf<{
83
- color?: string | null;
84
- }>();
85
- c({ color: "red" });
86
- c({ color: null });
87
- });
88
-
89
- test("mixed function and object variants infer independently", () => {
90
- const c = cv({
91
- variants: {
92
- size: (value: number) => `s-${value}`,
93
- color: { red: "r", blue: "b" },
94
- },
95
- });
96
- expectTypeOf(c.getVariants()).branded.toEqualTypeOf<{
97
- size?: number;
98
- color?: "red" | "blue";
99
- }>();
100
- c({ size: 3, color: "red" });
101
- c({
102
- // @ts-expect-error number expected
103
- size: "3",
104
- });
105
- c({
106
- // @ts-expect-error "yellow" not in object keys
107
- color: "yellow",
108
- });
109
- });
110
-
111
- test("function variant in child overrides object variant in parent", () => {
112
- const base = cv({ variants: { size: { sm: "sm", lg: "lg" } } });
113
- const child = cv({
114
- extend: [base],
115
- variants: { size: (value: number) => `s-${value}` },
116
- });
117
- expectTypeOf(child.getVariants()).branded.toEqualTypeOf<{
118
- size?: number;
119
- }>();
120
- child({ size: 3 });
121
- child({
122
- // @ts-expect-error string is not assignable to number after override
123
- size: "sm",
124
- });
125
- });
126
-
127
- test("object variant in child overrides function variant in parent", () => {
128
- const base = cv({ variants: { size: (value: number) => `s-${value}` } });
129
- const child = cv({
130
- extend: [base],
131
- variants: { size: { sm: "child-sm", lg: "child-lg" } },
132
- });
133
- expectTypeOf(child.getVariants()).branded.toEqualTypeOf<{
134
- size?: "sm" | "lg";
135
- }>();
136
- child({ size: "sm" });
137
- child({
138
- // @ts-expect-error number is not assignable to "sm" | "lg" after override
139
- size: 3,
140
- });
141
- });
142
-
143
- test("object variant in child merges with object variant in parent", () => {
144
- const base = cv({ variants: { size: { sm: "base-sm", md: "base-md" } } });
145
- const child = cv({
146
- extend: [base],
147
- variants: { size: { lg: "child-lg" } },
148
- });
149
- expectTypeOf(child.getVariants()).branded.toEqualTypeOf<{
150
- size?: "sm" | "md" | "lg";
151
- }>();
152
- child({ size: "sm" });
153
- child({ size: "md" });
154
- child({ size: "lg" });
155
- child({
156
- // @ts-expect-error not in merged keys
157
- size: "xl",
158
- });
159
- });
160
-
161
- test("function variant in parent inherits in child without override", () => {
162
- const base = cv({
163
- variants: { size: (value: number) => `s-${value}` },
164
- });
165
- const child = cv({
166
- extend: [base],
167
- variants: { color: { red: "r" } },
168
- });
169
- expectTypeOf(child.getVariants()).branded.toEqualTypeOf<{
170
- size?: number;
171
- color?: "red";
172
- }>();
173
- child({ size: 5, color: "red" });
174
- child({
175
- // @ts-expect-error inherited size is still number
176
- size: "5",
177
- });
178
- });
179
-
180
- test("defaultVariants infers types from merged variants", () => {
181
- cv({
182
- variants: {
183
- size: (value: number) => `s-${value}`,
184
- color: { red: "r", blue: "b" },
185
- },
186
- defaultVariants: { size: 3, color: "red" },
187
- });
188
- cv({
189
- variants: { size: (value: number) => `s-${value}` },
190
- defaultVariants: {
191
- // @ts-expect-error string is not assignable to number
192
- size: "3",
193
- },
194
- });
195
- });
196
-
197
- test("computed defaultVariants infer defaultValue and variants", () => {
198
- cv({
199
- variants: {
200
- size: (value: number) => `s-${value}`,
201
- color: { red: "r", blue: "b" },
202
- },
203
- defaultVariants: {
204
- size: (defaultValue, variants) => {
205
- expectTypeOf(defaultValue).toEqualTypeOf<number | undefined>();
206
- expectTypeOf(variants.color).toEqualTypeOf<
207
- "red" | "blue" | undefined
208
- >();
209
- return variants.color === "red" ? 10 : defaultValue;
210
- },
211
- },
212
- });
213
- cv({
214
- variants: { size: (value: number) => `s-${value}` },
215
- defaultVariants: {
216
- // @ts-expect-error string is not assignable to number
217
- size: () => "10",
218
- },
219
- });
220
- });
221
-
222
- test("computed defaultVariants method infers defaultValue and variants", () => {
223
- cv({
224
- variants: {
225
- compact: { true: "compact", false: "" },
226
- size(value?: number) {
227
- return `s-${value}`;
228
- },
229
- },
230
- defaultVariants: {
231
- size(defaultValue, variants) {
232
- expectTypeOf(defaultValue).toEqualTypeOf<number | undefined>();
233
- expectTypeOf(variants.compact).toEqualTypeOf<boolean | undefined>();
234
- return variants.compact ? 2 : defaultValue;
235
- },
236
- },
237
- });
238
- cv({
239
- variants: {
240
- size(value?: number) {
241
- return `s-${value}`;
242
- },
243
- },
244
- defaultVariants: {
245
- // @ts-expect-error string is not assignable to number
246
- size() {
247
- return "10";
248
- },
249
- },
250
- });
251
- });
252
-
253
- test("function-valued defaultVariants must return the function", () => {
254
- cv({
255
- variants: {
256
- transform: (value: (value: string) => string) => value("a"),
257
- },
258
- defaultVariants: {
259
- transform: () => callback,
260
- },
261
- });
262
- cv({
263
- variants: {
264
- transform: (value: (value: string) => string) => value("a"),
265
- },
266
- defaultVariants: {
267
- // @ts-expect-error function values must be returned from a computed default
268
- transform: callback,
269
- },
270
- });
271
- });
272
-
273
- test("mixed function-valued defaultVariants must return the function", () => {
274
- cv({
275
- variants: {
276
- transform: (value: ((value: string) => string) | "none") => {
277
- return value === "none" ? "none" : value("a");
278
- },
279
- },
280
- defaultVariants: {
281
- transform: "none",
282
- },
283
- });
284
- cv({
285
- variants: {
286
- transform: (value: ((value: string) => string) | "none") => {
287
- return value === "none" ? "none" : value("a");
288
- },
289
- },
290
- defaultVariants: {
291
- transform: () => callback,
292
- },
293
- });
294
- cv({
295
- variants: {
296
- transform: (value: ((value: string) => string) | "none") => {
297
- return value === "none" ? "none" : value("a");
298
- },
299
- },
300
- defaultVariants: {
301
- // @ts-expect-error function values must be returned from a computed default
302
- transform: callback,
303
- },
304
- });
305
- });
306
-
307
- test("refine callback sees function variant param type", () => {
308
- cv({
309
- variants: { size: (value: number) => `s-${value}` },
310
- refine: ({ variants, setVariants }) => {
311
- expectTypeOf(variants.size).toEqualTypeOf<number | undefined>();
312
- setVariants({ size: 10 });
313
- setVariants({
314
- // @ts-expect-error string not assignable to number
315
- size: "10",
316
- });
317
- },
318
- });
319
- });
320
-
321
- test("refine method infers context from method variants", () => {
322
- cv({
323
- variants: {
324
- size(value?: number) {
325
- return `s-${value}`;
326
- },
327
- },
328
- refine({ variants, setVariants }) {
329
- expectTypeOf(variants.size).toEqualTypeOf<number | undefined>();
330
- setVariants({ size: 10 });
331
- setVariants({
332
- // @ts-expect-error string not assignable to number
333
- size: "10",
334
- });
335
- },
336
- });
337
- });
338
-
339
- test("function variant return type accepts ClassValue and StyleClassValue", () => {
340
- cv({
341
- variants: {
342
- a: (_: number) => "class-a",
343
- b: (_: number) => null,
344
- c: (_: number) => undefined,
345
- d: (_: number) => ["x", "y"],
346
- e: (_: number) => ({ class: "c", style: { color: "red" } }),
347
- f: (_: number) => ({ style: { color: "red" } }),
348
- },
349
- });
350
- });
351
-
352
- test("variantKeys includes function variant keys", () => {
353
- const c = cv({
354
- variants: {
355
- size: { sm: "sm" },
356
- columns: (value: number) => `cols-${value}`,
357
- },
358
- });
359
- expectTypeOf(c.variantKeys).toEqualTypeOf<("size" | "columns")[]>();
360
- });
361
-
362
- test("intermediate object variant hides grandparent function variant from descendants", () => {
363
- const base = cv({
364
- variants: { size: (value: number) => `s-${value}` },
365
- });
366
- const middle = cv({
367
- extend: [base],
368
- variants: { size: { sm: "middle-sm", md: "middle-md" } },
369
- });
370
- // Descendant has no own `size` — it should inherit `middle`'s object,
371
- // not the grandparent function. The intersection used to leak through.
372
- const descendant = cv({ extend: [middle] });
373
- expectTypeOf(descendant.getVariants()).branded.toEqualTypeOf<{
374
- size?: "sm" | "md";
375
- }>();
376
- descendant({ size: "sm" });
377
- descendant({
378
- // @ts-expect-error grandparent function is no longer in the chain
379
- size: 3,
380
- });
381
- });
382
-
383
- test("null disables inherited function variant", () => {
384
- const base = cv({
385
- variants: {
386
- size: (value: number) => `s-${value}`,
387
- },
388
- });
389
- const child = cv({
390
- extend: [base],
391
- variants: { size: null },
392
- });
393
- expectTypeOf(child.getVariants()).branded.toEqualTypeOf<{
394
- size?: never;
395
- }>();
396
- child({
397
- // @ts-expect-error size was disabled
398
- size: 1,
399
- });
400
- });
401
- });