|
| 1 | +import van from "vanjs-core"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { define } from "../src/van-element"; |
| 4 | + |
| 5 | +const { button, div } = van.tags; |
| 6 | + |
| 7 | +const mountFn = vi.fn(); |
| 8 | +const unmountFn = vi.fn(); |
| 9 | +const clickFn = vi.fn(); |
| 10 | + |
| 11 | +define("internals-test", ({ attr, $this, children, mount }) => { |
| 12 | + const attribute = attr("attribute", "default"); |
| 13 | + |
| 14 | + const count = van.state(0); |
| 15 | + |
| 16 | + mount(() => { |
| 17 | + mountFn(); |
| 18 | + return () => unmountFn(); |
| 19 | + }); |
| 20 | + |
| 21 | + const onClick = () => { |
| 22 | + clickFn(); |
| 23 | + count.val++; |
| 24 | + $this.dispatchEvent(new CustomEvent("count", { detail: count.val })); |
| 25 | + }; |
| 26 | + return [ |
| 27 | + div("Attribute: ", attribute), |
| 28 | + button({ onclick: () => onClick() }, "Count: ", count), |
| 29 | + children, |
| 30 | + ]; |
| 31 | +}); |
| 32 | + |
| 33 | +describe("check that a Van Element", async () => { |
| 34 | + function getComponent() { |
| 35 | + return document.body.querySelector("internals-test"); |
| 36 | + } |
| 37 | + |
| 38 | + function queryInShadow<K extends keyof HTMLElementTagNameMap>( |
| 39 | + selector: K |
| 40 | + ): HTMLElementTagNameMap[K] | null | undefined { |
| 41 | + return getComponent()?.shadowRoot?.querySelector(selector); |
| 42 | + } |
| 43 | + |
| 44 | + function mountComponent(name = "Peter", children = "") { |
| 45 | + document.body.innerHTML = `<internals-test attribute="${name}">${children}</internals-test>`; |
| 46 | + } |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + // Because VanJS uses setTimeout() to queue procedures in the event loop, we need to fake that otherwise the updates don't render. |
| 50 | + vi.useFakeTimers(); |
| 51 | + vi.clearAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("has VanJS behavior", async () => { |
| 55 | + mountComponent(); |
| 56 | + queryInShadow("button")?.click(); |
| 57 | + expect(clickFn).toHaveBeenCalled(); |
| 58 | + vi.runAllTimers(); |
| 59 | + expect(queryInShadow("button")?.textContent).toContain("1"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("has attribute reactivity", () => { |
| 63 | + mountComponent("Jack"); |
| 64 | + expect(queryInShadow("div")?.textContent).toContain("Jack"); |
| 65 | + getComponent()!.setAttribute("attribute", "John"); |
| 66 | + vi.runAllTimers(); |
| 67 | + expect(queryInShadow("div")?.textContent).toContain("John"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("mounts and unmounts properly", () => { |
| 71 | + expect(mountFn).not.toHaveBeenCalled(); |
| 72 | + mountComponent(); |
| 73 | + expect(mountFn).toHaveBeenCalled(); |
| 74 | + getComponent()!.remove(); |
| 75 | + expect(unmountFn).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("propagates events out", () => { |
| 79 | + const spyClick = vi.fn(); |
| 80 | + mountComponent(); |
| 81 | + getComponent()!.addEventListener("count", spyClick); |
| 82 | + queryInShadow("button")?.click(); |
| 83 | + expect(spyClick).toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("has a slot as children", () => { |
| 87 | + mountComponent("Bob", "Hi mom"); |
| 88 | + const slotted = queryInShadow("slot")?.assignedNodes(); |
| 89 | + expect(slotted?.[0].textContent).toContain("Hi mom"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments