|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { createSlotStyleRecipe } from "./slot-style-recipe.js"; |
| 4 | +import { createStyleRecipe } from "./style-recipe.js"; |
| 5 | +import { createThemedRecipes } from "./themed-recipes.js"; |
| 6 | + |
| 7 | +describe("the cache setting of a style recipe's config", () => { |
| 8 | + it("builds a new frozen style on every call without the cache", () => { |
| 9 | + const uncached = createStyleRecipe({ |
| 10 | + cache: false, |
| 11 | + variants: { size: { sm: { height: 32 } } }, |
| 12 | + }); |
| 13 | + const cached = createStyleRecipe({ |
| 14 | + variants: { size: { sm: { height: 32 } } }, |
| 15 | + }); |
| 16 | + |
| 17 | + expect(uncached({ size: "sm" })).not.toBe(uncached({ size: "sm" })); |
| 18 | + expect(uncached({ size: "sm" })).toStrictEqual({ height: 32 }); |
| 19 | + expect(Object.isFrozen(uncached({ size: "sm" }))).toBe(true); |
| 20 | + expect(cached({ size: "sm" })).toBe(cached({ size: "sm" })); |
| 21 | + }); |
| 22 | + |
| 23 | + it("builds new styles on every call of a slot style recipe", () => { |
| 24 | + const card = createSlotStyleRecipe({ |
| 25 | + cache: false, |
| 26 | + slots: ["root"], |
| 27 | + variants: { size: { sm: { root: { height: 32 } } } }, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(card({ size: "sm" })).not.toBe(card({ size: "sm" })); |
| 31 | + expect(card({ size: "sm" })).toStrictEqual({ root: { height: 32 } }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("applies to the recipe of each theme", () => { |
| 35 | + const theme: { readonly radius: number } = { radius: 8 }; |
| 36 | + const { createStyleRecipe: createThemedStyleRecipe } = |
| 37 | + createThemedRecipes<typeof theme>(); |
| 38 | + const box = createThemedStyleRecipe((tokens) => ({ |
| 39 | + cache: false, |
| 40 | + base: { borderRadius: tokens.radius }, |
| 41 | + variants: { size: { sm: { height: 32 } } }, |
| 42 | + })); |
| 43 | + |
| 44 | + expect(box(theme, { size: "sm" })).not.toBe(box(theme, { size: "sm" })); |
| 45 | + expect(box(theme, { size: "sm" })).toStrictEqual({ |
| 46 | + borderRadius: 8, |
| 47 | + height: 32, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("rejects a setting that is not a boolean", () => { |
| 52 | + const box = createStyleRecipe({ |
| 53 | + // @ts-expect-error cache must be a boolean |
| 54 | + cache: "no", |
| 55 | + variants: { size: { sm: { height: 32 } } }, |
| 56 | + }); |
| 57 | + const card = createSlotStyleRecipe({ |
| 58 | + // @ts-expect-error cache must be a boolean |
| 59 | + cache: "no", |
| 60 | + slots: ["root"], |
| 61 | + variants: { size: { sm: { root: { height: 32 } } } }, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(box({ size: "sm" })).toStrictEqual({ height: 32 }); |
| 65 | + expect(card({ size: "sm" })).toStrictEqual({ root: { height: 32 } }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments