refactor(tests): use connection line tester cmp

This commit is contained in:
braks
2024-02-05 13:13:22 +01:00
committed by Braks
parent 9837e0d34f
commit 96b93e30e3
@@ -1,24 +1,42 @@
import { h } from 'vue' import { defineComponent, h, watch } from 'vue'
import type { FunctionalComponent } from 'vue' import type { ConnectionLineProps } from '@vue-flow/core'
import type { ConnectionLineProps, GraphNode, HandleElement } from '@vue-flow/core'
import { BaseEdge, getBezierPath } from '@vue-flow/core' import { BaseEdge, getBezierPath } from '@vue-flow/core'
let propTargetNode: GraphNode | null | undefined = null const connectionLineId = 'test-custom-connection-line'
let propTargetHandle: HandleElement | null | undefined = null
const CustomConnectionLine: FunctionalComponent<ConnectionLineProps> = (props: ConnectionLineProps) => { describe('Custom Connection Line', () => {
const path = getBezierPath(props) it('renders a custom connection line component', () => {
const CustomConnectionLine = defineComponent({
props: ['sourceNode', 'sourceHandle', 'targetNode', 'targetHandle', 'sourceX', 'sourceY', 'targetX', 'targetY'],
emits: ['change'],
setup(props, { emit }) {
watch(
() => props,
() => {
emit('change', {
sourceNodeId: props.sourceNode?.id,
sourceHandleId: props.sourceHandle.id,
targetNodeId: props.targetNode?.id,
targetHandleId: props.targetHandle?.id,
})
},
{ immediate: true, deep: true },
)
propTargetNode = props.targetNode return () => {
propTargetHandle = props.targetHandle const path = getBezierPath(props)
return h(BaseEdge, { path: path[0], class: 'test-custom-connection-line' }) return h(BaseEdge, { path: path[0], class: connectionLineId })
} }
},
})
const onChangeSpy = cy.spy().as('onChangeSpy')
describe('Check if custom connection lines are rendered', () => {
beforeEach(() => {
cy.vueFlow( cy.vueFlow(
{ {
autoConnect: true,
fitViewOnInit: false,
modelValue: [ modelValue: [
{ {
id: '1', id: '1',
@@ -34,11 +52,9 @@ describe('Check if custom connection lines are rendered', () => {
], ],
}, },
{}, {},
{ 'connection-line': CustomConnectionLine }, { 'connection-line': (props: ConnectionLineProps) => h(CustomConnectionLine, { ...props, onChange: onChangeSpy }) },
) )
})
it('renders custom connection line', () => {
cy.window().then((win) => { cy.window().then((win) => {
const sourceHandle = cy.get(`[data-nodeid="1"].source`) const sourceHandle = cy.get(`[data-nodeid="1"].source`)
const targetHandle = cy.get(`[data-nodeid="2"].target`) const targetHandle = cy.get(`[data-nodeid="2"].target`)
@@ -54,27 +70,30 @@ describe('Check if custom connection lines are rendered', () => {
view: win, view: win,
}) })
.trigger('mousemove', { .trigger('mousemove', {
clientX: x + 5, clientX: x,
clientY: y + 5, clientY: y,
force: true, force: true,
}) })
cy.get('.test-custom-connection-line') cy.get(`.${connectionLineId}`).should('have.length', 1)
.should('have.length', 1)
.then(() => {
expect(propTargetNode).to.not.be.null
expect(propTargetNode?.id).to.equal('2')
expect(propTargetHandle).to.not.be.null cy.get('@onChangeSpy').should('have.been.calledWith', {
expect(propTargetHandle?.id).to.equal('2__handle-top') sourceNodeId: '1',
sourceHandleId: '1__handle-bottom',
targetNodeId: '2',
targetHandleId: '2__handle-top',
})
sourceHandle.trigger('mouseup', { sourceHandle.trigger('mouseup', {
clientX: x + 5, clientX: x,
clientY: y + 5, clientY: y,
force: true, force: true,
view: win, view: win,
}) })
})
cy.get(`.${connectionLineId}`).should('have.length', 0)
cy.get('.vue-flow__edge').should('have.length', 1)
}) })
}) })
}) })