tests: add tests for updateNodeData & updateNode

This commit is contained in:
braks
2024-02-03 15:02:05 +01:00
committed by Braks
parent eca0447d3b
commit 4d8cd3c1a4
2 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
import { useVueFlow } from '@vue-flow/core'
import { getElements } from '../../../utils'
const { nodes, edges } = getElements()
describe('Store Action: `updateNode`', () => {
const store = useVueFlow({ id: 'test' })
let randomIndex: number
beforeEach(() => {
cy.vueFlow({
nodes,
edges,
})
})
beforeEach(() => {
randomIndex = Math.floor(Math.random() * nodes.length)
})
it('updates node from object', () => {
const nodeId = nodes[randomIndex].id
const newPosition = {
x: Math.random() * 100,
y: Math.random() * 100,
}
store.updateNode(nodeId, { position: newPosition })
const updatedNode = store.findNode(nodeId)
if (!updatedNode) {
throw new Error('Node not found in store')
}
expect(updatedNode.id).to.equal(nodeId)
expect(updatedNode.position.x).to.equal(newPosition.x)
expect(updatedNode.position.y).to.equal(newPosition.y)
})
it('updates node from function', () => {
const nodeId = nodes[randomIndex].id
let newPosition = {
x: 0,
y: 0,
}
store.updateNode(nodeId, (node) => {
newPosition = {
x: node.position.x + 10,
y: node.position.y + 10,
}
return { position: newPosition }
})
const updatedNode = store.findNode(nodeId)
if (!updatedNode) {
throw new Error('Node not found in store')
}
expect(updatedNode.id).to.equal(nodeId)
expect(updatedNode.position.x).to.equal(newPosition.x)
expect(updatedNode.position.y).to.equal(newPosition.y)
})
it('replaces node when `options.replace` is true', () => {
const nodeId = nodes[randomIndex].id
const testLabel = Date.now().toString()
const newNode = {
id: nodeId,
label: testLabel,
position: {
x: Math.random() * 100,
y: Math.random() * 100,
},
}
store.updateNode(nodeId, newNode, { replace: true })
const updatedNode = store.findNode(nodeId)
if (!updatedNode) {
throw new Error('Node not found in store')
}
expect(updatedNode.id).to.equal(nodeId)
expect(updatedNode.position.x).to.equal(newNode.position.x)
expect(updatedNode.position.y).to.equal(newNode.position.y)
expect(updatedNode.data).to.equal(undefined)
})
})

View File

@@ -0,0 +1,74 @@
import { useVueFlow } from '@vue-flow/core'
import { getElements } from '../../../utils'
const { nodes, edges } = getElements()
describe('Store Action: `updateNodeData`', () => {
const store = useVueFlow({ id: 'test' })
let randomIndex: number
beforeEach(() => {
cy.vueFlow({
nodes,
edges,
})
})
beforeEach(() => {
randomIndex = Math.floor(Math.random() * nodes.length)
})
it('updates node data from object', () => {
const nodeId = nodes[randomIndex].id
const testData = Date.now().toString()
store.updateNodeData(nodeId, { randomData: testData })
const updatedNode = store.findNode(nodeId)
if (!updatedNode) {
throw new Error('Node not found in store')
}
expect(updatedNode.id).to.equal(nodeId)
expect(updatedNode.data.randomData).to.equal(testData)
})
it('updates node data from function', () => {
const nodeId = nodes[randomIndex].id
let testData = ''
store.updateNodeData(nodeId, (node) => {
testData = node.data.randomData + Date.now().toString()
return { randomData: testData }
})
const updatedNode = store.findNode(nodeId)
if (!updatedNode) {
throw new Error('Node not found in store')
}
expect(updatedNode.id).to.equal(nodeId)
expect(updatedNode.data.randomData).to.equal(testData)
})
it('replaces node data when `replace` option is true', () => {
const nodeId = nodes[randomIndex].id
const testData = Date.now().toString()
store.updateNodeData(nodeId, { testData }, { replace: true })
const updatedNode = store.findNode(nodeId)
if (!updatedNode) {
throw new Error('Node not found in store')
}
expect(updatedNode.id).to.equal(nodeId)
expect(updatedNode.data.testData).to.equal(testData)
expect(updatedNode.data.randomData).to.not.exist
})
})