Skip to content

Commit

Permalink
test: add range tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Sep 3, 2024
1 parent 668a367 commit c0fb740
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
20 changes: 20 additions & 0 deletions packages/g6/__tests__/unit/behaviors/drag-canvas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,24 @@ describe('behavior drag canvas', () => {

await expect(graph).toMatchSnapshot(__filename, 'drag-on-element');
});

it('range', () => {
graph.updateBehavior({ key: 'drag-canvas', trigger: 'drag', direction: 'both', range: 0.5 });

const emitDragEvent = (dx: number, dy: number, count: number) => {
for (let i = 0; i < count; i++) {
dispatchCanvasEvent(graph, CommonEvent.DRAG_START, { targetType: 'canvas' });
dispatchCanvasEvent(graph, CommonEvent.DRAG, { movement: { x: dx, y: dy }, targetType: 'canvas' });
dispatchCanvasEvent(graph, CommonEvent.DRAG_END);
}
};

const [canvasWidth, canvasHeight] = graph.getCanvas().getSize();
emitDragEvent(10, 0, 60);
expect(graph.getPosition()[0]).toBeCloseTo(canvasWidth / 2);
emitDragEvent(-10, 0, 60);
expect(graph.getPosition()[0]).toBeCloseTo(-canvasWidth / 2);
emitDragEvent(0, -10, 60);
expect(graph.getPosition()[0]).toBeCloseTo(-canvasHeight / 2);
});
});
28 changes: 22 additions & 6 deletions packages/g6/__tests__/unit/behaviors/scroll-canvas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ describe('behavior scroll canvas', () => {

it('direction', async () => {
setBehavior({ direction: 'x' });
const [x, y] = graph.getPosition();
let [x, y] = graph.getPosition();
emitWheelEvent({ deltaX: -10, deltaY: -10 });
expect(graph.getPosition()).toBeCloseTo([x + 10, y]);

setBehavior({ direction: 'y' });
[x, y] = graph.getPosition();
emitWheelEvent({ deltaX: -10, deltaY: -10 });
expect(graph.getPosition()).toBeCloseTo([x, y + 10]);

setBehavior({ direction: undefined });
});

Expand Down Expand Up @@ -99,11 +104,22 @@ describe('behavior scroll canvas', () => {
});

it('range', () => {
graph.setBehaviors((behavior) => [...behavior, { ...shortcutScrollCanvasOptions, range: 0 }]);
const [x, y] = graph.getPosition();
graph.emit(CommonEvent.KEY_DOWN, { key: 'ArrowRight' });
graph.emit(CommonEvent.KEY_UP, { key: 'ArrowRight' });
expect(graph.getPosition()).toBeCloseTo([x, y]);
graph.setBehaviors((behavior) => [...behavior, { ...shortcutScrollCanvasOptions, range: 0.5 }]);

const emitArrow = (key: 'ArrowRight' | 'ArrowLeft' | 'ArrowUp' | 'ArrowDown', count: number) => {
for (let i = 0; i < count; i++) {
graph.emit(CommonEvent.KEY_DOWN, { key });
graph.emit(CommonEvent.KEY_UP, { key });
}
};

const [canvasWidth, canvasHeight] = graph.getCanvas().getSize();
emitArrow('ArrowRight', 50);
expect(graph.getPosition()[0]).toBeCloseTo(canvasWidth / 2);
emitArrow('ArrowLeft', 50);
expect(graph.getPosition()[0]).toBeCloseTo(-canvasWidth / 2);
emitArrow('ArrowUp', 50);
expect(graph.getPosition()[1]).toBeCloseTo(-canvasHeight / 2);
});

it('destroy', () => {
Expand Down

0 comments on commit c0fb740

Please sign in to comment.