clava 0.4.1 → 0.5.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.
@@ -1,6 +1,8 @@
1
1
  import { describe, expectTypeOf, test } from "vitest";
2
2
  import { cv } from "../src/index.ts";
3
3
 
4
+ const callback = (value: string) => value.toUpperCase();
5
+
4
6
  describe("variants type inference", () => {
5
7
  test("function variant infers parameter type as prop type", () => {
6
8
  const button = cv({
@@ -173,6 +175,85 @@ describe("variants type inference", () => {
173
175
  });
174
176
  });
175
177
 
178
+ test("computed defaultVariants infer defaultValue and variants", () => {
179
+ cv({
180
+ variants: {
181
+ size: (value: number) => `s-${value}`,
182
+ color: { red: "r", blue: "b" },
183
+ },
184
+ defaultVariants: {
185
+ size: ({ defaultValue, variants }) => {
186
+ expectTypeOf(defaultValue).toEqualTypeOf<number | undefined>();
187
+ expectTypeOf(variants.color).toEqualTypeOf<
188
+ "red" | "blue" | undefined
189
+ >();
190
+ return variants.color === "red" ? 10 : defaultValue;
191
+ },
192
+ },
193
+ });
194
+ cv({
195
+ variants: { size: (value: number) => `s-${value}` },
196
+ defaultVariants: {
197
+ // @ts-expect-error string is not assignable to number
198
+ size: () => "10",
199
+ },
200
+ });
201
+ });
202
+
203
+ test("function-valued defaultVariants must return the function", () => {
204
+ cv({
205
+ variants: {
206
+ transform: (value: (value: string) => string) => value("a"),
207
+ },
208
+ defaultVariants: {
209
+ transform: () => callback,
210
+ },
211
+ });
212
+ cv({
213
+ variants: {
214
+ transform: (value: (value: string) => string) => value("a"),
215
+ },
216
+ defaultVariants: {
217
+ // @ts-expect-error function values must be returned from a computed default
218
+ transform: callback,
219
+ },
220
+ });
221
+ });
222
+
223
+ test("mixed function-valued defaultVariants must return the function", () => {
224
+ cv({
225
+ variants: {
226
+ transform: (value: ((value: string) => string) | "none") => {
227
+ return value === "none" ? "none" : value("a");
228
+ },
229
+ },
230
+ defaultVariants: {
231
+ transform: "none",
232
+ },
233
+ });
234
+ cv({
235
+ variants: {
236
+ transform: (value: ((value: string) => string) | "none") => {
237
+ return value === "none" ? "none" : value("a");
238
+ },
239
+ },
240
+ defaultVariants: {
241
+ transform: () => callback,
242
+ },
243
+ });
244
+ cv({
245
+ variants: {
246
+ transform: (value: ((value: string) => string) | "none") => {
247
+ return value === "none" ? "none" : value("a");
248
+ },
249
+ },
250
+ defaultVariants: {
251
+ // @ts-expect-error function values must be returned from a computed default
252
+ transform: callback,
253
+ },
254
+ });
255
+ });
256
+
176
257
  test("refine callback sees function variant param type", () => {
177
258
  cv({
178
259
  variants: { size: (value: number) => `s-${value}` },