-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGraphComponent.test.ts
More file actions
158 lines (127 loc) · 4.8 KB
/
Copy pathGraphComponent.test.ts
File metadata and controls
158 lines (127 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Graph } from "../../../graph";
import { GraphEventsDefinitions } from "../../../graphEvents";
import { Component } from "../../../lib/Component";
import { HitBox } from "../../../services/HitTest";
import { CanvasStyles } from "../../../services/canvasStyles";
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);
}
public resolveStyles(classes: string[]): Readonly<Record<string, unknown>> {
return this.resolveCanvasStyles(classes);
}
}
type TestSetup = {
component: TestGraphComponent;
graphOn: jest.Mock<() => void, Parameters<Graph["on"]>>;
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);
const hitTestRemove = jest.fn();
const canvasStyles = new CanvasStyles();
const fakeGraph = {
on: graphOn,
canvasStyles,
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,
getCameraScale: () => 1,
limitScaleEffect: (value: number) => value,
},
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");
});
it("applies runtime style classes in resolveCanvasStyles", () => {
const { component } = createTestComponent();
component.context.graph.canvasStyles.register({
selector: ".base.highlighted",
style: {
composite: {
opacity: 0.6,
},
},
});
const before = component.resolveStyles(["base"]);
expect(before.composite).toBeUndefined();
component.addStyleClass("highlighted");
const after = component.resolveStyles(["base"]);
expect((after.composite as { opacity?: number } | undefined)?.opacity).toBe(0.6);
});
});