-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Add helpers to subsribe events for GraphComponent #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+185
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
src/components/canvas/GraphComponent/GraphComponent.test.ts
This file contains hidden or 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,129 @@ | ||
| import { Graph } from "../../../graph"; | ||
| import { GraphEventsDefinitions } from "../../../graphEvents"; | ||
| import { Component } from "../../../lib/Component"; | ||
| import { HitBox } from "../../../services/HitTest"; | ||
|
|
||
| import { GraphComponent, GraphComponentContext } from "./index"; | ||
|
|
||
| class TestGraphComponent extends GraphComponent { | ||
| public getEntityId(): string { | ||
| return "test-id"; | ||
| } | ||
|
|
||
| public subscribeGraphEvent<EventName extends keyof GraphEventsDefinitions>( | ||
| eventName: EventName, | ||
| handler: GraphEventsDefinitions[EventName], | ||
| options?: AddEventListenerOptions | boolean | ||
| ): () => void { | ||
| return this.onGraphEvent(eventName, handler, options); | ||
| } | ||
|
|
||
| public subscribeRootEvent<K extends keyof HTMLElementEventMap>( | ||
| eventName: K, | ||
| handler: (this: HTMLElement, ev: HTMLElementEventMap[K]) => void, | ||
| options?: AddEventListenerOptions | boolean | ||
| ): () => void { | ||
| return this.onRootEvent(eventName, handler, options); | ||
| } | ||
| } | ||
|
|
||
| type TestSetup = { | ||
| component: TestGraphComponent; | ||
| graphOn: jest.Mock<() => () => void>; | ||
| graphOff: jest.Mock<void, []>; | ||
| rootEl: HTMLDivElement; | ||
| hitTestRemove: jest.Mock<void, [HitBox]>; | ||
| }; | ||
|
|
||
| function createTestComponent(root?: HTMLDivElement): TestSetup { | ||
| const graphOff = jest.fn(); | ||
| const graphOn = jest.fn<() => () => void, Parameters<Graph["on"]>>().mockReturnValue(graphOff); | ||
astandrik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const hitTestRemove = jest.fn(); | ||
| const fakeGraph = { | ||
| on: graphOn, | ||
| hitTest: { | ||
| remove: hitTestRemove, | ||
| update: jest.fn(), | ||
| }, | ||
| // The rest of Graph API is not needed for these tests | ||
| }; | ||
|
|
||
| const rootEl = root ?? document.createElement("div"); | ||
|
|
||
| const parent = new Component({}, undefined); | ||
|
|
||
| parent.setContext({ | ||
| graph: fakeGraph, | ||
| root: rootEl, | ||
| canvas: document.createElement("canvas"), | ||
| ctx: document.createElement("canvas").getContext("2d") as CanvasRenderingContext2D, | ||
| ownerDocument: document, | ||
| camera: { | ||
| isRectVisible: () => true, | ||
| }, | ||
| constants: {} as GraphComponentContext["constants"], | ||
| colors: {} as GraphComponentContext["colors"], | ||
| graphCanvas: document.createElement("canvas"), | ||
| layer: {} as GraphComponentContext["layer"], | ||
| affectsUsableRect: true, | ||
| } as unknown as GraphComponentContext); | ||
|
|
||
| const component = new TestGraphComponent({}, parent); | ||
|
|
||
| return { | ||
| component, | ||
| graphOn, | ||
| graphOff, | ||
| rootEl, | ||
| hitTestRemove, | ||
| }; | ||
| } | ||
|
|
||
| describe("GraphComponent event helpers", () => { | ||
| it("subscribes to graph events via onGraphEvent and cleans up on unmount", () => { | ||
| const { component, graphOn, graphOff } = createTestComponent(); | ||
|
|
||
| const handler = jest.fn(); | ||
|
|
||
| component.subscribeGraphEvent("camera-change", handler); | ||
|
|
||
| expect(graphOn).toHaveBeenCalledTimes(1); | ||
| expect(graphOn).toHaveBeenCalledWith("camera-change", handler, undefined); | ||
|
|
||
| Component.unmount(component); | ||
|
|
||
| expect(graphOff).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("subscribes to root DOM events via onRootEvent and cleans up on unmount", () => { | ||
| const rootEl = document.createElement("div"); | ||
| const addSpy = jest.spyOn(rootEl, "addEventListener"); | ||
| const removeSpy = jest.spyOn(rootEl, "removeEventListener"); | ||
|
|
||
| const { component } = createTestComponent(rootEl); | ||
|
|
||
| const handler = jest.fn((event: MouseEvent) => { | ||
| // Use event to keep types happy | ||
| expect(event).toBeInstanceOf(MouseEvent); | ||
| }); | ||
|
|
||
| component.subscribeRootEvent("click", handler); | ||
|
|
||
| expect(addSpy).toHaveBeenCalledTimes(1); | ||
| const [eventName, addListener] = addSpy.mock.calls[0]; | ||
| expect(eventName).toBe("click"); | ||
| expect(typeof addListener).toBe("function"); | ||
|
|
||
| const event = new MouseEvent("click"); | ||
| rootEl.dispatchEvent(event); | ||
| expect(handler).toHaveBeenCalledTimes(1); | ||
|
|
||
| Component.unmount(component); | ||
|
|
||
| expect(removeSpy).toHaveBeenCalledTimes(1); | ||
| const [removedEventName, removeListener] = removeSpy.mock.calls[0]; | ||
| expect(removedEventName).toBe("click"); | ||
| expect(typeof removeListener).toBe("function"); | ||
| }); | ||
| }); | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.