From 4d8cd3c1a4828c8f322203446f411424fc88e976 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Sat, 3 Feb 2024 15:02:05 +0100 Subject: [PATCH] tests: add tests for `updateNodeData` & `updateNode` --- .../component/1-store/nodes/updateNode.cy.ts | 96 +++++++++++++++++++ .../1-store/nodes/updateNodeData.cy.ts | 74 ++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 tests/cypress/component/1-store/nodes/updateNode.cy.ts create mode 100644 tests/cypress/component/1-store/nodes/updateNodeData.cy.ts diff --git a/tests/cypress/component/1-store/nodes/updateNode.cy.ts b/tests/cypress/component/1-store/nodes/updateNode.cy.ts new file mode 100644 index 00000000..a890d043 --- /dev/null +++ b/tests/cypress/component/1-store/nodes/updateNode.cy.ts @@ -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) + }) +}) diff --git a/tests/cypress/component/1-store/nodes/updateNodeData.cy.ts b/tests/cypress/component/1-store/nodes/updateNodeData.cy.ts new file mode 100644 index 00000000..160c8c01 --- /dev/null +++ b/tests/cypress/component/1-store/nodes/updateNodeData.cy.ts @@ -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 + }) +})