File
Blob: tests/client/components/editor/controllers/toolbar-button.dom.test.tsx
| 1 | import { act } from "react"; |
| 2 | import { createRoot } from "react-dom/client"; |
| 3 | import { expect, it, vi } from "vitest"; |
| 4 | import { ToolbarButton } from "@/client/components/editor/controllers/toolbar-button"; |
| 5 | |
| 6 | it("activates once for pointer clicks and also supports keyboard clicks", () => { |
| 7 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 8 | const container = document.createElement("div"); |
| 9 | document.body.append(container); |
| 10 | const root = createRoot(container); |
| 11 | const activate = vi.fn(); |
| 12 | try { |
| 13 | act(() => |
| 14 | root.render( |
| 15 | <ToolbarButton title="Reset brightness" onActivate={activate}> |
| 16 | Reset |
| 17 | </ToolbarButton>, |
| 18 | ), |
| 19 | ); |
| 20 | const button = container.querySelector("button")!; |
| 21 | act(() => { |
| 22 | button.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, detail: 1 })); |
| 23 | button.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 1 })); |
| 24 | }); |
| 25 | expect(activate).toHaveBeenCalledTimes(1); |
| 26 | act(() => button.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }))); |
| 27 | expect(activate).toHaveBeenCalledTimes(2); |
| 28 | } finally { |
| 29 | act(() => root.unmount()); |
| 30 | container.remove(); |
| 31 | vi.unstubAllGlobals(); |
| 32 | } |
| 33 | }); |