test: test for the actual better clone- and make sure we use the JSON stringify and parse for this since tests are weird

ref: N25B-371
This commit is contained in:
Björn Otgaar
2025-12-02 14:12:35 +01:00
parent 7640c32830
commit fe13017f2d
4 changed files with 63 additions and 37 deletions

View File

@@ -1,9 +1,9 @@
import { useDraggable } from '@neodrag/react';
import { useReactFlow, type XYPosition } from '@xyflow/react';
import { type ReactNode, useCallback, useRef, useState } from 'react';
import useFlowStore from '../VisProgStores';
import styles from '../../VisProg.module.css';
import { NodeDefaults, type NodeTypes } from '../NodeRegistry'
import addNode from '../utils/AddNode';
/**
* DraggableNodeProps dictates the type properties of a DraggableNode
@@ -47,41 +47,6 @@ function DraggableNode({ className, children, nodeType, onDrop }: DraggableNodeP
);
}
/**
* addNode — adds a new node to the flow using the unified class-based system.
* Keeps numbering logic for phase/norm nodes.
*/
function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
const { nodes, setNodes } = useFlowStore.getState();
// Find out if there's any default data about our ndoe
const defaultData = NodeDefaults[nodeType] ?? {}
// Currently, we find out what the Id is by checking the last node and adding one
const sameTypeNodes = nodes.filter((node) => node.type === nodeType);
const nextNumber =
sameTypeNodes.length > 0
? (() => {
const lastNode = sameTypeNodes[sameTypeNodes.length - 1];
const parts = lastNode.id.split('-');
const lastNum = Number(parts[1]);
return Number.isNaN(lastNum) ? sameTypeNodes.length + 1 : lastNum + 1;
})()
: 1;
const id = `${nodeType}-${nextNumber}`;
// Create new node
const newNode = {
id: id,
type: nodeType,
position,
// Deep copy using JSON because thats how things work:
// Ref: https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
data: structuredClone(defaultData)
}
setNodes([...nodes, newNode]);
}
/**
* DndToolbar defines how the drag and drop toolbar component works
* and includes the default onDrop behavior.

View File

@@ -0,0 +1,39 @@
import type { XYPosition } from "@xyflow/react";
import { NodeDefaults, type NodeTypes } from "../NodeRegistry";
import useFlowStore from "../VisProgStores";
/**
* addNode — adds a new node to the flow using the unified class-based system.
* Keeps numbering logic for phase/norm nodes.
*/
export default function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
const { nodes, setNodes } = useFlowStore.getState();
// Find out if there's any default data about our ndoe
const defaultData = NodeDefaults[nodeType] ?? {}
// Currently, we find out what the Id is by checking the last node and adding one
const sameTypeNodes = nodes.filter((node) => node.type === nodeType);
const nextNumber =
sameTypeNodes.length > 0
? (() => {
const lastNode = sameTypeNodes[sameTypeNodes.length - 1];
const parts = lastNode.id.split('-');
const lastNum = Number(parts[1]);
return Number.isNaN(lastNum) ? sameTypeNodes.length + 1 : lastNum + 1;
})()
: 1;
const id = `${nodeType}-${nextNumber}`;
// Create new node
const newNode = {
id: id,
type: nodeType,
position,
// Deep copy using JSON because thats how things work:
// Ref: https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
data: JSON.parse(JSON.stringify(defaultData))
}
setNodes([...nodes, newNode]);
}