PettyUI/packages/core/tests/toggle.test.ts
Mats Bosson 168b5642d0 All components, schemas, tests, MCP, and showcase
- 51 headless Web Components (45 core + 6 animation)
- Shared helpers: emit(), part(), listen(), wireLabel(), initialValue()
- Zero `new CustomEvent` or `static #counter` — all use shared utils
- Zod schemas for all 44 core components
- MCP package with discover, inspect, compose, validate tools
- Showcase with Aperture Science theme, M3 Expressive motion
- 81 tests passing, TypeScript strict mode clean
- Signals (~500B), SPA router (~400B), zero dependencies
2026-03-31 21:42:29 +07:00

48 lines
1.5 KiB
TypeScript

import { describe, it, expect, beforeEach } from "vitest";
import { PettyToggle } from "../src/components/toggle/index";
import { h } from "./helpers";
describe("petty-toggle", () => {
let el: PettyToggle;
beforeEach(() => {
document.body.textContent = "";
el = document.createElement("petty-toggle") as PettyToggle;
el.appendChild(h("button", { "data-part": "control", type: "button" }, "Bold"));
document.body.appendChild(el);
});
it("registers the custom element", () => {
expect(customElements.get("petty-toggle")).toBe(PettyToggle);
});
it("defaults to unpressed with aria-pressed false", () => {
expect(el.pressed).toBe(false);
expect(el.dataset.state).toBe("off");
expect(el.querySelector("button")!.getAttribute("aria-pressed")).toBe("false");
});
it("sets pressed via property", () => {
el.pressed = true;
expect(el.hasAttribute("pressed")).toBe(true);
expect(el.querySelector("button")!.getAttribute("aria-pressed")).toBe("true");
expect(el.dataset.state).toBe("on");
});
it("toggles on click and fires petty-change", () => {
let detail: { pressed: boolean } | null = null;
el.addEventListener("petty-change", ((e: CustomEvent) => {
detail = e.detail;
}) as EventListener);
el.querySelector("button")!.click();
expect(el.pressed).toBe(true);
expect(detail).toEqual({ pressed: true });
});
it("does not toggle when disabled", () => {
el.setAttribute("disabled", "");
el.querySelector("button")!.click();
expect(el.pressed).toBe(false);
});
});