clava 0.1.19 → 0.2.0
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/CHANGELOG.md +31 -0
- package/dist/index.d.ts +13 -20
- package/dist/index.js +49 -21
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +79 -53
- package/src/types.ts +13 -4
- package/tests/_utils.ts +176 -0
- package/tests/class-style-test.ts +341 -0
- package/tests/component-api-test.ts +214 -0
- package/tests/computed-test.ts +800 -0
- package/tests/computed-variants-test.ts +298 -0
- package/tests/extend-test.ts +253 -0
- package/{src/test-language-service.ts → tests/language-service-test.ts} +3 -2
- package/{src/test-react.ts → tests/react-test.ts} +3 -3
- package/{src/test-solid.ts → tests/solid-test.ts} +3 -5
- package/tests/split-props-test.ts +590 -0
- package/tests/variants-test.ts +380 -0
- package/tsconfig.json +1 -1
- package/src/test.ts +0 -2873
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
CONFIGS,
|
|
4
|
+
createCVFromConfig,
|
|
5
|
+
getConfigDescription,
|
|
6
|
+
getConfigMode,
|
|
7
|
+
getConfigTransformClass,
|
|
8
|
+
getModeComponent,
|
|
9
|
+
getStyleClass,
|
|
10
|
+
} from "./_utils.ts";
|
|
11
|
+
|
|
12
|
+
for (const config of Object.values(CONFIGS)) {
|
|
13
|
+
const mode = getConfigMode(config);
|
|
14
|
+
const cv = createCVFromConfig(config);
|
|
15
|
+
const cls = getConfigTransformClass(config);
|
|
16
|
+
|
|
17
|
+
describe(getConfigDescription(config), () => {
|
|
18
|
+
test("computed", () => {
|
|
19
|
+
const component = getModeComponent(
|
|
20
|
+
mode,
|
|
21
|
+
cv({
|
|
22
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
23
|
+
computed: ({ variants }) =>
|
|
24
|
+
variants.size === "lg" ? "computed-lg" : null,
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
const props = component({ size: "lg" });
|
|
28
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg computed-lg") });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("computed with setVariants", () => {
|
|
32
|
+
const component = getModeComponent(
|
|
33
|
+
mode,
|
|
34
|
+
cv({
|
|
35
|
+
variants: {
|
|
36
|
+
size: { sm: "sm", lg: "lg" },
|
|
37
|
+
color: { red: "red", blue: "blue" },
|
|
38
|
+
},
|
|
39
|
+
computed: ({ variants, setVariants }) => {
|
|
40
|
+
if (variants.size === "lg") {
|
|
41
|
+
setVariants({ color: "red" });
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
const props = component({ size: "lg" });
|
|
47
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("computed with setDefaultVariants", () => {
|
|
51
|
+
const component = getModeComponent(
|
|
52
|
+
mode,
|
|
53
|
+
cv({
|
|
54
|
+
variants: {
|
|
55
|
+
size: { sm: "sm", lg: "lg" },
|
|
56
|
+
color: { red: "red", blue: "blue" },
|
|
57
|
+
},
|
|
58
|
+
computed: ({ variants, setDefaultVariants }) => {
|
|
59
|
+
if (variants.size === "lg") {
|
|
60
|
+
setDefaultVariants({ color: "red" });
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
const props = component({ size: "lg" });
|
|
66
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("computed setDefaultVariants does not override props", () => {
|
|
70
|
+
const component = getModeComponent(
|
|
71
|
+
mode,
|
|
72
|
+
cv({
|
|
73
|
+
variants: {
|
|
74
|
+
size: { sm: "sm", lg: "lg" },
|
|
75
|
+
color: { red: "red", blue: "blue" },
|
|
76
|
+
},
|
|
77
|
+
computed: ({ setDefaultVariants }) => {
|
|
78
|
+
setDefaultVariants({ color: "red" });
|
|
79
|
+
},
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
const props = component({ size: "lg", color: "blue" });
|
|
83
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg blue") });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("computed setDefaultVariants overrides defaultVariants", () => {
|
|
87
|
+
const component = getModeComponent(
|
|
88
|
+
mode,
|
|
89
|
+
cv({
|
|
90
|
+
variants: {
|
|
91
|
+
size: { sm: "sm", lg: "lg" },
|
|
92
|
+
color: { red: "red", blue: "blue" },
|
|
93
|
+
},
|
|
94
|
+
defaultVariants: { size: "sm", color: "red" },
|
|
95
|
+
computed: ({ setDefaultVariants }) => {
|
|
96
|
+
setDefaultVariants({ color: "blue" });
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
const props = component();
|
|
101
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm blue") });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("computed setDefaultVariants overrides extended defaultVariants", () => {
|
|
105
|
+
const base = cv({
|
|
106
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
107
|
+
defaultVariants: { color: "red" },
|
|
108
|
+
});
|
|
109
|
+
const component = getModeComponent(
|
|
110
|
+
mode,
|
|
111
|
+
cv({
|
|
112
|
+
extend: [base],
|
|
113
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
114
|
+
defaultVariants: { size: "sm" },
|
|
115
|
+
computed: ({ setDefaultVariants }) => {
|
|
116
|
+
setDefaultVariants({ color: "blue" });
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
const props = component();
|
|
121
|
+
expect(getStyleClass(props)).toEqual({ class: cls("blue sm") });
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("computed setDefaultVariants overrides child defaultVariants", () => {
|
|
125
|
+
const base = cv({
|
|
126
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
127
|
+
});
|
|
128
|
+
const component = getModeComponent(
|
|
129
|
+
mode,
|
|
130
|
+
cv({
|
|
131
|
+
extend: [base],
|
|
132
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
133
|
+
defaultVariants: { size: "sm", color: "red" },
|
|
134
|
+
computed: ({ setDefaultVariants }) => {
|
|
135
|
+
setDefaultVariants({ size: "lg" });
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
139
|
+
const props = component();
|
|
140
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("computed setDefaultVariants from parent overrides child defaultVariants", () => {
|
|
144
|
+
const base = cv({
|
|
145
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
146
|
+
defaultVariants: { size: "sm" },
|
|
147
|
+
computed: ({ setDefaultVariants }) => {
|
|
148
|
+
setDefaultVariants({ size: "lg" });
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
const component = getModeComponent(
|
|
152
|
+
mode,
|
|
153
|
+
cv({
|
|
154
|
+
extend: [base],
|
|
155
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
156
|
+
defaultVariants: { size: "sm", color: "red" },
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
const props = component();
|
|
160
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("computed setDefaultVariants from parent overrides child defaultVariants based on props", () => {
|
|
164
|
+
const base = cv({
|
|
165
|
+
variants: { size: { sm: "sm", lg: "lg" }, enabled: "" },
|
|
166
|
+
defaultVariants: { size: "sm" },
|
|
167
|
+
computed: ({ variants, setDefaultVariants }) => {
|
|
168
|
+
if (!variants.enabled) return;
|
|
169
|
+
setDefaultVariants({ size: "lg" });
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
const component = getModeComponent(
|
|
173
|
+
mode,
|
|
174
|
+
cv({
|
|
175
|
+
extend: [base],
|
|
176
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
177
|
+
defaultVariants: { size: "sm", color: "red" },
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
const props = component({ enabled: true });
|
|
181
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("computed receives default variants from child", () => {
|
|
185
|
+
const base = cv({
|
|
186
|
+
variants: { size: { sm: "sm", lg: "lg" }, large: "" },
|
|
187
|
+
defaultVariants: { size: "sm" },
|
|
188
|
+
computed: ({ variants, setDefaultVariants }) => {
|
|
189
|
+
if (variants.large) {
|
|
190
|
+
setDefaultVariants({ size: "lg" });
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
const component = getModeComponent(
|
|
195
|
+
mode,
|
|
196
|
+
cv({
|
|
197
|
+
extend: [base],
|
|
198
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
199
|
+
defaultVariants: { size: "sm", color: "red", large: true },
|
|
200
|
+
}),
|
|
201
|
+
);
|
|
202
|
+
const props = component();
|
|
203
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("computed receives default variants from grandchild", () => {
|
|
207
|
+
const base = cv({
|
|
208
|
+
variants: { size: { sm: "sm", lg: "lg" }, large: "" },
|
|
209
|
+
defaultVariants: { size: "sm" },
|
|
210
|
+
computed: ({ variants, setDefaultVariants }) => {
|
|
211
|
+
if (variants.large) {
|
|
212
|
+
setDefaultVariants({ size: "lg" });
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
const base2 = cv({ extend: [base] });
|
|
217
|
+
const component = getModeComponent(
|
|
218
|
+
mode,
|
|
219
|
+
cv({
|
|
220
|
+
extend: [base2],
|
|
221
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
222
|
+
defaultVariants: { size: "sm", color: "red", large: true },
|
|
223
|
+
}),
|
|
224
|
+
);
|
|
225
|
+
const props = component();
|
|
226
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("computed receives default variants from intermediate component", () => {
|
|
230
|
+
const parent = cv({
|
|
231
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
232
|
+
computed: ({ variants, setDefaultVariants }) => {
|
|
233
|
+
if (!variants.size) {
|
|
234
|
+
setDefaultVariants({ size: "lg" });
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
const child = cv({ extend: [parent], defaultVariants: { size: "sm" } });
|
|
239
|
+
const component = getModeComponent(mode, cv({ extend: [child] }));
|
|
240
|
+
const props = component();
|
|
241
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("child computed setDefaultVariants overrides parent computed setDefaultVariants", () => {
|
|
245
|
+
const base = cv({
|
|
246
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
247
|
+
defaultVariants: { size: "sm" },
|
|
248
|
+
computed: ({ setDefaultVariants }) => {
|
|
249
|
+
setDefaultVariants({ size: "lg" });
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
const component = getModeComponent(
|
|
253
|
+
mode,
|
|
254
|
+
cv({
|
|
255
|
+
extend: [base],
|
|
256
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
257
|
+
defaultVariants: { size: "sm", color: "red" },
|
|
258
|
+
computed: ({ setDefaultVariants }) => {
|
|
259
|
+
setDefaultVariants({ size: "sm" });
|
|
260
|
+
},
|
|
261
|
+
}),
|
|
262
|
+
);
|
|
263
|
+
const props = component();
|
|
264
|
+
// Order: parent defaultVariants (sm) -> child defaultVariants (sm)
|
|
265
|
+
// -> parent computed.setDefaultVariants (lg)
|
|
266
|
+
// -> child computed.setDefaultVariants (sm)
|
|
267
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm red") });
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("child setDefaultVariants receives computed variants from parent", () => {
|
|
271
|
+
const base = cv({
|
|
272
|
+
variants: { size: { sm: "sm", lg: "lg" }, small: "" },
|
|
273
|
+
computed: ({ setDefaultVariants }) => {
|
|
274
|
+
setDefaultVariants({ small: true });
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
const component = getModeComponent(
|
|
278
|
+
mode,
|
|
279
|
+
cv({
|
|
280
|
+
extend: [base],
|
|
281
|
+
variants: { color: { red: "red", blue: "blue" } },
|
|
282
|
+
defaultVariants: { size: "lg", color: "red" },
|
|
283
|
+
computed: ({ variants, setDefaultVariants }) => {
|
|
284
|
+
if (variants.small) {
|
|
285
|
+
setDefaultVariants({ size: "sm" });
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
}),
|
|
289
|
+
);
|
|
290
|
+
const props = component();
|
|
291
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm red") });
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("computed setDefaultVariants when explicitly passing undefined", () => {
|
|
295
|
+
const component = getModeComponent(
|
|
296
|
+
mode,
|
|
297
|
+
cv({
|
|
298
|
+
variants: {
|
|
299
|
+
size: { sm: "sm", lg: "lg" },
|
|
300
|
+
color: { red: "red", blue: "blue" },
|
|
301
|
+
},
|
|
302
|
+
computed: ({ setDefaultVariants }) => {
|
|
303
|
+
setDefaultVariants({ color: "red" });
|
|
304
|
+
},
|
|
305
|
+
}),
|
|
306
|
+
);
|
|
307
|
+
const props = component({ size: "lg", color: undefined });
|
|
308
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test("computed with defaultVariants", () => {
|
|
312
|
+
const component = getModeComponent(
|
|
313
|
+
mode,
|
|
314
|
+
cv({
|
|
315
|
+
variants: {
|
|
316
|
+
size: { sm: "sm", lg: "lg" },
|
|
317
|
+
color: { red: "red", blue: "blue" },
|
|
318
|
+
},
|
|
319
|
+
defaultVariants: { size: "lg" },
|
|
320
|
+
computed: ({ variants }) =>
|
|
321
|
+
variants.size === "lg" ? "computed-lg" : null,
|
|
322
|
+
}),
|
|
323
|
+
);
|
|
324
|
+
const props = component();
|
|
325
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg computed-lg") });
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
test("computed with defaultVariants from extended", () => {
|
|
329
|
+
const base = cv({
|
|
330
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
331
|
+
defaultVariants: { size: "lg" },
|
|
332
|
+
});
|
|
333
|
+
const component = getModeComponent(
|
|
334
|
+
mode,
|
|
335
|
+
cv({
|
|
336
|
+
extend: [base],
|
|
337
|
+
computed: ({ variants }) =>
|
|
338
|
+
variants.size === "lg" ? "computed-lg" : null,
|
|
339
|
+
}),
|
|
340
|
+
);
|
|
341
|
+
const props = component();
|
|
342
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg computed-lg") });
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test("computed from parent receives boolean default value from overridden variant in child", () => {
|
|
346
|
+
const base = cv({
|
|
347
|
+
variants: {
|
|
348
|
+
size: { sm: "sm", lg: "lg" },
|
|
349
|
+
border: { default: "default", true: "border", false: "" },
|
|
350
|
+
},
|
|
351
|
+
defaultVariants: { size: "lg" },
|
|
352
|
+
computed: ({ variants, setVariants }) => {
|
|
353
|
+
expect(variants.border).toBe(false);
|
|
354
|
+
if (!variants.border) {
|
|
355
|
+
setVariants({ size: "sm" });
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
const component = getModeComponent(
|
|
360
|
+
mode,
|
|
361
|
+
cv({
|
|
362
|
+
extend: [base],
|
|
363
|
+
computedVariants: {
|
|
364
|
+
border: (_: boolean) => {},
|
|
365
|
+
},
|
|
366
|
+
defaultVariants: { border: false },
|
|
367
|
+
}),
|
|
368
|
+
);
|
|
369
|
+
const props = component();
|
|
370
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("computed from parent receives boolean default value from overridden variant in grandchild", () => {
|
|
374
|
+
const base = cv({
|
|
375
|
+
variants: {
|
|
376
|
+
size: { sm: "sm", lg: "lg" },
|
|
377
|
+
border: { default: "default", true: "border", false: "" },
|
|
378
|
+
},
|
|
379
|
+
defaultVariants: { size: "lg" },
|
|
380
|
+
computed: ({ variants, setVariants }) => {
|
|
381
|
+
expect(variants.border).toBe(false);
|
|
382
|
+
if (!variants.border) {
|
|
383
|
+
setVariants({ size: "sm" });
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
const base2 = cv({ extend: [base] });
|
|
388
|
+
const component = getModeComponent(
|
|
389
|
+
mode,
|
|
390
|
+
cv({
|
|
391
|
+
extend: [base2],
|
|
392
|
+
computedVariants: {
|
|
393
|
+
border: (_: boolean) => {},
|
|
394
|
+
},
|
|
395
|
+
defaultVariants: { border: false },
|
|
396
|
+
}),
|
|
397
|
+
);
|
|
398
|
+
const props = component();
|
|
399
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test("computed from parent receives false prop from overridden variant in child", () => {
|
|
403
|
+
const base = cv({
|
|
404
|
+
variants: {
|
|
405
|
+
size: { sm: "sm", lg: "lg" },
|
|
406
|
+
border: { default: "default", true: "border", false: "" },
|
|
407
|
+
},
|
|
408
|
+
defaultVariants: { size: "lg" },
|
|
409
|
+
computed: ({ variants, setVariants }) => {
|
|
410
|
+
expect(variants.border).toBe(false);
|
|
411
|
+
if (!variants.border) {
|
|
412
|
+
setVariants({ size: "sm" });
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
const component = getModeComponent(
|
|
417
|
+
mode,
|
|
418
|
+
cv({
|
|
419
|
+
extend: [base],
|
|
420
|
+
computedVariants: {
|
|
421
|
+
border: (_: boolean) => {},
|
|
422
|
+
},
|
|
423
|
+
}),
|
|
424
|
+
);
|
|
425
|
+
const props = component({ border: false });
|
|
426
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test("computed from parent receives true prop from overridden variant in child", () => {
|
|
430
|
+
const base = cv({
|
|
431
|
+
variants: {
|
|
432
|
+
size: { sm: "sm", lg: "lg" },
|
|
433
|
+
border: { default: "default", true: "border", false: "" },
|
|
434
|
+
},
|
|
435
|
+
defaultVariants: { size: "lg" },
|
|
436
|
+
computed: ({ variants, setVariants }) => {
|
|
437
|
+
expect(variants.border).toBe(true);
|
|
438
|
+
if (variants.border) {
|
|
439
|
+
setVariants({ size: "sm" });
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
const component = getModeComponent(
|
|
444
|
+
mode,
|
|
445
|
+
cv({
|
|
446
|
+
extend: [base],
|
|
447
|
+
computedVariants: {
|
|
448
|
+
border: (_: boolean) => {},
|
|
449
|
+
},
|
|
450
|
+
}),
|
|
451
|
+
);
|
|
452
|
+
const props = component({ border: true });
|
|
453
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
test("computed from parent receives true prop from overridden variant in grandchild", () => {
|
|
457
|
+
const base = cv({
|
|
458
|
+
variants: {
|
|
459
|
+
size: { sm: "sm", lg: "lg" },
|
|
460
|
+
border: { default: "default", true: "border", false: "" },
|
|
461
|
+
},
|
|
462
|
+
defaultVariants: { size: "lg" },
|
|
463
|
+
computed: ({ variants, setVariants }) => {
|
|
464
|
+
expect(variants.border).toBe(true);
|
|
465
|
+
if (variants.border) {
|
|
466
|
+
setVariants({ size: "sm" });
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
const base2 = cv({ extend: [base] });
|
|
471
|
+
const component = getModeComponent(
|
|
472
|
+
mode,
|
|
473
|
+
cv({
|
|
474
|
+
extend: [base2],
|
|
475
|
+
computedVariants: {
|
|
476
|
+
border: (_: boolean) => {},
|
|
477
|
+
},
|
|
478
|
+
}),
|
|
479
|
+
);
|
|
480
|
+
const props = component({ border: true });
|
|
481
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
test("computed with style", () => {
|
|
485
|
+
const component = getModeComponent(
|
|
486
|
+
mode,
|
|
487
|
+
cv({
|
|
488
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
489
|
+
computed: ({ variants }) =>
|
|
490
|
+
variants.size === "lg" ? { style: { fontSize: "20px" } } : null,
|
|
491
|
+
}),
|
|
492
|
+
);
|
|
493
|
+
const props = component({ size: "lg" });
|
|
494
|
+
expect(getStyleClass(props)).toEqual({
|
|
495
|
+
class: cls("lg"),
|
|
496
|
+
fontSize: "20px",
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
test("computed with class and style", () => {
|
|
501
|
+
const component = getModeComponent(
|
|
502
|
+
mode,
|
|
503
|
+
cv({
|
|
504
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
505
|
+
computed: ({ variants }) =>
|
|
506
|
+
variants.size === "lg"
|
|
507
|
+
? { class: "computed-lg", style: { fontSize: "20px" } }
|
|
508
|
+
: null,
|
|
509
|
+
}),
|
|
510
|
+
);
|
|
511
|
+
const props = component({ size: "lg" });
|
|
512
|
+
expect(getStyleClass(props)).toEqual({
|
|
513
|
+
class: cls("lg computed-lg"),
|
|
514
|
+
fontSize: "20px",
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
test("computed style does not accept numbers", () => {
|
|
519
|
+
const component = getModeComponent(
|
|
520
|
+
mode,
|
|
521
|
+
cv({
|
|
522
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
523
|
+
// @ts-expect-error
|
|
524
|
+
computed: ({ variants }) =>
|
|
525
|
+
variants.size === "lg"
|
|
526
|
+
? {
|
|
527
|
+
class: "computed-lg",
|
|
528
|
+
style: { fontSize: 20 },
|
|
529
|
+
}
|
|
530
|
+
: null,
|
|
531
|
+
}),
|
|
532
|
+
);
|
|
533
|
+
const props = component({ size: "lg" });
|
|
534
|
+
expect(getStyleClass(props)).toEqual({
|
|
535
|
+
class: cls("lg computed-lg"),
|
|
536
|
+
fontSize: expect.toBeOneOf(["20", "20px"]),
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
test("computed setVariants does not accept invalid keys", () => {
|
|
541
|
+
const component = getModeComponent(
|
|
542
|
+
mode,
|
|
543
|
+
cv({
|
|
544
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
545
|
+
computed: ({ setVariants }) => {
|
|
546
|
+
setVariants({
|
|
547
|
+
// @ts-expect-error
|
|
548
|
+
invalidKey: "value",
|
|
549
|
+
});
|
|
550
|
+
},
|
|
551
|
+
}),
|
|
552
|
+
);
|
|
553
|
+
const props = component({ size: "lg" });
|
|
554
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg") });
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
test("computed setVariants does not accept invalid values", () => {
|
|
558
|
+
const component = getModeComponent(
|
|
559
|
+
mode,
|
|
560
|
+
cv({
|
|
561
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
562
|
+
computed: ({ setVariants }) => {
|
|
563
|
+
setVariants({
|
|
564
|
+
// @ts-expect-error invalid value
|
|
565
|
+
size:
|
|
566
|
+
// no error
|
|
567
|
+
"invalid",
|
|
568
|
+
});
|
|
569
|
+
},
|
|
570
|
+
}),
|
|
571
|
+
);
|
|
572
|
+
const props = component({ size: "lg" });
|
|
573
|
+
// Invalid value overrides the valid one, resulting in no match
|
|
574
|
+
expect(getStyleClass(props)).toEqual({ class: "" });
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test("computed addClass with string", () => {
|
|
578
|
+
const component = getModeComponent(
|
|
579
|
+
mode,
|
|
580
|
+
cv({
|
|
581
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
582
|
+
computed: ({ variants, addClass }) => {
|
|
583
|
+
if (variants.size === "lg") {
|
|
584
|
+
addClass("added-lg");
|
|
585
|
+
}
|
|
586
|
+
},
|
|
587
|
+
}),
|
|
588
|
+
);
|
|
589
|
+
const props = component({ size: "lg" });
|
|
590
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg added-lg") });
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
test("computed addClass with array", () => {
|
|
594
|
+
const component = getModeComponent(
|
|
595
|
+
mode,
|
|
596
|
+
cv({
|
|
597
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
598
|
+
computed: ({ variants, addClass }) => {
|
|
599
|
+
if (variants.size === "lg") {
|
|
600
|
+
addClass(["added-lg", "extra-class"]);
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
}),
|
|
604
|
+
);
|
|
605
|
+
const props = component({ size: "lg" });
|
|
606
|
+
expect(getStyleClass(props)).toEqual({
|
|
607
|
+
class: cls("lg added-lg extra-class"),
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
test("computed addStyle", () => {
|
|
612
|
+
const component = getModeComponent(
|
|
613
|
+
mode,
|
|
614
|
+
cv({
|
|
615
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
616
|
+
computed: ({ variants, addStyle }) => {
|
|
617
|
+
if (variants.size === "lg") {
|
|
618
|
+
addStyle({ fontSize: "20px" });
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
}),
|
|
622
|
+
);
|
|
623
|
+
const props = component({ size: "lg" });
|
|
624
|
+
expect(getStyleClass(props)).toEqual({
|
|
625
|
+
class: cls("lg"),
|
|
626
|
+
fontSize: "20px",
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
test("computed addClass combined with return value", () => {
|
|
631
|
+
const component = getModeComponent(
|
|
632
|
+
mode,
|
|
633
|
+
cv({
|
|
634
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
635
|
+
computed: ({ variants, addClass }) => {
|
|
636
|
+
if (variants.size === "lg") {
|
|
637
|
+
addClass("added-class");
|
|
638
|
+
}
|
|
639
|
+
return "returned-class";
|
|
640
|
+
},
|
|
641
|
+
}),
|
|
642
|
+
);
|
|
643
|
+
const props = component({ size: "lg" });
|
|
644
|
+
expect(getStyleClass(props)).toEqual({
|
|
645
|
+
class: cls("lg added-class returned-class"),
|
|
646
|
+
});
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
test("computed addStyle combined with return value", () => {
|
|
650
|
+
const component = getModeComponent(
|
|
651
|
+
mode,
|
|
652
|
+
cv({
|
|
653
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
654
|
+
computed: ({ variants, addStyle }) => {
|
|
655
|
+
if (variants.size === "lg") {
|
|
656
|
+
addStyle({ fontSize: "20px" });
|
|
657
|
+
}
|
|
658
|
+
return { style: { backgroundColor: "red" } };
|
|
659
|
+
},
|
|
660
|
+
}),
|
|
661
|
+
);
|
|
662
|
+
const props = component({ size: "lg" });
|
|
663
|
+
expect(getStyleClass(props)).toEqual({
|
|
664
|
+
class: cls("lg"),
|
|
665
|
+
fontSize: "20px",
|
|
666
|
+
backgroundColor: "red",
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
test("computed addClass and addStyle together", () => {
|
|
671
|
+
const component = getModeComponent(
|
|
672
|
+
mode,
|
|
673
|
+
cv({
|
|
674
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
675
|
+
computed: ({ variants, addClass, addStyle }) => {
|
|
676
|
+
if (variants.size === "lg") {
|
|
677
|
+
addClass("added-lg");
|
|
678
|
+
addStyle({ fontSize: "20px" });
|
|
679
|
+
}
|
|
680
|
+
},
|
|
681
|
+
}),
|
|
682
|
+
);
|
|
683
|
+
const props = component({ size: "lg" });
|
|
684
|
+
expect(getStyleClass(props)).toEqual({
|
|
685
|
+
class: cls("lg added-lg"),
|
|
686
|
+
fontSize: "20px",
|
|
687
|
+
});
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
test("computed addClass and addStyle with return value", () => {
|
|
691
|
+
const component = getModeComponent(
|
|
692
|
+
mode,
|
|
693
|
+
cv({
|
|
694
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
695
|
+
computed: ({ variants, addClass, addStyle }) => {
|
|
696
|
+
if (variants.size === "lg") {
|
|
697
|
+
addClass("added-lg");
|
|
698
|
+
addStyle({ fontSize: "20px" });
|
|
699
|
+
}
|
|
700
|
+
return {
|
|
701
|
+
class: "returned-class",
|
|
702
|
+
style: { backgroundColor: "red" },
|
|
703
|
+
};
|
|
704
|
+
},
|
|
705
|
+
}),
|
|
706
|
+
);
|
|
707
|
+
const props = component({ size: "lg" });
|
|
708
|
+
expect(getStyleClass(props)).toEqual({
|
|
709
|
+
class: cls("lg added-lg returned-class"),
|
|
710
|
+
fontSize: "20px",
|
|
711
|
+
backgroundColor: "red",
|
|
712
|
+
});
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
test("computed addClass multiple calls", () => {
|
|
716
|
+
const component = getModeComponent(
|
|
717
|
+
mode,
|
|
718
|
+
cv({
|
|
719
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
720
|
+
computed: ({ variants, addClass }) => {
|
|
721
|
+
if (variants.size === "lg") {
|
|
722
|
+
addClass("first");
|
|
723
|
+
addClass("second");
|
|
724
|
+
addClass("third");
|
|
725
|
+
}
|
|
726
|
+
},
|
|
727
|
+
}),
|
|
728
|
+
);
|
|
729
|
+
const props = component({ size: "lg" });
|
|
730
|
+
expect(getStyleClass(props)).toEqual({
|
|
731
|
+
class: cls("lg first second third"),
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
test("computed addStyle multiple calls merges styles", () => {
|
|
736
|
+
const component = getModeComponent(
|
|
737
|
+
mode,
|
|
738
|
+
cv({
|
|
739
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
740
|
+
computed: ({ variants, addStyle }) => {
|
|
741
|
+
if (variants.size === "lg") {
|
|
742
|
+
addStyle({ fontSize: "20px" });
|
|
743
|
+
addStyle({ backgroundColor: "red" });
|
|
744
|
+
addStyle({ color: "blue" });
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
}),
|
|
748
|
+
);
|
|
749
|
+
const props = component({ size: "lg" });
|
|
750
|
+
expect(getStyleClass(props)).toEqual({
|
|
751
|
+
class: cls("lg"),
|
|
752
|
+
fontSize: "20px",
|
|
753
|
+
backgroundColor: "red",
|
|
754
|
+
color: "blue",
|
|
755
|
+
});
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
test("computed addStyle later call overrides earlier", () => {
|
|
759
|
+
const component = getModeComponent(
|
|
760
|
+
mode,
|
|
761
|
+
cv({
|
|
762
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
763
|
+
computed: ({ variants, addStyle }) => {
|
|
764
|
+
if (variants.size === "lg") {
|
|
765
|
+
addStyle({ fontSize: "16px" });
|
|
766
|
+
addStyle({ fontSize: "20px" });
|
|
767
|
+
}
|
|
768
|
+
},
|
|
769
|
+
}),
|
|
770
|
+
);
|
|
771
|
+
const props = component({ size: "lg" });
|
|
772
|
+
expect(getStyleClass(props)).toEqual({
|
|
773
|
+
class: cls("lg"),
|
|
774
|
+
fontSize: "20px",
|
|
775
|
+
});
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
test("computed addStyle does not accept numbers", () => {
|
|
779
|
+
const component = getModeComponent(
|
|
780
|
+
mode,
|
|
781
|
+
cv({
|
|
782
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
783
|
+
computed: ({ variants, addStyle }) => {
|
|
784
|
+
if (variants.size === "lg") {
|
|
785
|
+
addStyle({
|
|
786
|
+
// @ts-expect-error
|
|
787
|
+
fontSize: 20,
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
},
|
|
791
|
+
}),
|
|
792
|
+
);
|
|
793
|
+
const props = component({ size: "lg" });
|
|
794
|
+
expect(getStyleClass(props)).toEqual({
|
|
795
|
+
class: cls("lg"),
|
|
796
|
+
fontSize: expect.toBeOneOf(["20", "20px"]),
|
|
797
|
+
});
|
|
798
|
+
});
|
|
799
|
+
});
|
|
800
|
+
}
|