|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, createElement, Profiler, useRef } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | +import { useTimelineShiftModifier } from "./useTimelineShiftModifier"; |
| 6 | + |
| 7 | +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 8 | + |
| 9 | +function Timeline() { |
| 10 | + const container = useRef<HTMLDivElement>(null); |
| 11 | + useTimelineShiftModifier(container); |
| 12 | + return createElement("div", { ref: container, "data-testid": "timeline" }); |
| 13 | +} |
| 14 | + |
| 15 | +const key = (type: string, name: string) => |
| 16 | + act(() => void window.dispatchEvent(new KeyboardEvent(type, { key: name }))); |
| 17 | + |
| 18 | +it("Shift marks the timeline for its cursor without rendering it", () => { |
| 19 | + const host = document.createElement("div"); |
| 20 | + const root = createRoot(host); |
| 21 | + let commits = 0; |
| 22 | + act(() => |
| 23 | + root.render( |
| 24 | + createElement(Profiler, { id: "t", onRender: () => (commits += 1) }, createElement(Timeline)), |
| 25 | + ), |
| 26 | + ); |
| 27 | + const timeline = host.querySelector("[data-testid=timeline]")!; |
| 28 | + const start = commits; |
| 29 | + key("keydown", "Shift"); |
| 30 | + expect(timeline.hasAttribute("data-shift-held")).toBe(true); |
| 31 | + key("keydown", "a"); |
| 32 | + expect(timeline.hasAttribute("data-shift-held")).toBe(true); |
| 33 | + key("keyup", "Shift"); |
| 34 | + expect(timeline.hasAttribute("data-shift-held")).toBe(false); |
| 35 | + key("keydown", "Shift"); |
| 36 | + act(() => void window.dispatchEvent(new Event("blur"))); |
| 37 | + expect(timeline.hasAttribute("data-shift-held")).toBe(false); |
| 38 | + expect(commits).toBe(start); |
| 39 | + act(() => root.unmount()); |
| 40 | +}); |
0 commit comments