Skip to content

Commit

Permalink
[test] Makes some existing tests stronger
Browse files Browse the repository at this point in the history
  • Loading branch information
jacomyal committed Oct 29, 2024
1 parent b814803 commit f8a1f78
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<br />

![Sigma.js](packages/website/static/img/logo-sigma-text.png)
![Sigma.js](packages/website/static/img/logo-sigma-text.svg)

**[Website](https://www.sigmajs.org/)** | **[Documentation](https://www.sigmajs.org/docs)** | **[Storybook](https://www.sigmajs.org/storybook)** | <strong><a rel="me" href="https://vis.social/@sigmajs">Mastodon</a></strong>

Expand Down
2 changes: 1 addition & 1 deletion packages/storybook/stories/0-docs/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meta } from "@storybook/addon-docs";

<Meta title="Introduction" />

![Sigma.js](https://raw.githubusercontent.com/jacomyal/sigma.js/main/packages/website/static/img/logo-sigma-text.png)
![Sigma.js](https://raw.githubusercontent.com/jacomyal/sigma.js/main/packages/website/static/img/logo-sigma-text.svg)

**[Website](https://www.sigmajs.org/)** | **[Documentation](https://www.sigmajs.org/docs)** | **[GitHub](https://github.com/jacomyal/sigma.js)** | <strong><a rel="me" href="https://vis.social/@sigmajs">Mastodon</a></strong>

Expand Down
22 changes: 12 additions & 10 deletions packages/test/unit/sigma/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ describe("Camera", function () {
const camera = new Camera();
const targetState1 = { ...camera.getState(), x: 1 };
const targetState2 = { ...camera.getState(), x: 2 };
const duration = 50;
const delay = 10;
const duration = 500;
const delay = 50;

const t0 = Date.now();
await Promise.all([
camera.animate(targetState1, { duration }),
(async () => {
await wait(10);
(async (): Promise<void> => {
await wait(delay);
await camera.animate(targetState2, { duration: 0 });
})(),
]);
Expand All @@ -256,19 +256,20 @@ describe("Camera", function () {
expect(camera.getState()).toEqual(targetState2);
// Time measures are very rough at this scale, we just want to check that t1 - t0 (the actual spent time)
// is basically closer to delay than to duration:
expect(Math.abs(t1 - t0 - delay)).toBeLessThan(delay);
const spentTime = t1 - t0;
expect(Math.abs(spentTime - delay)).toBeLessThan(Math.abs(spentTime - duration));
});

test("it should resolve promises when animation is interrupted by a new animation (using the shortcut methods).", async function () {
const camera = new Camera();
const duration = 50;
const delay = 10;
const duration = 500;
const delay = 50;

const t0 = Date.now();
await Promise.all([
camera.animatedZoom({ duration }),
(async () => {
await wait(10);
(async (): Promise<void> => {
await wait(delay);
await camera.animatedReset({ duration: 0 });
})(),
]);
Expand All @@ -277,7 +278,8 @@ describe("Camera", function () {
expect(camera.ratio).toEqual(1);
// Time measures are very rough at this scale, we just want to check that t1 - t0 (the actual spent time)
// is basically closer to delay than to duration:
expect(Math.abs(t1 - t0 - delay)).toBeLessThan(delay);
const spentTime = t1 - t0;
expect(Math.abs(spentTime - delay)).toBeLessThan(Math.abs(spentTime - duration));
});
});
});
56 changes: 29 additions & 27 deletions packages/test/unit/sigma/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,38 @@ afterEach<SigmaTestContext>(async ({ sigma }) => {
});

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 };
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);
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"),
});
});
expect(sigma.getCamera().getState()).toEqual({
x: 0.5,
y: 0.5,
angle: 0,
ratio: 1 / sigma.getSetting("doubleClickZoomingRatio"),
});
},
{ retry: 2 },
);

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);
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);
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),
);
});
const newMouseGraphCoordinates = sigma.viewportToFramedGraph(position);
(["x", "y"] as const).forEach((key) =>
expect(newMouseGraphCoordinates[key]).toBeCloseTo(originalMouseGraphCoordinates[key], 6),
);
},
{ retry: 2 },
);
});
116 changes: 116 additions & 0 deletions packages/website/static/img/logo-sigma-text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f8a1f78

Please sign in to comment.