tests: add tests for useNodesData

This commit is contained in:
braks
2024-02-03 18:05:30 +01:00
committed by Braks
parent 1dff3a1764
commit 7f6ae2fbf8

View File

@@ -0,0 +1,92 @@
import type { Node } from '@vue-flow/core'
import { useNodesData } from '@vue-flow/core'
import { defineComponent, h } from 'vue'
import { getElements } from '../../utils'
const { nodes } = getElements()
describe('Composable: `useNodesData`', () => {
describe('returns node data for a single node', () => {
let data: any = null
const node = nodes[0]
beforeEach(() => {
cy.vueFlow(
{
nodes: nodes.map((node) => ({ ...node, type: 'custom' })),
},
{},
{
'node-custom': defineComponent({
setup() {
data = useNodesData(nodes[0].id)
return () => h('div', 'Custom Node')
},
}),
},
)
})
it('should return the node data', () => {
expect(data.value).to.deep.equal(node.data)
})
})
describe('returns node data for multiple nodes', () => {
let data: any = null
const nodeIds = nodes.map((node) => node.id)
beforeEach(() => {
cy.vueFlow(
{
nodes: nodes.map((node) => ({ ...node, type: 'custom' })),
},
{},
{
'node-custom': defineComponent({
setup() {
data = useNodesData(nodeIds)
return () => h('div', 'Custom Node')
},
}),
},
)
})
it('should return the node data', () => {
expect(data.value).to.deep.equal(nodes.map((node) => node.data))
})
})
describe('returns node data for multiple nodes with a guard', () => {
let data: any = null
const nodeIds = nodes.map((node) => node.id)
beforeEach(() => {
cy.vueFlow(
{
nodes: nodes.map((node) => ({ ...node, type: 'custom' })),
},
{},
{
'node-custom': defineComponent({
setup() {
data = useNodesData(nodeIds, (node): node is Node<{ randomData: number }> => node.type === 'custom')
return () => h('div', 'Custom Node')
},
}),
},
)
})
it('should return the node data', () => {
expect(data.value).to.deep.equal(nodes.map((node) => node.data))
})
})
})