Skip to content

Commit 96b93e3

Browse files
committed
refactor(tests): use connection line tester cmp
1 parent 9837e0d commit 96b93e3

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

tests/cypress/component/2-vue-flow/customConnectionLine.cy.ts

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
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'
43
import { BaseEdge, getBezierPath } from '@vue-flow/core'
54

6-
let propTargetNode: GraphNode | null | undefined = null
7-
let propTargetHandle: HandleElement | null | undefined = null
5+
const connectionLineId = 'test-custom-connection-line'
86

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)
1128

12-
propTargetNode = props.targetNode
13-
propTargetHandle = props.targetHandle
29+
return h(BaseEdge, { path: path[0], class: connectionLineId })
30+
}
31+
},
32+
})
1433

15-
return h(BaseEdge, { path: path[0], class: 'test-custom-connection-line' })
16-
}
34+
const onChangeSpy = cy.spy().as('onChangeSpy')
1735

18-
describe('Check if custom connection lines are rendered', () => {
19-
beforeEach(() => {
2036
cy.vueFlow(
2137
{
38+
autoConnect: true,
39+
fitViewOnInit: false,
2240
modelValue: [
2341
{
2442
id: '1',
@@ -34,11 +52,9 @@ describe('Check if custom connection lines are rendered', () => {
3452
],
3553
},
3654
{},
37-
{ 'connection-line': CustomConnectionLine },
55+
{ 'connection-line': (props: ConnectionLineProps) => h(CustomConnectionLine, { ...props, onChange: onChangeSpy }) },
3856
)
39-
})
4057

41-
it('renders custom connection line', () => {
4258
cy.window().then((win) => {
4359
const sourceHandle = cy.get(`[data-nodeid="1"].source`)
4460
const targetHandle = cy.get(`[data-nodeid="2"].target`)
@@ -54,27 +70,30 @@ describe('Check if custom connection lines are rendered', () => {
5470
view: win,
5571
})
5672
.trigger('mousemove', {
57-
clientX: x + 5,
58-
clientY: y + 5,
73+
clientX: x,
74+
clientY: y,
5975
force: true,
6076
})
6177

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)
6779

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+
})
7086

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)
7897
})
7998
})
8099
})

0 commit comments

Comments
 (0)