Skip to content

Commit

Permalink
test: add test case and snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Dec 12, 2024
1 parent fd5802b commit 567b6a6
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { CommonEvent, NodeEvent } from '@/src';
import { createGraph } from '@@/utils';

describe('behavior drag-node with click select', () => {
const createDemoGraph = async () => {
const graph = createGraph({
data: {
nodes: [
{ id: 'node-1', style: { x: 100, y: 100 } },
{ id: 'node-2', combo: 'combo-1', style: { x: 200, y: 100 } },
{ id: 'node-3', style: { x: 100, y: 200 } },
{ id: 'node-4', combo: 'combo-1', style: { x: 200, y: 200 } },
],
edges: [
{ source: 'node-1', target: 'node-2' },
{ source: 'node-2', target: 'node-4' },
{ source: 'node-1', target: 'node-3' },
{ source: 'node-3', target: 'node-4' },
],
combos: [{ id: 'combo-1' }],
},
node: { style: { size: 20 } },
edge: {
style: { endArrow: true },
},
behaviors: [{ type: 'drag-element' }, { type: 'click-select', multiple: true }],
});
await graph.render();
return graph;
};

it('drag unselected node', async () => {
const graph = await createDemoGraph();

graph.emit(NodeEvent.CLICK, { target: { id: 'node-1' }, targetType: 'node' });

await expect(graph).toMatchSnapshot(__filename, 'click-node-1');

// drag node-2
graph.emit(NodeEvent.DRAG_START, { target: { id: 'node-2' }, targetType: 'node' });
graph.emit(NodeEvent.DRAG, { dx: 20, dy: 20 });
graph.emit(NodeEvent.DRAG_END);

await expect(graph).toMatchSnapshot(__filename, 'drag-node-2');
});

it('drag selected node', async () => {
const graph = await createDemoGraph();

graph.emit(CommonEvent.KEY_DOWN, { key: 'shift' });
graph.emit(NodeEvent.CLICK, { target: { id: 'node-1' }, targetType: 'node' });
graph.emit(NodeEvent.CLICK, { target: { id: 'node-2' }, targetType: 'node' });
graph.emit(CommonEvent.KEY_UP, { key: 'shift' });

await expect(graph).toMatchSnapshot(__filename, 'click-node-1-node-2');

// drag node-2
graph.emit(NodeEvent.DRAG_START, { target: { id: 'node-2' }, targetType: 'node' });
graph.emit(NodeEvent.DRAG, { dx: 20, dy: 20 });
graph.emit(NodeEvent.DRAG_END);

await expect(graph).toMatchSnapshot(__filename, 'drag-node-1-node-2');
});
});
4 changes: 4 additions & 0 deletions packages/g6/__tests__/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { Controller } from 'lil-gui';
import GUI from 'lil-gui';
import { ComboEvent, CommonEvent, EdgeEvent, NodeEvent } from '../src';
import '../src/preset';
import * as demos from './demos';
import { createGraphCanvas } from './utils';

// inject
Object.assign(window, { NodeEvent, EdgeEvent, ComboEvent, CommonEvent });

type Options = {
Search: string;
Demo: string;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 567b6a6

Please sign in to comment.