This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mouse events to plugin API (#280)
* add mouse event api * clear code * refactor * refactor * fix type * update type * refactor: update type * add screen x y to mouse event props * add test for useEngineRef * refactor: add empty line between functions * refactor: update type definition * fix: type error * refactor: rename type
- Loading branch information
Showing
10 changed files
with
467 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/components/molecules/Visualizer/Engine/Cesium/useEngineRef.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import type { Viewer as CesiumViewer } from "cesium"; | ||
import { useRef } from "react"; | ||
import type { CesiumComponentRef } from "resium"; | ||
import { vi, expect, test } from "vitest"; | ||
|
||
import { EngineRef } from "../ref"; | ||
|
||
import useEngineRef from "./useEngineRef"; | ||
|
||
const fn = vi.fn(e => e); | ||
const props = { x: 1, y: 1 }; | ||
|
||
test("engine should be cesium", () => { | ||
const { result } = renderHook(() => { | ||
const cesium = useRef<CesiumComponentRef<CesiumViewer>>(null); | ||
const engineRef = useRef<EngineRef>(null); | ||
useEngineRef(engineRef, cesium); | ||
return engineRef; | ||
}); | ||
expect(result.current.current?.name).toBe("cesium"); | ||
}); | ||
|
||
test("bind mouse events", () => { | ||
const { result } = renderHook(() => { | ||
const cesium = useRef<CesiumComponentRef<CesiumViewer>>(null); | ||
const engineRef = useRef<EngineRef>(null); | ||
useEngineRef(engineRef, cesium); | ||
return engineRef; | ||
}); | ||
|
||
result.current.current?.onClick(fn); | ||
expect(result.current.current?.mouseEventCallbacks.click).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.click?.(props); | ||
expect(fn).toHaveBeenCalledTimes(1); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onDoubleClick(fn); | ||
expect(result.current.current?.mouseEventCallbacks.doubleclick).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.doubleclick?.(props); | ||
expect(fn).toHaveBeenCalledTimes(2); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMouseDown(fn); | ||
expect(result.current.current?.mouseEventCallbacks.mousedown).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.mousedown?.(props); | ||
expect(fn).toHaveBeenCalledTimes(3); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMouseUp(fn); | ||
expect(result.current.current?.mouseEventCallbacks.mouseup).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.mouseup?.(props); | ||
expect(fn).toHaveBeenCalledTimes(4); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onRightClick(fn); | ||
expect(result.current.current?.mouseEventCallbacks.rightclick).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.rightclick?.(props); | ||
expect(fn).toHaveBeenCalledTimes(5); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onRightDown(fn); | ||
expect(result.current.current?.mouseEventCallbacks.rightdown).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.rightdown?.(props); | ||
expect(fn).toHaveBeenCalledTimes(6); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onRightUp(fn); | ||
expect(result.current.current?.mouseEventCallbacks.rightup).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.rightup?.(props); | ||
expect(fn).toHaveBeenCalledTimes(7); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMiddleClick(fn); | ||
expect(result.current.current?.mouseEventCallbacks.middleclick).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.middleclick?.(props); | ||
expect(fn).toHaveBeenCalledTimes(8); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMiddleDown(fn); | ||
expect(result.current.current?.mouseEventCallbacks.middledown).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.middledown?.(props); | ||
expect(fn).toHaveBeenCalledTimes(9); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMiddleUp(fn); | ||
expect(result.current.current?.mouseEventCallbacks.middleup).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.middleup?.(props); | ||
expect(fn).toHaveBeenCalledTimes(10); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMouseMove(fn); | ||
expect(result.current.current?.mouseEventCallbacks.mousemove).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.mousemove?.(props); | ||
expect(fn).toHaveBeenCalledTimes(11); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMouseEnter(fn); | ||
expect(result.current.current?.mouseEventCallbacks.mouseenter).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.mouseenter?.(props); | ||
expect(fn).toHaveBeenCalledTimes(12); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onMouseLeave(fn); | ||
expect(result.current.current?.mouseEventCallbacks.mouseleave).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.mouseleave?.(props); | ||
expect(fn).toHaveBeenCalledTimes(13); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onPinchStart(fn); | ||
expect(result.current.current?.mouseEventCallbacks.pinchstart).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.pinchstart?.(props); | ||
expect(fn).toHaveBeenCalledTimes(14); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onPinchEnd(fn); | ||
expect(result.current.current?.mouseEventCallbacks.pinchend).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.pinchend?.(props); | ||
expect(fn).toHaveBeenCalledTimes(15); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onPinchMove(fn); | ||
expect(result.current.current?.mouseEventCallbacks.pinchmove).toBe(fn); | ||
|
||
result.current.current?.mouseEventCallbacks.pinchmove?.(props); | ||
expect(fn).toHaveBeenCalledTimes(16); | ||
expect(fn).toHaveBeenCalledWith(props); | ||
|
||
result.current.current?.onWheel(fn); | ||
expect(result.current.current?.mouseEventCallbacks.wheel).toBe(fn); | ||
|
||
const wheelProps = { delta: 1 }; | ||
result.current.current?.mouseEventCallbacks.wheel?.(wheelProps); | ||
expect(fn).toHaveBeenCalledTimes(17); | ||
expect(fn).toHaveBeenCalledWith(wheelProps); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.