|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { createRoot, createSignal } from "solid-js" |
| 3 | +import { createPresence } from "../src/runtime/presence" |
| 4 | + |
| 5 | +const cleanups: Array<() => void> = [] |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + cleanups.splice(0).forEach((cleanup) => cleanup()) |
| 9 | +}) |
| 10 | + |
| 11 | +function setup(input: { show: boolean; element?: boolean }) { |
| 12 | + const element = document.createElement("div") |
| 13 | + // Browsers compute "none" without an animation; happy-dom computes an empty string. |
| 14 | + element.style.animationName = "none" |
| 15 | + document.body.append(element) |
| 16 | + |
| 17 | + return createRoot((dispose) => { |
| 18 | + const [show, setShow] = createSignal(input.show) |
| 19 | + const [target, setTarget] = createSignal<HTMLElement | null>(input.element === false ? null : element) |
| 20 | + const presence = createPresence({ show, element: target }) |
| 21 | + |
| 22 | + cleanups.push(() => { |
| 23 | + dispose() |
| 24 | + element.remove() |
| 25 | + }) |
| 26 | + |
| 27 | + return { element, presence, setShow, setTarget } |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +function animate(element: HTMLElement, name: string) { |
| 32 | + element.style.animationName = name |
| 33 | + element.dispatchEvent(new AnimationEvent("animationstart", { animationName: name })) |
| 34 | +} |
| 35 | + |
| 36 | +describe("createPresence", () => { |
| 37 | + test("starts from the initial show value", () => { |
| 38 | + expect(setup({ show: true }).presence.state()).toBe("present") |
| 39 | + expect(setup({ show: false }).presence.state()).toBe("hidden") |
| 40 | + expect(setup({ show: false }).presence.present()).toBe(false) |
| 41 | + }) |
| 42 | + |
| 43 | + test("enters at once, with or without an enter animation", () => { |
| 44 | + const view = setup({ show: false }) |
| 45 | + |
| 46 | + view.setShow(true) |
| 47 | + expect(view.presence.state()).toBe("present") |
| 48 | + expect(view.presence.present()).toBe(true) |
| 49 | + }) |
| 50 | + |
| 51 | + test("hides at once without an exit animation, as under reduced motion", () => { |
| 52 | + const view = setup({ show: true }) |
| 53 | + |
| 54 | + view.setShow(false) |
| 55 | + expect(view.presence.state()).toBe("hidden") |
| 56 | + expect(view.presence.present()).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + test("stays present while the exit animation runs and hides when it ends", () => { |
| 60 | + const view = setup({ show: true }) |
| 61 | + animate(view.element, "enter") |
| 62 | + |
| 63 | + view.element.style.animationName = "exit" |
| 64 | + view.setShow(false) |
| 65 | + expect(view.presence.state()).toBe("hiding") |
| 66 | + expect(view.presence.present()).toBe(true) |
| 67 | + |
| 68 | + view.element.dispatchEvent(new AnimationEvent("animationstart", { animationName: "exit" })) |
| 69 | + view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "enter" })) |
| 70 | + expect(view.presence.state()).toBe("hiding") |
| 71 | + |
| 72 | + view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" })) |
| 73 | + expect(view.presence.state()).toBe("hidden") |
| 74 | + }) |
| 75 | + |
| 76 | + test("hides when the exit animation is cancelled", () => { |
| 77 | + const view = setup({ show: true }) |
| 78 | + view.element.style.animationName = "exit" |
| 79 | + view.setShow(false) |
| 80 | + |
| 81 | + view.element.dispatchEvent(new AnimationEvent("animationcancel", { animationName: "exit" })) |
| 82 | + expect(view.presence.state()).toBe("hidden") |
| 83 | + }) |
| 84 | + |
| 85 | + test("ignores animation events from children", () => { |
| 86 | + const view = setup({ show: true }) |
| 87 | + const child = document.createElement("span") |
| 88 | + view.element.append(child) |
| 89 | + view.element.style.animationName = "exit" |
| 90 | + view.setShow(false) |
| 91 | + |
| 92 | + child.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit", bubbles: true })) |
| 93 | + expect(view.presence.state()).toBe("hiding") |
| 94 | + }) |
| 95 | + |
| 96 | + test("hides at once when the running animation did not change", () => { |
| 97 | + const view = setup({ show: true }) |
| 98 | + animate(view.element, "pulse") |
| 99 | + |
| 100 | + view.setShow(false) |
| 101 | + expect(view.presence.state()).toBe("hidden") |
| 102 | + }) |
| 103 | + |
| 104 | + test("hides at once when the element is not displayed", () => { |
| 105 | + const view = setup({ show: true }) |
| 106 | + view.element.style.animationName = "exit" |
| 107 | + view.element.style.display = "none" |
| 108 | + |
| 109 | + view.setShow(false) |
| 110 | + expect(view.presence.state()).toBe("hidden") |
| 111 | + }) |
| 112 | + |
| 113 | + test("hides at once without an element", () => { |
| 114 | + const view = setup({ show: true, element: false }) |
| 115 | + |
| 116 | + view.setShow(false) |
| 117 | + expect(view.presence.state()).toBe("hidden") |
| 118 | + }) |
| 119 | + |
| 120 | + test("comes back when shown again during the exit animation", () => { |
| 121 | + const view = setup({ show: true }) |
| 122 | + view.element.style.animationName = "exit" |
| 123 | + view.setShow(false) |
| 124 | + |
| 125 | + view.element.style.animationName = "enter" |
| 126 | + view.setShow(true) |
| 127 | + expect(view.presence.state()).toBe("present") |
| 128 | + |
| 129 | + view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" })) |
| 130 | + expect(view.presence.state()).toBe("present") |
| 131 | + }) |
| 132 | + |
| 133 | + test("runs an exit-only animation again on every hide", () => { |
| 134 | + const view = setup({ show: true }) |
| 135 | + |
| 136 | + const hide = () => { |
| 137 | + view.element.style.animationName = "exit" |
| 138 | + view.setShow(false) |
| 139 | + expect(view.presence.state()).toBe("hiding") |
| 140 | + view.element.dispatchEvent(new AnimationEvent("animationstart", { animationName: "exit" })) |
| 141 | + view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" })) |
| 142 | + expect(view.presence.state()).toBe("hidden") |
| 143 | + } |
| 144 | + |
| 145 | + hide() |
| 146 | + view.element.style.animationName = "none" |
| 147 | + view.setShow(true) |
| 148 | + hide() |
| 149 | + }) |
| 150 | + |
| 151 | + test("stops listening to a replaced element", () => { |
| 152 | + const view = setup({ show: true }) |
| 153 | + const previous = view.element |
| 154 | + const next = document.createElement("div") |
| 155 | + document.body.append(next) |
| 156 | + view.setTarget(next) |
| 157 | + next.style.animationName = "exit" |
| 158 | + view.setShow(false) |
| 159 | + |
| 160 | + previous.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" })) |
| 161 | + expect(view.presence.state()).toBe("hiding") |
| 162 | + |
| 163 | + next.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" })) |
| 164 | + expect(view.presence.state()).toBe("hidden") |
| 165 | + next.remove() |
| 166 | + }) |
| 167 | +}) |
0 commit comments