|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { windctrl, wc } from "./"; |
| 2 | +import { windctrl, wc, dynamic as d } from "./"; |
3 | 3 |
|
4 | 4 | describe("wc", () => { |
5 | 5 | it("should be the same as windctrl", () => { |
@@ -321,6 +321,144 @@ describe("windctrl", () => { |
321 | 321 | }); |
322 | 322 | }); |
323 | 323 |
|
| 324 | + describe("Dynamic presets", () => { |
| 325 | + describe("d.px()", () => { |
| 326 | + it("should output inline style for number (px) and keep className empty", () => { |
| 327 | + const box = windctrl({ |
| 328 | + dynamic: { |
| 329 | + w: d.px("width"), |
| 330 | + }, |
| 331 | + }); |
| 332 | + |
| 333 | + const result = box({ w: 123 }); |
| 334 | + expect(result.style).toEqual({ width: "123px" }); |
| 335 | + expect(result.className).toBe(""); |
| 336 | + }); |
| 337 | + |
| 338 | + it("should pass through className string for string input (Unified API)", () => { |
| 339 | + const box = windctrl({ |
| 340 | + dynamic: { |
| 341 | + w: d.px("width"), |
| 342 | + }, |
| 343 | + }); |
| 344 | + |
| 345 | + const result = box({ w: "w-full" }); |
| 346 | + expect(result.className).toContain("w-full"); |
| 347 | + expect(result.style).toEqual(undefined); |
| 348 | + }); |
| 349 | + }); |
| 350 | + |
| 351 | + describe("d.num()", () => { |
| 352 | + it("should output inline style for number (unitless) and keep className empty", () => { |
| 353 | + const layer = windctrl({ |
| 354 | + dynamic: { |
| 355 | + z: d.num("zIndex"), |
| 356 | + }, |
| 357 | + }); |
| 358 | + |
| 359 | + const result = layer({ z: 999 }); |
| 360 | + expect(result.style).toEqual({ zIndex: 999 }); |
| 361 | + expect(result.className).toBe(""); |
| 362 | + }); |
| 363 | + |
| 364 | + it("should pass through className string for string input (Unified API)", () => { |
| 365 | + const layer = windctrl({ |
| 366 | + dynamic: { |
| 367 | + z: d.num("zIndex"), |
| 368 | + }, |
| 369 | + }); |
| 370 | + |
| 371 | + const result = layer({ z: "z-50" }); |
| 372 | + expect(result.className).toContain("z-50"); |
| 373 | + expect(result.style).toEqual(undefined); |
| 374 | + }); |
| 375 | + }); |
| 376 | + |
| 377 | + describe("d.opacity()", () => { |
| 378 | + it("should output inline style for number and keep className empty", () => { |
| 379 | + const fade = windctrl({ |
| 380 | + dynamic: { |
| 381 | + opacity: d.opacity(), |
| 382 | + }, |
| 383 | + }); |
| 384 | + |
| 385 | + const result = fade({ opacity: 0.4 }); |
| 386 | + expect(result.style).toEqual({ opacity: 0.4 }); |
| 387 | + expect(result.className).toBe(""); |
| 388 | + }); |
| 389 | + |
| 390 | + it("should pass through className string for string input (Unified API)", () => { |
| 391 | + const fade = windctrl({ |
| 392 | + dynamic: { |
| 393 | + opacity: d.opacity(), |
| 394 | + }, |
| 395 | + }); |
| 396 | + |
| 397 | + const result = fade({ opacity: "opacity-50" }); |
| 398 | + expect(result.className).toContain("opacity-50"); |
| 399 | + expect(result.style).toEqual(undefined); |
| 400 | + }); |
| 401 | + }); |
| 402 | + |
| 403 | + describe("d.var()", () => { |
| 404 | + it("should set CSS variable as inline style for number with unit (no className output)", () => { |
| 405 | + const card = windctrl({ |
| 406 | + dynamic: { |
| 407 | + x: d.var("--x", { unit: "px" }), |
| 408 | + }, |
| 409 | + }); |
| 410 | + |
| 411 | + const result = card({ x: 12 }); |
| 412 | + |
| 413 | + // NOTE: CSS custom properties are stored as object keys. |
| 414 | + expect(result.style).toEqual({ "--x": "12px" }); |
| 415 | + expect(result.className).toBe(""); |
| 416 | + }); |
| 417 | + |
| 418 | + it("should set CSS variable as inline style for string value (no className output)", () => { |
| 419 | + const card = windctrl({ |
| 420 | + dynamic: { |
| 421 | + x: d.var("--x"), |
| 422 | + }, |
| 423 | + }); |
| 424 | + |
| 425 | + const result = card({ x: "10%" }); |
| 426 | + expect(result.style).toEqual({ "--x": "10%" }); |
| 427 | + expect(result.className).toBe(""); |
| 428 | + }); |
| 429 | + |
| 430 | + it("should merge multiple CSS variables via last-one-wins when same variable is set twice", () => { |
| 431 | + const card = windctrl({ |
| 432 | + dynamic: { |
| 433 | + x1: d.var("--x", { unit: "px" }), |
| 434 | + x2: d.var("--x", { unit: "px" }), |
| 435 | + }, |
| 436 | + }); |
| 437 | + |
| 438 | + const result = card({ x1: 10, x2: 20 }); |
| 439 | + |
| 440 | + // last one wins |
| 441 | + expect(result.style).toEqual({ "--x": "20px" }); |
| 442 | + }); |
| 443 | + }); |
| 444 | + |
| 445 | + it("should coexist with other dynamic resolvers (className + style merge)", () => { |
| 446 | + const box = windctrl({ |
| 447 | + dynamic: { |
| 448 | + w: d.px("width"), |
| 449 | + opacity: d.opacity(), |
| 450 | + // keep an existing custom resolver in the same config |
| 451 | + custom: (v) => (v ? "ring-2" : ""), |
| 452 | + }, |
| 453 | + }); |
| 454 | + |
| 455 | + const result = box({ w: 100, opacity: 0.5, custom: true }); |
| 456 | + |
| 457 | + expect(result.style).toEqual({ width: "100px", opacity: 0.5 }); |
| 458 | + expect(result.className).toContain("ring-2"); |
| 459 | + }); |
| 460 | + }); |
| 461 | + |
324 | 462 | describe("Scopes", () => { |
325 | 463 | it("should apply scope classes with group-data selector", () => { |
326 | 464 | const button = windctrl({ |
|
0 commit comments