1+ import userEvent from '@testing-library/user-event' ;
2+
13import { Canvas } from '@/components/canvas/canvas' ;
24import { render , screen } from '@/mocks/testing-utils' ;
35import { EMPLOYEES_NODE , ORDERS_NODE } from '@/mocks/datasets/nodes' ;
46import { EMPLOYEES_TO_ORDERS_EDGE } from '@/mocks/datasets/edges' ;
57
8+ const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
9+
610describe ( 'canvas' , ( ) => {
711 it ( 'Should have elements on the canvas' , ( ) => {
812 render (
@@ -15,4 +19,90 @@ describe('canvas', () => {
1519 expect ( screen . getByText ( 'orders' ) ) . toBeInTheDocument ( ) ;
1620 expect ( screen . getByText ( 'employees' ) ) . toBeInTheDocument ( ) ;
1721 } ) ;
22+
23+ describe ( 'callbacks' , ( ) => {
24+ it ( 'Should call onNodeContextMenu when a node is right-clicked' , async ( ) => {
25+ const onNodeContextMenu = vi . fn ( ) ;
26+ render (
27+ < Canvas
28+ title = { 'MongoDB Diagram' }
29+ nodes = { [ ORDERS_NODE , EMPLOYEES_NODE ] }
30+ edges = { [ EMPLOYEES_TO_ORDERS_EDGE ] }
31+ onNodeContextMenu = { onNodeContextMenu }
32+ /> ,
33+ ) ;
34+ const orders = screen . getByText ( 'orders' ) ;
35+ await userEvent . pointer ( { target : orders , keys : '[MouseRight]' } ) ;
36+ expect ( onNodeContextMenu ) . toHaveBeenCalledTimes ( 1 ) ;
37+ expect ( onNodeContextMenu . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( ORDERS_NODE ) ;
38+ } ) ;
39+
40+ // Disabled because the drag simulation is not working. Leaving this here in case someone figures it out.
41+ // From Compass e2e tests we know that react flow requires the drag to take a certain time
42+ // (with webdriverio we were able to add not only pauses but also set the duration of the drag itself).
43+ // Test onSelectionDragStop should be added as well.
44+ it . skip ( 'Should call the drag callbacks when a node is being dragged' , async ( ) => {
45+ const onNodeDrag = vi . fn ( ) ;
46+ const onNodeDragStop = vi . fn ( ) ;
47+ render (
48+ < Canvas
49+ title = { 'MongoDB Diagram' }
50+ nodes = { [ ORDERS_NODE , EMPLOYEES_NODE ] }
51+ edges = { [ EMPLOYEES_TO_ORDERS_EDGE ] }
52+ onNodeDrag = { onNodeDrag }
53+ onNodeDragStop = { onNodeDragStop }
54+ /> ,
55+ ) ;
56+ const orders = screen . getByText ( 'orders' ) ;
57+ const rect = orders . getBoundingClientRect ( ) ;
58+ await userEvent . pointer ( { target : orders , keys : '[MouseLeft>]' } ) ;
59+ await sleep ( 100 ) ;
60+ await userEvent . pointer ( { coords : { x : rect . x + 120 , y : rect . y + 120 } } ) ;
61+ await sleep ( 100 ) ;
62+ await userEvent . pointer ( { coords : { x : rect . x + 130 , y : rect . y + 130 } } ) ;
63+ await sleep ( 100 ) ;
64+ await userEvent . pointer ( { keys : '[/MouseLeft]' } ) ;
65+ const { position : _position , ...nodeWithoutPosition } = ORDERS_NODE ;
66+
67+ expect ( onNodeDrag ) . toHaveBeenCalled ( ) ;
68+ expect ( onNodeDrag . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( nodeWithoutPosition ) ;
69+
70+ expect ( onNodeDragStop ) . toHaveBeenCalledTimes ( 1 ) ;
71+ expect ( onNodeDragStop . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( nodeWithoutPosition ) ;
72+ } ) ;
73+
74+ it ( 'Should call onNodeClick when a node is clicked' , async ( ) => {
75+ const onNodeClick = vi . fn ( ) ;
76+ render (
77+ < Canvas
78+ title = { 'MongoDB Diagram' }
79+ nodes = { [ ORDERS_NODE , EMPLOYEES_NODE ] }
80+ edges = { [ EMPLOYEES_TO_ORDERS_EDGE ] }
81+ onNodeClick = { onNodeClick }
82+ /> ,
83+ ) ;
84+ const orders = screen . getByText ( 'orders' ) ;
85+ orders . click ( ) ;
86+ expect ( onNodeClick ) . toHaveBeenCalledTimes ( 1 ) ;
87+ expect ( onNodeClick . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( ORDERS_NODE ) ;
88+ } ) ;
89+
90+ it ( 'Should call onSelectionChange when different nodes are clicked' , async ( ) => {
91+ const onSelectionChange = vi . fn ( ) ;
92+ render (
93+ < Canvas
94+ title = { 'MongoDB Diagram' }
95+ nodes = { [ ORDERS_NODE , EMPLOYEES_NODE ] }
96+ edges = { [ EMPLOYEES_TO_ORDERS_EDGE ] }
97+ onSelectionChange = { onSelectionChange }
98+ /> ,
99+ ) ;
100+ screen . getByText ( 'orders' ) . click ( ) ;
101+ screen . getByText ( 'employees' ) . click ( ) ;
102+ screen . getByText ( 'employees' ) . click ( ) ; // for unknown reason there needs to be an extra click in the test
103+ expect ( onSelectionChange . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( { nodes : [ ] } ) ;
104+ expect ( onSelectionChange . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { nodes : [ ORDERS_NODE ] } ) ;
105+ expect ( onSelectionChange . mock . calls [ 2 ] [ 0 ] ) . toMatchObject ( { nodes : [ EMPLOYEES_NODE ] } ) ;
106+ } ) ;
107+ } ) ;
18108} ) ;
0 commit comments