Skip to content

Commit 4d8cd3c

Browse files
committed
tests: add tests for updateNodeData & updateNode
1 parent eca0447 commit 4d8cd3c

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { useVueFlow } from '@vue-flow/core'
2+
import { getElements } from '../../../utils'
3+
4+
const { nodes, edges } = getElements()
5+
6+
describe('Store Action: `updateNode`', () => {
7+
const store = useVueFlow({ id: 'test' })
8+
let randomIndex: number
9+
10+
beforeEach(() => {
11+
cy.vueFlow({
12+
nodes,
13+
edges,
14+
})
15+
})
16+
17+
beforeEach(() => {
18+
randomIndex = Math.floor(Math.random() * nodes.length)
19+
})
20+
21+
it('updates node from object', () => {
22+
const nodeId = nodes[randomIndex].id
23+
24+
const newPosition = {
25+
x: Math.random() * 100,
26+
y: Math.random() * 100,
27+
}
28+
29+
store.updateNode(nodeId, { position: newPosition })
30+
31+
const updatedNode = store.findNode(nodeId)
32+
33+
if (!updatedNode) {
34+
throw new Error('Node not found in store')
35+
}
36+
37+
expect(updatedNode.id).to.equal(nodeId)
38+
expect(updatedNode.position.x).to.equal(newPosition.x)
39+
expect(updatedNode.position.y).to.equal(newPosition.y)
40+
})
41+
42+
it('updates node from function', () => {
43+
const nodeId = nodes[randomIndex].id
44+
45+
let newPosition = {
46+
x: 0,
47+
y: 0,
48+
}
49+
50+
store.updateNode(nodeId, (node) => {
51+
newPosition = {
52+
x: node.position.x + 10,
53+
y: node.position.y + 10,
54+
}
55+
56+
return { position: newPosition }
57+
})
58+
59+
const updatedNode = store.findNode(nodeId)
60+
61+
if (!updatedNode) {
62+
throw new Error('Node not found in store')
63+
}
64+
65+
expect(updatedNode.id).to.equal(nodeId)
66+
expect(updatedNode.position.x).to.equal(newPosition.x)
67+
expect(updatedNode.position.y).to.equal(newPosition.y)
68+
})
69+
70+
it('replaces node when `options.replace` is true', () => {
71+
const nodeId = nodes[randomIndex].id
72+
const testLabel = Date.now().toString()
73+
74+
const newNode = {
75+
id: nodeId,
76+
label: testLabel,
77+
position: {
78+
x: Math.random() * 100,
79+
y: Math.random() * 100,
80+
},
81+
}
82+
83+
store.updateNode(nodeId, newNode, { replace: true })
84+
85+
const updatedNode = store.findNode(nodeId)
86+
87+
if (!updatedNode) {
88+
throw new Error('Node not found in store')
89+
}
90+
91+
expect(updatedNode.id).to.equal(nodeId)
92+
expect(updatedNode.position.x).to.equal(newNode.position.x)
93+
expect(updatedNode.position.y).to.equal(newNode.position.y)
94+
expect(updatedNode.data).to.equal(undefined)
95+
})
96+
})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { useVueFlow } from '@vue-flow/core'
2+
import { getElements } from '../../../utils'
3+
4+
const { nodes, edges } = getElements()
5+
6+
describe('Store Action: `updateNodeData`', () => {
7+
const store = useVueFlow({ id: 'test' })
8+
let randomIndex: number
9+
10+
beforeEach(() => {
11+
cy.vueFlow({
12+
nodes,
13+
edges,
14+
})
15+
})
16+
17+
beforeEach(() => {
18+
randomIndex = Math.floor(Math.random() * nodes.length)
19+
})
20+
21+
it('updates node data from object', () => {
22+
const nodeId = nodes[randomIndex].id
23+
const testData = Date.now().toString()
24+
25+
store.updateNodeData(nodeId, { randomData: testData })
26+
27+
const updatedNode = store.findNode(nodeId)
28+
29+
if (!updatedNode) {
30+
throw new Error('Node not found in store')
31+
}
32+
33+
expect(updatedNode.id).to.equal(nodeId)
34+
expect(updatedNode.data.randomData).to.equal(testData)
35+
})
36+
37+
it('updates node data from function', () => {
38+
const nodeId = nodes[randomIndex].id
39+
40+
let testData = ''
41+
42+
store.updateNodeData(nodeId, (node) => {
43+
testData = node.data.randomData + Date.now().toString()
44+
45+
return { randomData: testData }
46+
})
47+
48+
const updatedNode = store.findNode(nodeId)
49+
50+
if (!updatedNode) {
51+
throw new Error('Node not found in store')
52+
}
53+
54+
expect(updatedNode.id).to.equal(nodeId)
55+
expect(updatedNode.data.randomData).to.equal(testData)
56+
})
57+
58+
it('replaces node data when `replace` option is true', () => {
59+
const nodeId = nodes[randomIndex].id
60+
const testData = Date.now().toString()
61+
62+
store.updateNodeData(nodeId, { testData }, { replace: true })
63+
64+
const updatedNode = store.findNode(nodeId)
65+
66+
if (!updatedNode) {
67+
throw new Error('Node not found in store')
68+
}
69+
70+
expect(updatedNode.id).to.equal(nodeId)
71+
expect(updatedNode.data.testData).to.equal(testData)
72+
expect(updatedNode.data.randomData).to.not.exist
73+
})
74+
})

0 commit comments

Comments
 (0)