|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "@testing-library/jest-dom/vitest"; |
| 3 | +import { act, cleanup, renderHook } from "@testing-library/react"; |
| 4 | +import { afterEach, expect, it, vi } from "vitest"; |
| 5 | +import { useReturnHighlight } from "./use-return-highlight"; |
| 6 | +import styles from "./Messages.module.css"; |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + cleanup(); |
| 10 | + document.body.replaceChildren(); |
| 11 | + vi.restoreAllMocks(); |
| 12 | + vi.unstubAllGlobals(); |
| 13 | +}); |
| 14 | + |
| 15 | +it("ignores other animation completions while its own cue and cancellation retire it", () => { |
| 16 | + const container = document.createElement("div"); |
| 17 | + container.innerHTML = |
| 18 | + '<section data-channel-timeline="alpha"><div data-message-id="root"><div data-layout="timeline"><span>Reaction</span></div></div></section>'; |
| 19 | + document.body.append(container); |
| 20 | + const scroller = container.querySelector("section"); |
| 21 | + const row = container.querySelector<HTMLElement>("[data-layout]"); |
| 22 | + const reaction = row?.querySelector("span"); |
| 23 | + const highlight = styles.returnHighlight; |
| 24 | + if (!scroller || !row || !reaction || !highlight) |
| 25 | + throw new Error("Missing fixture elements"); |
| 26 | + vi.spyOn(scroller, "getBoundingClientRect").mockReturnValue( |
| 27 | + new DOMRect(0, 0, 300, 300), |
| 28 | + ); |
| 29 | + vi.spyOn(row, "getBoundingClientRect").mockReturnValue( |
| 30 | + new DOMRect(0, 50, 300, 50), |
| 31 | + ); |
| 32 | + let frame: FrameRequestCallback | undefined; |
| 33 | + vi.stubGlobal("requestAnimationFrame", (next: FrameRequestCallback) => { |
| 34 | + frame = next; |
| 35 | + return 1; |
| 36 | + }); |
| 37 | + vi.stubGlobal("cancelAnimationFrame", () => { |
| 38 | + frame = undefined; |
| 39 | + }); |
| 40 | + const ref = { current: container }; |
| 41 | + const h = renderHook(({ channel }) => useReturnHighlight(ref, channel), { |
| 42 | + initialProps: { channel: "alpha" }, |
| 43 | + }); |
| 44 | + const paint = () => |
| 45 | + act(() => { |
| 46 | + h.result.current("root"); |
| 47 | + frame?.(0); |
| 48 | + }); |
| 49 | + const finish = (target: Element, pseudoElement = "") => { |
| 50 | + const event = new Event("animationend", { bubbles: true }); |
| 51 | + Object.defineProperty(event, "pseudoElement", { value: pseudoElement }); |
| 52 | + act(() => target.dispatchEvent(event)); |
| 53 | + }; |
| 54 | + paint(); |
| 55 | + expect(row).toHaveClass(highlight); |
| 56 | + finish(reaction, "::after"); |
| 57 | + finish(row); |
| 58 | + finish(row, "::before"); |
| 59 | + expect(row).toHaveClass(highlight); |
| 60 | + finish(row, "::after"); |
| 61 | + expect(row).not.toHaveClass(highlight); |
| 62 | + paint(); |
| 63 | + act(() => h.result.current(undefined)); |
| 64 | + expect(row).not.toHaveClass(highlight); |
| 65 | + paint(); |
| 66 | + h.rerender({ channel: "beta" }); |
| 67 | + expect(row).not.toHaveClass(highlight); |
| 68 | + h.rerender({ channel: "alpha" }); |
| 69 | + paint(); |
| 70 | + h.unmount(); |
| 71 | + expect(row).not.toHaveClass(highlight); |
| 72 | +}); |
0 commit comments