1
- import { h } from 'vue'
2
- import type { FunctionalComponent } from 'vue'
3
- import type { ConnectionLineProps , GraphNode , HandleElement } from '@vue-flow/core'
1
+ import { defineComponent , h , watch } from 'vue'
2
+ import type { ConnectionLineProps } from '@vue-flow/core'
4
3
import { BaseEdge , getBezierPath } from '@vue-flow/core'
5
4
6
- let propTargetNode : GraphNode | null | undefined = null
7
- let propTargetHandle : HandleElement | null | undefined = null
5
+ const connectionLineId = 'test-custom-connection-line'
8
6
9
- const CustomConnectionLine : FunctionalComponent < ConnectionLineProps > = ( props : ConnectionLineProps ) => {
10
- const path = getBezierPath ( props )
7
+ describe ( 'Custom Connection Line' , ( ) => {
8
+ it ( 'renders a custom connection line component' , ( ) => {
9
+ const CustomConnectionLine = defineComponent ( {
10
+ props : [ 'sourceNode' , 'sourceHandle' , 'targetNode' , 'targetHandle' , 'sourceX' , 'sourceY' , 'targetX' , 'targetY' ] ,
11
+ emits : [ 'change' ] ,
12
+ setup ( props , { emit } ) {
13
+ watch (
14
+ ( ) => props ,
15
+ ( ) => {
16
+ emit ( 'change' , {
17
+ sourceNodeId : props . sourceNode ?. id ,
18
+ sourceHandleId : props . sourceHandle . id ,
19
+ targetNodeId : props . targetNode ?. id ,
20
+ targetHandleId : props . targetHandle ?. id ,
21
+ } )
22
+ } ,
23
+ { immediate : true , deep : true } ,
24
+ )
25
+
26
+ return ( ) => {
27
+ const path = getBezierPath ( props )
11
28
12
- propTargetNode = props . targetNode
13
- propTargetHandle = props . targetHandle
29
+ return h ( BaseEdge , { path : path [ 0 ] , class : connectionLineId } )
30
+ }
31
+ } ,
32
+ } )
14
33
15
- return h ( BaseEdge , { path : path [ 0 ] , class : 'test-custom-connection-line' } )
16
- }
34
+ const onChangeSpy = cy . spy ( ) . as ( 'onChangeSpy' )
17
35
18
- describe ( 'Check if custom connection lines are rendered' , ( ) => {
19
- beforeEach ( ( ) => {
20
36
cy . vueFlow (
21
37
{
38
+ autoConnect : true ,
39
+ fitViewOnInit : false ,
22
40
modelValue : [
23
41
{
24
42
id : '1' ,
@@ -34,11 +52,9 @@ describe('Check if custom connection lines are rendered', () => {
34
52
] ,
35
53
} ,
36
54
{ } ,
37
- { 'connection-line' : CustomConnectionLine } ,
55
+ { 'connection-line' : ( props : ConnectionLineProps ) => h ( CustomConnectionLine , { ... props , onChange : onChangeSpy } ) } ,
38
56
)
39
- } )
40
57
41
- it ( 'renders custom connection line' , ( ) => {
42
58
cy . window ( ) . then ( ( win ) => {
43
59
const sourceHandle = cy . get ( `[data-nodeid="1"].source` )
44
60
const targetHandle = cy . get ( `[data-nodeid="2"].target` )
@@ -54,27 +70,30 @@ describe('Check if custom connection lines are rendered', () => {
54
70
view : win ,
55
71
} )
56
72
. trigger ( 'mousemove' , {
57
- clientX : x + 5 ,
58
- clientY : y + 5 ,
73
+ clientX : x ,
74
+ clientY : y ,
59
75
force : true ,
60
76
} )
61
77
62
- cy . get ( '.test-custom-connection-line' )
63
- . should ( 'have.length' , 1 )
64
- . then ( ( ) => {
65
- expect ( propTargetNode ) . to . not . be . null
66
- expect ( propTargetNode ?. id ) . to . equal ( '2' )
78
+ cy . get ( `.${ connectionLineId } ` ) . should ( 'have.length' , 1 )
67
79
68
- expect ( propTargetHandle ) . to . not . be . null
69
- expect ( propTargetHandle ?. id ) . to . equal ( '2__handle-top' )
80
+ cy . get ( '@onChangeSpy' ) . should ( 'have.been.calledWith' , {
81
+ sourceNodeId : '1' ,
82
+ sourceHandleId : '1__handle-bottom' ,
83
+ targetNodeId : '2' ,
84
+ targetHandleId : '2__handle-top' ,
85
+ } )
70
86
71
- sourceHandle . trigger ( 'mouseup' , {
72
- clientX : x + 5 ,
73
- clientY : y + 5 ,
74
- force : true ,
75
- view : win ,
76
- } )
77
- } )
87
+ sourceHandle . trigger ( 'mouseup' , {
88
+ clientX : x ,
89
+ clientY : y ,
90
+ force : true ,
91
+ view : win ,
92
+ } )
93
+
94
+ cy . get ( `.${ connectionLineId } ` ) . should ( 'have.length' , 0 )
95
+
96
+ cy . get ( '.vue-flow__edge' ) . should ( 'have.length' , 1 )
78
97
} )
79
98
} )
80
99
} )
0 commit comments