Skip to content

Commit 41baea8

Browse files
authored
Merge pull request #10 from VisActor/feat/improve-test-coverage
test: improve vgraph coverage
2 parents 8139a93 + cc6815f commit 41baea8

33 files changed

Lines changed: 4008 additions & 137 deletions
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { Graph, attachableDragNode } from "../../src";
2+
3+
function createGraph() {
4+
const div = document.createElement("div");
5+
const graph = new Graph({
6+
container: div,
7+
width: 800,
8+
height: 600,
9+
setDefaultNode() {
10+
return {
11+
width: 60,
12+
height: 30,
13+
};
14+
},
15+
});
16+
17+
graph.data({
18+
nodes: [
19+
{ id: "parent", x: 100, y: 100 },
20+
{ id: "child", x: 200, y: 100 },
21+
{ id: "candidate", x: 260, y: 100 },
22+
],
23+
edges: [{ id: "parent-child", source: "parent", target: "child" }],
24+
});
25+
26+
return graph;
27+
}
28+
29+
describe("src/behaviors/attachable_drag_node.ts", () => {
30+
afterEach(() => {
31+
document.body.innerHTML = "";
32+
});
33+
34+
it("should expose mouse and global events", () => {
35+
expect(attachableDragNode.getEvents()).toEqual({
36+
"node:mousedown": "onMouseDown",
37+
"node:mouseenter": "onMouseEnter",
38+
"node:mouseleave": "onMouseLeave",
39+
mousemove: "onMouseMove",
40+
});
41+
expect(attachableDragNode.getGlobalEvents()).toEqual({
42+
mouseup: "onMouseUp",
43+
});
44+
});
45+
46+
it("should ignore mouse movement when trigger guard returns false", () => {
47+
const graph = createGraph();
48+
const child = graph.getNodeById("child");
49+
50+
graph.addBehavior(attachableDragNode, {
51+
autoTranslate: false,
52+
shouldTrigger: jest.fn(() => false),
53+
});
54+
55+
graph.emit("node:mousedown", {
56+
target: child,
57+
relatedTarget: child.getKeyShape(),
58+
clientX: 0,
59+
clientY: 0,
60+
});
61+
graph.emit("mousemove", {
62+
target: child,
63+
clientX: 10,
64+
clientY: 10,
65+
});
66+
67+
const behavior = graph.getBehavior("attachableDragNode")!;
68+
expect(behavior.dragging).toBe(false);
69+
expect(behavior.lastPositions).toBeNull();
70+
71+
graph.removeBehavior(attachableDragNode);
72+
graph.destroy();
73+
});
74+
75+
it("should drag with delegate and attach to the closest parent", () => {
76+
const graph = createGraph();
77+
const child = graph.getNodeById("child");
78+
const candidate = graph.getNodeById("candidate");
79+
const onDrop = jest.fn();
80+
const onDrag = jest.fn();
81+
const onAttachNode = jest.fn((node) => node);
82+
83+
graph.addBehavior(attachableDragNode, {
84+
autoTranslate: false,
85+
delegate: true,
86+
onDrop,
87+
onDrag,
88+
onAttachNode,
89+
delegateStyles: {
90+
opacity: 0.4,
91+
},
92+
});
93+
94+
graph.emit("node:mousedown", {
95+
target: child,
96+
relatedTarget: child.getKeyShape(),
97+
clientX: 0,
98+
clientY: 0,
99+
});
100+
graph.emit("mousemove", {
101+
target: child,
102+
clientX: 20,
103+
clientY: 0,
104+
});
105+
106+
const behavior = graph.getBehavior("attachableDragNode")!;
107+
expect(behavior.dragging).toBe(true);
108+
expect(behavior.shape).toBeDefined();
109+
expect(behavior.shape.get("opacity")).toBe(0.4);
110+
expect(onDrag).toHaveBeenCalledWith(child, 20, 0);
111+
112+
child.updatePosition(250, 100);
113+
behavior.updateParent();
114+
115+
expect(onAttachNode).toHaveBeenCalled();
116+
expect(behavior.parent).toBe(candidate);
117+
expect(behavior.tempEdge.isVisible()).toBe(true);
118+
119+
behavior.onMouseUp({
120+
target: child,
121+
} as any);
122+
123+
expect(onDrop).toHaveBeenCalledWith(child, candidate, { target: child });
124+
expect(behavior.dragging).toBe(false);
125+
expect(behavior.shape).toBeNull();
126+
127+
graph.removeBehavior(attachableDragNode);
128+
graph.destroy();
129+
});
130+
131+
it("should drag without delegate, restore position on rejected drop and clear timers", () => {
132+
const graph = createGraph();
133+
const child = graph.getNodeById("child");
134+
const edge = graph.getEdgeById("parent-child");
135+
136+
graph.addBehavior(attachableDragNode, {
137+
autoTranslate: false,
138+
delegate: false,
139+
shouldDrop: jest.fn(() => false),
140+
});
141+
142+
graph.emit("node:mousedown", {
143+
target: child,
144+
relatedTarget: child.getKeyShape(),
145+
clientX: 0,
146+
clientY: 0,
147+
});
148+
graph.emit("mousemove", {
149+
target: child,
150+
clientX: 20,
151+
clientY: 10,
152+
});
153+
154+
const behavior = graph.getBehavior("attachableDragNode")!;
155+
expect(behavior.dragging).toBe(true);
156+
expect(edge.isVisible()).toBe(false);
157+
expect(child.get("x")).toBe(220);
158+
expect(child.get("y")).toBe(110);
159+
160+
behavior.interval = setInterval(() => undefined, 100);
161+
behavior.onMouseUp({
162+
target: child,
163+
} as any);
164+
165+
expect(behavior.interval).toBeNull();
166+
expect(child.get("x")).toBe(200);
167+
expect(child.get("y")).toBe(100);
168+
expect(edge.isVisible()).toBe(true);
169+
expect(behavior.dragging).toBe(false);
170+
171+
graph.removeBehavior(attachableDragNode);
172+
graph.destroy();
173+
});
174+
});

0 commit comments

Comments
 (0)