-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Adds some mouse captor unit tests
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { userEvent } from "@vitest/browser/context"; | ||
import Graph from "graphology"; | ||
import { SerializedGraph } from "graphology-types"; | ||
import Sigma from "sigma"; | ||
import { createElement } from "sigma/utils"; | ||
import { afterEach, beforeEach, describe, expect, test } from "vitest"; | ||
|
||
interface SigmaTestContext { | ||
sigma: Sigma; | ||
graph: Graph; | ||
container: HTMLDivElement; | ||
} | ||
|
||
function wait(timeout: number): Promise<void> { | ||
return new Promise<void>((resolve) => setTimeout(resolve, timeout)); | ||
} | ||
|
||
const STAGE_WIDTH = 200; | ||
const STAGE_HEIGHT = 400; | ||
|
||
const GRAPH: Pick<SerializedGraph, "nodes" | "edges"> = { | ||
nodes: [ | ||
{ key: "n1", attributes: { x: 0, y: 0, size: 5 } }, | ||
{ key: "n2", attributes: { x: 50, y: 50, size: 5 } }, | ||
], | ||
edges: [{ source: "n1", target: "n2" }], | ||
}; | ||
|
||
beforeEach<SigmaTestContext>(async (context) => { | ||
const graph = new Graph(); | ||
graph.import(GRAPH); | ||
const container = createElement("div", { width: `${STAGE_WIDTH}px`, height: `${STAGE_HEIGHT}px` }) as HTMLDivElement; | ||
document.body.append(container); | ||
|
||
context.sigma = new Sigma(graph, container, { | ||
zoomDuration: 30, | ||
inertiaDuration: 30, | ||
doubleClickZoomingDuration: 30, | ||
}); | ||
context.graph = graph; | ||
context.container = container; | ||
}); | ||
|
||
afterEach<SigmaTestContext>(async ({ sigma }) => { | ||
sigma.kill(); | ||
sigma.getContainer().remove(); | ||
}); | ||
|
||
describe("Sigma mouse management", () => { | ||
test<SigmaTestContext>("it should zoom to the center when user double-clicks in the center", async ({ | ||
sigma, | ||
container, | ||
}) => { | ||
const position = { x: STAGE_WIDTH / 2, y: STAGE_HEIGHT / 2 }; | ||
|
||
await userEvent.dblClick(container, { position }); | ||
await wait(sigma.getSetting("doubleClickZoomingDuration") * 1.1); | ||
|
||
expect(sigma.getCamera().getState()).toEqual({ | ||
x: 0.5, | ||
y: 0.5, | ||
angle: 0, | ||
ratio: 1 / sigma.getSetting("doubleClickZoomingRatio"), | ||
}); | ||
}); | ||
|
||
test<SigmaTestContext>("it should zoom to the mouse position when user double-clicks in the center", async ({ | ||
sigma, | ||
container, | ||
}) => { | ||
const position = { x: STAGE_WIDTH * 0.2, y: STAGE_HEIGHT * 0.7 }; | ||
const originalMouseGraphCoordinates = sigma.viewportToFramedGraph(position); | ||
|
||
await userEvent.dblClick(container, { position }); | ||
await wait(sigma.getSetting("doubleClickZoomingDuration") * 1.1); | ||
|
||
const newMouseGraphCoordinates = sigma.viewportToFramedGraph(position); | ||
(["x", "y"] as const).forEach((key) => | ||
expect(newMouseGraphCoordinates[key]).toBeCloseTo(originalMouseGraphCoordinates[key], 6), | ||
); | ||
}); | ||
}); |