|
| 1 | +import { renderHook } from "@testing-library/react"; |
| 2 | +import { useCreateDataAttribute, useComposeAttributes } from "."; |
| 3 | + |
| 4 | +/** |
| 5 | + * Test case: Verify that attributes are correctly created and applied. |
| 6 | + */ |
| 7 | +test("attributes are created and applied", () => { |
| 8 | + // Render the hook for creating data attributes with the prefix "button" |
| 9 | + const { result: dataAttributes } = renderHook(() => |
| 10 | + useCreateDataAttribute("button", { variant: "primary", size: "large" }) |
| 11 | + ); |
| 12 | + |
| 13 | + // Render the hook for creating data attributes with the prefix "accent" |
| 14 | + const { result: accentAttributes } = renderHook(() => |
| 15 | + useCreateDataAttribute("accent", { color: "red" }) |
| 16 | + ); |
| 17 | + |
| 18 | + // Render the hook that merges the two attribute objects into a single object |
| 19 | + const { result: composedAttributes } = renderHook(() => |
| 20 | + useComposeAttributes(dataAttributes.current(), accentAttributes.current()) |
| 21 | + ); |
| 22 | + |
| 23 | + // Assert that the composed attributes object contains the expected `data-*` attributes |
| 24 | + expect(composedAttributes.current()).toEqual({ |
| 25 | + "data-button-variant": "primary", |
| 26 | + "data-button-size": "large", |
| 27 | + "data-accent-color": "red", |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +/** |
| 32 | + * Test case: Verify that attributes are correctly created, ignoring undefined or empty values. |
| 33 | + */ |
| 34 | +test("attributes are created and applied with undefined and empty values", () => { |
| 35 | + // Render the hook with an undefined variant and a defined size |
| 36 | + const { result: dataAttributes } = renderHook(() => |
| 37 | + useCreateDataAttribute("button", { variant: undefined, size: "large" }) |
| 38 | + ); |
| 39 | + |
| 40 | + // Render the hook with an empty string for color (should be ignored) |
| 41 | + const { result: accentAttributes } = renderHook(() => |
| 42 | + useCreateDataAttribute("accent", { color: "" }) |
| 43 | + ); |
| 44 | + |
| 45 | + // Merge the attributes |
| 46 | + const { result: composedAttributes } = renderHook(() => |
| 47 | + useComposeAttributes(dataAttributes.current(), accentAttributes.current()) |
| 48 | + ); |
| 49 | + |
| 50 | + // Assert that only the valid `data-*` attributes are present |
| 51 | + expect(composedAttributes.current()).toEqual({ |
| 52 | + "data-button-size": "large", // "variant" is ignored since it's undefined, and "color" is ignored since it's an empty string |
| 53 | + }); |
| 54 | +}); |
0 commit comments