chore: solve merge conflicts with dev
ref: N25B-189
This commit is contained in:
@@ -105,8 +105,9 @@ export function TextField({
|
|||||||
}) {
|
}) {
|
||||||
const [inputValue, setInputValue] = useState(value);
|
const [inputValue, setInputValue] = useState(value);
|
||||||
|
|
||||||
// Re-render when the value gets updated externally
|
useEffect(() => {
|
||||||
useEffect(() => setInputValue(value), [setInputValue, value]);
|
setInputValue(value);
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
const onCommit = () => setValue(inputValue);
|
const onCommit = () => setValue(inputValue);
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
MarkerType,
|
MarkerType,
|
||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import '@xyflow/react/dist/style.css';
|
import '@xyflow/react/dist/style.css';
|
||||||
|
import {useEffect} from "react";
|
||||||
import {useShallow} from 'zustand/react/shallow';
|
import {useShallow} from 'zustand/react/shallow';
|
||||||
import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx';
|
import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx';
|
||||||
import useFlowStore from './visualProgrammingUI/VisProgStores.tsx';
|
import useFlowStore from './visualProgrammingUI/VisProgStores.tsx';
|
||||||
@@ -42,7 +43,11 @@ const selector = (state: FlowState) => ({
|
|||||||
onConnect: state.onConnect,
|
onConnect: state.onConnect,
|
||||||
onReconnectStart: state.onReconnectStart,
|
onReconnectStart: state.onReconnectStart,
|
||||||
onReconnectEnd: state.onReconnectEnd,
|
onReconnectEnd: state.onReconnectEnd,
|
||||||
onReconnect: state.onReconnect
|
onReconnect: state.onReconnect,
|
||||||
|
undo: state.undo,
|
||||||
|
redo: state.redo,
|
||||||
|
beginBatchAction: state.beginBatchAction,
|
||||||
|
endBatchAction: state.endBatchAction
|
||||||
});
|
});
|
||||||
|
|
||||||
// --| define ReactFlow editor |--
|
// --| define ReactFlow editor |--
|
||||||
@@ -61,9 +66,23 @@ const VisProgUI = () => {
|
|||||||
onConnect,
|
onConnect,
|
||||||
onReconnect,
|
onReconnect,
|
||||||
onReconnectStart,
|
onReconnectStart,
|
||||||
onReconnectEnd
|
onReconnectEnd,
|
||||||
|
undo,
|
||||||
|
redo,
|
||||||
|
beginBatchAction,
|
||||||
|
endBatchAction
|
||||||
} = useFlowStore(useShallow(selector)); // instructs the editor to use the corresponding functions from the FlowStore
|
} = useFlowStore(useShallow(selector)); // instructs the editor to use the corresponding functions from the FlowStore
|
||||||
|
|
||||||
|
// adds ctrl+z and ctrl+y support to respectively undo and redo actions
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = (e: KeyboardEvent) => {
|
||||||
|
if (e.ctrlKey && e.key === 'z') undo();
|
||||||
|
if (e.ctrlKey && e.key === 'y') redo();
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handler);
|
||||||
|
return () => window.removeEventListener('keydown', handler);
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`${styles.innerEditorContainer} round-lg border-lg`}>
|
<div className={`${styles.innerEditorContainer} round-lg border-lg`}>
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
@@ -77,6 +96,8 @@ const VisProgUI = () => {
|
|||||||
onReconnectStart={onReconnectStart}
|
onReconnectStart={onReconnectStart}
|
||||||
onReconnectEnd={onReconnectEnd}
|
onReconnectEnd={onReconnectEnd}
|
||||||
onConnect={onConnect}
|
onConnect={onConnect}
|
||||||
|
onNodeDragStart={beginBatchAction}
|
||||||
|
onNodeDragStop={endBatchAction}
|
||||||
snapToGrid
|
snapToGrid
|
||||||
fitView
|
fitView
|
||||||
proOptions={{hideAttribution: true}}
|
proOptions={{hideAttribution: true}}
|
||||||
@@ -87,6 +108,10 @@ const VisProgUI = () => {
|
|||||||
<Panel position = "bottom-left" className={styles.saveLoadPanel}>
|
<Panel position = "bottom-left" className={styles.saveLoadPanel}>
|
||||||
<SaveLoadPanel></SaveLoadPanel>
|
<SaveLoadPanel></SaveLoadPanel>
|
||||||
</Panel>
|
</Panel>
|
||||||
|
<Panel position="bottom-center">
|
||||||
|
<button onClick={() => undo()}>undo</button>
|
||||||
|
<button onClick={() => redo()}>Redo</button>
|
||||||
|
</Panel>
|
||||||
<Controls/>
|
<Controls/>
|
||||||
<Background/>
|
<Background/>
|
||||||
</ReactFlow>
|
</ReactFlow>
|
||||||
@@ -94,8 +119,6 @@ const VisProgUI = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Places the VisProgUI component inside a ReactFlowProvider
|
* Places the VisProgUI component inside a ReactFlowProvider
|
||||||
*
|
*
|
||||||
|
|||||||
129
src/pages/VisProgPage/visualProgrammingUI/EditorUndoRedo.ts
Normal file
129
src/pages/VisProgPage/visualProgrammingUI/EditorUndoRedo.ts
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
import type {Edge, Node} from "@xyflow/react";
|
||||||
|
import type {StateCreator, StoreApi } from 'zustand/vanilla';
|
||||||
|
import type {FlowState} from "./VisProgTypes.tsx";
|
||||||
|
|
||||||
|
export type FlowSnapshot = {
|
||||||
|
nodes: Node[];
|
||||||
|
edges: Edge[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A reduced version of the flowState type,
|
||||||
|
* This removes the functions that are provided by UndoRedo from the expected input type
|
||||||
|
*/
|
||||||
|
type BaseFlowState = Omit<FlowState, 'undo' | 'redo' | 'pushSnapshot' | 'beginBatchAction' | 'endBatchAction'>;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UndoRedo is implemented as a middleware for the FlowState store,
|
||||||
|
* this allows us to keep the undo redo logic separate from the flowState,
|
||||||
|
* and thus from the internal editor logic
|
||||||
|
*
|
||||||
|
* Allows users to undo and redo actions in the visual programming editor
|
||||||
|
*
|
||||||
|
* @param {(set: StoreApi<FlowState>["setState"], get: () => FlowState, api: StoreApi<FlowState>) => BaseFlowState} config
|
||||||
|
* @returns {StateCreator<FlowState>}
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
export const UndoRedo = (
|
||||||
|
config: (
|
||||||
|
set: StoreApi<FlowState>['setState'],
|
||||||
|
get: () => FlowState,
|
||||||
|
api: StoreApi<FlowState>
|
||||||
|
) => BaseFlowState ) : StateCreator<FlowState> => (set, get, api) => {
|
||||||
|
let batchTimeout: number | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Captures the current state for
|
||||||
|
*
|
||||||
|
* @param {BaseFlowState} state - the current state of the editor
|
||||||
|
* @returns {FlowSnapshot} - returns a snapshot of the current editor state
|
||||||
|
*/
|
||||||
|
const getSnapshot = (state : BaseFlowState) : FlowSnapshot => ({
|
||||||
|
nodes: state.nodes,
|
||||||
|
edges: state.edges
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialState = config(set, get, api);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...initialState,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a snapshot of the current state to the undo history
|
||||||
|
*/
|
||||||
|
pushSnapshot: () => {
|
||||||
|
const state = get();
|
||||||
|
// we don't add new snapshots during an ongoing batch action
|
||||||
|
if (!state.isBatchAction) {
|
||||||
|
set({
|
||||||
|
past: [...state.past, getSnapshot(state)],
|
||||||
|
future: []
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undoes the last action from the editor,
|
||||||
|
* The state before undoing is added to the future for potential redoing
|
||||||
|
*/
|
||||||
|
undo: () => {
|
||||||
|
const state = get();
|
||||||
|
if (!state.past.length) return;
|
||||||
|
|
||||||
|
const snapshot = state.past.pop()!; // pop last snapshot
|
||||||
|
const currentSnapshot: FlowSnapshot = getSnapshot(state);
|
||||||
|
|
||||||
|
set({
|
||||||
|
nodes: snapshot.nodes,
|
||||||
|
edges: snapshot.edges,
|
||||||
|
});
|
||||||
|
|
||||||
|
state.future.push(currentSnapshot); // push current to redo
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redoes the last undone action,
|
||||||
|
* The state before redoing is added to the past for potential undoing
|
||||||
|
*/
|
||||||
|
redo: () => {
|
||||||
|
const state = get();
|
||||||
|
if (!state.future.length) return;
|
||||||
|
|
||||||
|
const snapshot = state.future.pop()!; // pop last redo
|
||||||
|
const currentSnapshot: FlowSnapshot = getSnapshot(state);
|
||||||
|
|
||||||
|
set({
|
||||||
|
nodes: snapshot.nodes,
|
||||||
|
edges: snapshot.edges,
|
||||||
|
});
|
||||||
|
|
||||||
|
state.past.push(currentSnapshot); // push current to undo
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begins a batched action
|
||||||
|
*
|
||||||
|
* An example of a batched action is dragging a node in the editor,
|
||||||
|
* where we want the entire action of moving a node to a different position
|
||||||
|
* to be covered by one undoable snapshot
|
||||||
|
*/
|
||||||
|
beginBatchAction: () => {
|
||||||
|
get().pushSnapshot();
|
||||||
|
set({ isBatchAction: true });
|
||||||
|
if (batchTimeout) clearTimeout(batchTimeout);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends a batched action,
|
||||||
|
* a very short timeout is used to prevent new snapshots from being added
|
||||||
|
* until we are certain that the batch event is finished
|
||||||
|
*/
|
||||||
|
endBatchAction: () => {
|
||||||
|
batchTimeout = window.setTimeout(() => {
|
||||||
|
set({ isBatchAction: false });
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -79,6 +79,7 @@ export const NodeConnects = {
|
|||||||
export const NodeDeletes = {
|
export const NodeDeletes = {
|
||||||
start: () => false,
|
start: () => false,
|
||||||
end: () => false,
|
end: () => false,
|
||||||
|
test: () => false, // Used for coverage of universal/ undefined nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,4 +92,5 @@ export const NodesInPhase = {
|
|||||||
start: () => false,
|
start: () => false,
|
||||||
end: () => false,
|
end: () => false,
|
||||||
phase: () => false,
|
phase: () => false,
|
||||||
|
test: () => false, // Used for coverage of universal/ undefined nodes
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import type { FlowState } from './VisProgTypes';
|
import type { FlowState } from './VisProgTypes';
|
||||||
import { NodeDefaults, NodeConnects, NodeDeletes } from './NodeRegistry';
|
import { NodeDefaults, NodeConnects, NodeDeletes } from './NodeRegistry';
|
||||||
|
import { UndoRedo } from "./EditorUndoRedo.ts";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,7 +35,7 @@ function createNode(id: string, type: string, position: XYPosition, data: Record
|
|||||||
return {...defaultData, ...newData}
|
return {...defaultData, ...newData}
|
||||||
}
|
}
|
||||||
|
|
||||||
//* Initial nodes to populate the flow at startup.
|
//* Initial nodes, created by using createNode. */
|
||||||
const initialNodes : Node[] = [
|
const initialNodes : Node[] = [
|
||||||
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}, false),
|
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}, false),
|
||||||
createNode('end', 'end', {x: 500, y: 100}, {label: "End"}, false),
|
createNode('end', 'end', {x: 500, y: 100}, {label: "End"}, false),
|
||||||
@@ -42,7 +43,7 @@ const initialNodes : Node[] = [
|
|||||||
createNode('norms-1', 'norm', {x:-200, y:100}, {label: "Initial Norms", normList: ["Be a robot", "get good"]}),
|
createNode('norms-1', 'norm', {x:-200, y:100}, {label: "Initial Norms", normList: ["Be a robot", "get good"]}),
|
||||||
];
|
];
|
||||||
|
|
||||||
//* Initial edges to connect the startup nodes.
|
// * Initial edges * /
|
||||||
const initialEdges: Edge[] = [
|
const initialEdges: Edge[] = [
|
||||||
{ id: 'start-phase-1', source: 'start', target: 'phase-1' },
|
{ id: 'start-phase-1', source: 'start', target: 'phase-1' },
|
||||||
{ id: 'phase-1-end', source: 'phase-1', target: 'end' },
|
{ id: 'phase-1-end', source: 'phase-1', target: 'end' },
|
||||||
@@ -50,17 +51,17 @@ const initialEdges: Edge[] = [
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How we have defined the functions for our FlowState.
|
* useFlowStore contains the implementation for all editor functionality
|
||||||
* We have the normal functionality of a default FlowState with some exceptions to account for extra functionality.
|
* and stores the current state of the visual programming editor
|
||||||
* The biggest changes are in onConnect and onDelete, which we have given extra functionality based on the nodes defined functions.
|
|
||||||
*
|
*
|
||||||
* * Provides:
|
* * Provides:
|
||||||
* - Node and edge state management
|
* - Node and edge state management
|
||||||
* - Node creation, deletion, and updates
|
* - Node creation, deletion, and updates
|
||||||
* - Custom connection handling via NodeConnects
|
* - Custom connection handling via NodeConnects
|
||||||
* - Edge reconnection handling
|
* - Edge reconnection handling
|
||||||
|
* - Undo Redo functionality through custom middleware
|
||||||
*/
|
*/
|
||||||
const useFlowStore = create<FlowState>((set, get) => ({
|
const useFlowStore = create<FlowState>(UndoRedo((set, get) => ({
|
||||||
nodes: initialNodes,
|
nodes: initialNodes,
|
||||||
edges: initialEdges,
|
edges: initialEdges,
|
||||||
edgeReconnectSuccessful: true,
|
edgeReconnectSuccessful: true,
|
||||||
@@ -68,8 +69,7 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
/**
|
/**
|
||||||
* Handles changes to nodes triggered by ReactFlow.
|
* Handles changes to nodes triggered by ReactFlow.
|
||||||
*/
|
*/
|
||||||
onNodesChange: (changes) =>
|
onNodesChange: (changes) => set({nodes: applyNodeChanges(changes, get().nodes)}),
|
||||||
set({nodes: applyNodeChanges(changes, get().nodes)}),
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles changes to edges triggered by ReactFlow.
|
* Handles changes to edges triggered by ReactFlow.
|
||||||
@@ -81,28 +81,34 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Updates edges and calls the node-specific connection functions.
|
* Updates edges and calls the node-specific connection functions.
|
||||||
*/
|
*/
|
||||||
onConnect: (connection) => {
|
onConnect: (connection) => {
|
||||||
const edges = addEdge(connection, get().edges);
|
get().pushSnapshot();
|
||||||
const nodes = get().nodes;
|
|
||||||
// connection has: { source, sourceHandle, target, targetHandle }
|
|
||||||
// Let's find the source and target ID's.
|
|
||||||
const sourceNode = nodes.find((n) => n.id == connection.source);
|
|
||||||
const targetNode = nodes.find((n) => n.id == connection.target);
|
|
||||||
|
|
||||||
// In case the nodes weren't found, return basic functionality.
|
const edges = addEdge(connection, get().edges);
|
||||||
if (sourceNode == undefined || targetNode == undefined || sourceNode.type == undefined || targetNode.type == undefined) {
|
const nodes = get().nodes;
|
||||||
|
// connection has: { source, sourceHandle, target, targetHandle }
|
||||||
|
// Let's find the source and target ID's.
|
||||||
|
const sourceNode = nodes.find((n) => n.id == connection.source);
|
||||||
|
const targetNode = nodes.find((n) => n.id == connection.target);
|
||||||
|
|
||||||
|
// In case the nodes weren't found, return basic functionality.
|
||||||
|
if ( sourceNode == undefined
|
||||||
|
|| targetNode == undefined
|
||||||
|
|| sourceNode.type == undefined
|
||||||
|
|| targetNode.type == undefined
|
||||||
|
){
|
||||||
|
set({ nodes, edges });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We should find out how their data changes by calling their respective functions.
|
||||||
|
const sourceConnectFunction = NodeConnects[sourceNode.type as keyof typeof NodeConnects]
|
||||||
|
const targetConnectFunction = NodeConnects[targetNode.type as keyof typeof NodeConnects]
|
||||||
|
|
||||||
|
// We're going to have to update their data based on how they want to update it.
|
||||||
|
sourceConnectFunction(sourceNode, targetNode, true)
|
||||||
|
targetConnectFunction(targetNode, sourceNode, false)
|
||||||
set({ nodes, edges });
|
set({ nodes, edges });
|
||||||
return;
|
},
|
||||||
}
|
|
||||||
|
|
||||||
// We should find out how their data changes by calling their respective functions.
|
|
||||||
const sourceConnectFunction = NodeConnects[sourceNode.type as keyof typeof NodeConnects]
|
|
||||||
const targetConnectFunction = NodeConnects[targetNode.type as keyof typeof NodeConnects]
|
|
||||||
|
|
||||||
// We're going to have to update their data based on how they want to update it.
|
|
||||||
sourceConnectFunction(sourceNode, targetNode, true)
|
|
||||||
targetConnectFunction(targetNode, sourceNode, false)
|
|
||||||
set({ nodes, edges });
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles reconnecting an edge between nodes.
|
* Handles reconnecting an edge between nodes.
|
||||||
@@ -112,7 +118,18 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
set({ edges: reconnectEdge(oldEdge, newConnection, get().edges) });
|
set({ edges: reconnectEdge(oldEdge, newConnection, get().edges) });
|
||||||
},
|
},
|
||||||
|
|
||||||
onReconnectStart: () => set({ edgeReconnectSuccessful: false }),
|
onReconnectStart: () => {
|
||||||
|
get().pushSnapshot();
|
||||||
|
set({ edgeReconnectSuccessful: false })
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles potential dropping (deleting) of an edge
|
||||||
|
* if it is not reconnected to a node after detaching it
|
||||||
|
*
|
||||||
|
* @param _evt - the event
|
||||||
|
* @param {{id: string}} edge - the described edge
|
||||||
|
*/
|
||||||
onReconnectEnd: (_evt, edge) => {
|
onReconnectEnd: (_evt, edge) => {
|
||||||
if (!get().edgeReconnectSuccessful) {
|
if (!get().edgeReconnectSuccessful) {
|
||||||
set({ edges: get().edges.filter((e) => e.id !== edge.id) });
|
set({ edges: get().edges.filter((e) => e.id !== edge.id) });
|
||||||
@@ -125,6 +142,8 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Also removes all edges connected to that node.
|
* Also removes all edges connected to that node.
|
||||||
*/
|
*/
|
||||||
deleteNode: (nodeId) => {
|
deleteNode: (nodeId) => {
|
||||||
|
get().pushSnapshot();
|
||||||
|
|
||||||
// Let's find our node to check if they have a special deletion function
|
// Let's find our node to check if they have a special deletion function
|
||||||
const ourNode = get().nodes.find((n)=>n.id==nodeId);
|
const ourNode = get().nodes.find((n)=>n.id==nodeId);
|
||||||
const ourFunction = Object.entries(NodeDeletes).find(([t])=>t==ourNode?.type)?.[1]
|
const ourFunction = Object.entries(NodeDeletes).find(([t])=>t==ourNode?.type)?.[1]
|
||||||
@@ -135,7 +154,7 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
nodes: get().nodes.filter((n) => n.id !== nodeId),
|
nodes: get().nodes.filter((n) => n.id !== nodeId),
|
||||||
edges: get().edges.filter((e) => e.source !== nodeId && e.target !== nodeId),
|
edges: get().edges.filter((e) => e.source !== nodeId && e.target !== nodeId),
|
||||||
})}
|
})}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the entire nodes array in the store.
|
* Replaces the entire nodes array in the store.
|
||||||
@@ -151,6 +170,7 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Updates the data of a node by merging new data with existing data.
|
* Updates the data of a node by merging new data with existing data.
|
||||||
*/
|
*/
|
||||||
updateNodeData: (nodeId, data) => {
|
updateNodeData: (nodeId, data) => {
|
||||||
|
get().pushSnapshot();
|
||||||
set({
|
set({
|
||||||
nodes: get().nodes.map((node) => {
|
nodes: get().nodes.map((node) => {
|
||||||
if (node.id === nodeId) {
|
if (node.id === nodeId) {
|
||||||
@@ -165,8 +185,15 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
* Adds a new node to the flow store.
|
* Adds a new node to the flow store.
|
||||||
*/
|
*/
|
||||||
addNode: (node: Node) => {
|
addNode: (node: Node) => {
|
||||||
|
get().pushSnapshot();
|
||||||
set({ nodes: [...get().nodes, node] });
|
set({ nodes: [...get().nodes, node] });
|
||||||
},
|
},
|
||||||
}));
|
|
||||||
|
// undo redo default values
|
||||||
|
past: [],
|
||||||
|
future: [],
|
||||||
|
isBatchAction: false,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
export default useFlowStore;
|
export default useFlowStore;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// VisProgTypes.ts
|
// VisProgTypes.ts
|
||||||
import type { Edge, OnNodesChange, OnEdgesChange, OnConnect, OnReconnect, Node } from '@xyflow/react';
|
import type { Edge, OnNodesChange, OnEdgesChange, OnConnect, OnReconnect, Node } from '@xyflow/react';
|
||||||
import type { NodeTypes } from './NodeRegistry';
|
import type { NodeTypes } from './NodeRegistry';
|
||||||
|
import type {FlowSnapshot} from "./EditorUndoRedo.ts";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type representing all registered node types.
|
* Type representing all registered node types.
|
||||||
@@ -74,4 +76,14 @@ export type FlowState = {
|
|||||||
* @param node - the Node object to add
|
* @param node - the Node object to add
|
||||||
*/
|
*/
|
||||||
addNode: (node: Node) => void;
|
addNode: (node: Node) => void;
|
||||||
|
|
||||||
|
// UndoRedo Types
|
||||||
|
past: FlowSnapshot[];
|
||||||
|
future: FlowSnapshot[];
|
||||||
|
pushSnapshot: () => void;
|
||||||
|
isBatchAction: boolean;
|
||||||
|
beginBatchAction: () => void;
|
||||||
|
endBatchAction: () => void;
|
||||||
|
undo: () => void;
|
||||||
|
redo: () => void;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -47,7 +47,11 @@ function DraggableNode({ className, children, nodeType, onDrop }: DraggableNodeP
|
|||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={className} ref={draggableRef}>
|
<div className={className}
|
||||||
|
ref={draggableRef}
|
||||||
|
id={`draggable-${nodeType}`}
|
||||||
|
data-testid={`draggable-${nodeType}`}
|
||||||
|
>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -64,8 +68,8 @@ function DraggableNode({ className, children, nodeType, onDrop }: DraggableNodeP
|
|||||||
* @param nodeType - The type of node to create (from `NodeTypes`).
|
* @param nodeType - The type of node to create (from `NodeTypes`).
|
||||||
* @param position - The XY position in the flow canvas where the node will appear.
|
* @param position - The XY position in the flow canvas where the node will appear.
|
||||||
*/
|
*/
|
||||||
function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
|
function addNodeToFlow(nodeType: keyof typeof NodeTypes, position: XYPosition) {
|
||||||
const { nodes, setNodes } = useFlowStore.getState();
|
const { nodes, addNode } = useFlowStore.getState();
|
||||||
|
|
||||||
// Load any predefined data for this node type.
|
// Load any predefined data for this node type.
|
||||||
const defaultData = NodeDefaults[nodeType] ?? {}
|
const defaultData = NodeDefaults[nodeType] ?? {}
|
||||||
@@ -90,7 +94,7 @@ function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
|
|||||||
position,
|
position,
|
||||||
data: {...defaultData}
|
data: {...defaultData}
|
||||||
}
|
}
|
||||||
setNodes([...nodes, newNode]);
|
addNode(newNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,7 +129,7 @@ export function DndToolbar() {
|
|||||||
|
|
||||||
if (isInFlow) {
|
if (isInFlow) {
|
||||||
const position = screenToFlowPosition(screenPosition);
|
const position = screenToFlowPosition(screenPosition);
|
||||||
addNode(nodeType, position);
|
addNodeToFlow(nodeType, position);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[screenToFlowPosition],
|
[screenToFlowPosition],
|
||||||
@@ -149,6 +153,7 @@ export function DndToolbar() {
|
|||||||
{/* Maps over all the nodes that are droppable, and puts them in the panel */}
|
{/* Maps over all the nodes that are droppable, and puts them in the panel */}
|
||||||
{droppableNodes.map(({type, data}) => (
|
{droppableNodes.map(({type, data}) => (
|
||||||
<DraggableNode
|
<DraggableNode
|
||||||
|
key={type}
|
||||||
className={styles[`draggable-node-${type}`]} // Our current style signature for nodes
|
className={styles[`draggable-node-${type}`]} // Our current style signature for nodes
|
||||||
nodeType={type}
|
nodeType={type}
|
||||||
onDrop={handleNodeDrop}
|
onDrop={handleNodeDrop}
|
||||||
|
|||||||
@@ -40,14 +40,11 @@ export default function EndNode(props: NodeProps<EndNode>) {
|
|||||||
/**
|
/**
|
||||||
* Functionality for reducing this node into its more compact json program
|
* Functionality for reducing this node into its more compact json program
|
||||||
* @param node the node to reduce
|
* @param node the node to reduce
|
||||||
* @param nodes all nodes present
|
* @param _nodes all nodes present
|
||||||
* @returns Dictionary, {id: node.id}
|
* @returns Dictionary, {id: node.id}
|
||||||
*/
|
*/
|
||||||
export function EndReduce(node: Node, nodes: Node[]) {
|
export function EndReduce(node: Node, _nodes: Node[]) {
|
||||||
// Replace this for nodes functionality
|
// Replace this for nodes functionality
|
||||||
if (nodes.length <= -1) {
|
|
||||||
console.warn("Impossible nodes length in EndReduce")
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
id: node.id
|
id: node.id
|
||||||
}
|
}
|
||||||
@@ -55,13 +52,9 @@ export function EndReduce(node: Node, nodes: Node[]) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Any connection functionality that should get called when a connection is made to this node type (end)
|
* Any connection functionality that should get called when a connection is made to this node type (end)
|
||||||
* @param thisNode the node of which the functionality gets called
|
* @param _thisNode the node of which the functionality gets called
|
||||||
* @param otherNode the other node which has connected
|
* @param _otherNode the other node which has connected
|
||||||
* @param isThisSource whether this node is the one that is the source of the connection
|
* @param _isThisSource whether this node is the one that is the source of the connection
|
||||||
*/
|
*/
|
||||||
export function EndConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
|
export function EndConnects(_thisNode: Node, _otherNode: Node, _isThisSource: boolean) {
|
||||||
// Replace this for connection logic
|
|
||||||
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
|
|
||||||
console.warn("Impossible node connection called in EndConnects")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -32,23 +32,22 @@ export type GoalNode = Node<GoalNodeData>
|
|||||||
* @param props NodeProps, like id, label, children
|
* @param props NodeProps, like id, label, children
|
||||||
* @returns React.JSX.Element
|
* @returns React.JSX.Element
|
||||||
*/
|
*/
|
||||||
export default function GoalNode(props: NodeProps<GoalNode>) {
|
export default function GoalNode({id, data}: NodeProps<GoalNode>) {
|
||||||
const data = props.data
|
|
||||||
const {updateNodeData} = useFlowStore();
|
const {updateNodeData} = useFlowStore();
|
||||||
|
|
||||||
const text_input_id = `goal_${props.id}_text_input`;
|
const text_input_id = `goal_${id}_text_input`;
|
||||||
const checkbox_id = `goal_${props.id}_checkbox`;
|
const checkbox_id = `goal_${id}_checkbox`;
|
||||||
|
|
||||||
const setDescription = (value: string) => {
|
const setDescription = (value: string) => {
|
||||||
updateNodeData(props.id, {...data, description: value});
|
updateNodeData(id, {...data, description: value});
|
||||||
}
|
}
|
||||||
|
|
||||||
const setAchieved = (value: boolean) => {
|
const setAchieved = (value: boolean) => {
|
||||||
updateNodeData(props.id, {...data, achieved: value});
|
updateNodeData(id, {...data, achieved: value});
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
<Toolbar nodeId={id} allowDelete={true}/>
|
||||||
<div className={`${styles.defaultNode} ${styles.nodeGoal} flex-col gap-sm`}>
|
<div className={`${styles.defaultNode} ${styles.nodeGoal} flex-col gap-sm`}>
|
||||||
<div className={"flex-row gap-md"}>
|
<div className={"flex-row gap-md"}>
|
||||||
<label htmlFor={text_input_id}>Goal:</label>
|
<label htmlFor={text_input_id}>Goal:</label>
|
||||||
@@ -64,7 +63,7 @@ export default function GoalNode(props: NodeProps<GoalNode>) {
|
|||||||
<input
|
<input
|
||||||
id={checkbox_id}
|
id={checkbox_id}
|
||||||
type={"checkbox"}
|
type={"checkbox"}
|
||||||
value={data.achieved ? "checked" : ""}
|
checked={data.achieved || false}
|
||||||
onChange={(e) => setAchieved(e.target.checked)}
|
onChange={(e) => setAchieved(e.target.checked)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -77,13 +76,9 @@ export default function GoalNode(props: NodeProps<GoalNode>) {
|
|||||||
/**
|
/**
|
||||||
* Reduces each Goal, including its children down into its relevant data.
|
* Reduces each Goal, including its children down into its relevant data.
|
||||||
* @param node: The Node Properties of this node.
|
* @param node: The Node Properties of this node.
|
||||||
* @param nodes: all the nodes in the graph
|
* @param _nodes: all the nodes in the graph
|
||||||
*/
|
*/
|
||||||
export function GoalReduce(node: Node, nodes: Node[]) {
|
export function GoalReduce(node: Node, _nodes: Node[]) {
|
||||||
// Replace this for nodes functionality
|
|
||||||
if (nodes.length <= -1) {
|
|
||||||
console.warn("Impossible nodes length in GoalReduce")
|
|
||||||
}
|
|
||||||
const data = node.data as GoalNodeData;
|
const data = node.data as GoalNodeData;
|
||||||
return {
|
return {
|
||||||
id: node.id,
|
id: node.id,
|
||||||
@@ -95,13 +90,10 @@ export function GoalReduce(node: Node, nodes: Node[]) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is called whenever a connection is made with this node type (Goal)
|
* This function is called whenever a connection is made with this node type (Goal)
|
||||||
* @param thisNode the node of this node type which function is called
|
* @param _thisNode the node of this node type which function is called
|
||||||
* @param otherNode the other node which was part of the connection
|
* @param _otherNode the other node which was part of the connection
|
||||||
* @param isThisSource whether this instance of the node was the source in the connection, true = yes.
|
* @param _isThisSource whether this instance of the node was the source in the connection, true = yes.
|
||||||
*/
|
*/
|
||||||
export function GoalConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
|
export function GoalConnects(_thisNode: Node, _otherNode: Node, _isThisSource: boolean) {
|
||||||
// Replace this for connection logic
|
// Replace this for connection logic
|
||||||
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
|
|
||||||
console.warn("Impossible node connection called in EndConnects")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -61,13 +61,9 @@ export default function NormNode(props: NodeProps<NormNode>) {
|
|||||||
/**
|
/**
|
||||||
* Reduces each Norm, including its children down into its relevant data.
|
* Reduces each Norm, including its children down into its relevant data.
|
||||||
* @param node: The Node Properties of this node.
|
* @param node: The Node Properties of this node.
|
||||||
* @param nodes: all the nodes in the graph
|
* @param _nodes: all the nodes in the graph
|
||||||
*/
|
*/
|
||||||
export function NormReduce(node: Node, nodes: Node[]) {
|
export function NormReduce(node: Node, _nodes: Node[]) {
|
||||||
// Replace this for nodes functionality
|
|
||||||
if (nodes.length <= -1) {
|
|
||||||
console.warn("Impossible nodes length in NormReduce")
|
|
||||||
}
|
|
||||||
const data = node.data as NormNodeData;
|
const data = node.data as NormNodeData;
|
||||||
return {
|
return {
|
||||||
id: node.id,
|
id: node.id,
|
||||||
@@ -78,13 +74,9 @@ export function NormReduce(node: Node, nodes: Node[]) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is called whenever a connection is made with this node type (Norm)
|
* This function is called whenever a connection is made with this node type (Norm)
|
||||||
* @param thisNode the node of this node type which function is called
|
* @param _thisNode the node of this node type which function is called
|
||||||
* @param otherNode the other node which was part of the connection
|
* @param _otherNode the other node which was part of the connection
|
||||||
* @param isThisSource whether this instance of the node was the source in the connection, true = yes.
|
* @param _isThisSource whether this instance of the node was the source in the connection, true = yes.
|
||||||
*/
|
*/
|
||||||
export function NormConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
|
export function NormConnects(_thisNode: Node, _otherNode: Node, _isThisSource: boolean) {
|
||||||
// Replace this for connection logic
|
|
||||||
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
|
|
||||||
console.warn("Impossible node connection called in EndConnects")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -78,8 +78,10 @@ export function PhaseReduce(node: Node, nodes: Node[]) {
|
|||||||
.filter(([t]) => !nodesNotInPhase.includes(t))
|
.filter(([t]) => !nodesNotInPhase.includes(t))
|
||||||
.map(([t]) => t);
|
.map(([t]) => t);
|
||||||
|
|
||||||
// children nodes
|
// children nodes - make sure to check for empty arrays
|
||||||
const childrenNodes = nodes.filter((node) => data.children.includes(node.id));
|
let childrenNodes: Node[] = [];
|
||||||
|
if (data.children)
|
||||||
|
childrenNodes = nodes.filter((node) => data.children.includes(node.id));
|
||||||
|
|
||||||
// Build the result object
|
// Build the result object
|
||||||
const result: Record<string, unknown> = {
|
const result: Record<string, unknown> = {
|
||||||
|
|||||||
@@ -40,14 +40,11 @@ export default function StartNode(props: NodeProps<StartNode>) {
|
|||||||
/**
|
/**
|
||||||
* The reduce function for this node type.
|
* The reduce function for this node type.
|
||||||
* @param node this node
|
* @param node this node
|
||||||
* @param nodes all the nodes in the graph
|
* @param _nodes all the nodes in the graph
|
||||||
* @returns a reduced structure of this node
|
* @returns a reduced structure of this node
|
||||||
*/
|
*/
|
||||||
export function StartReduce(node: Node, nodes: Node[]) {
|
export function StartReduce(node: Node, _nodes: Node[]) {
|
||||||
// Replace this for nodes functionality
|
// Replace this for nodes functionality
|
||||||
if (nodes.length <= -1) {
|
|
||||||
console.warn("Impossible nodes length in StartReduce")
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
id: node.id
|
id: node.id
|
||||||
}
|
}
|
||||||
@@ -55,13 +52,9 @@ export function StartReduce(node: Node, nodes: Node[]) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is called whenever a connection is made with this node type (start)
|
* This function is called whenever a connection is made with this node type (start)
|
||||||
* @param thisNode the node of this node type which function is called
|
* @param _thisNode the node of this node type which function is called
|
||||||
* @param otherNode the other node which was part of the connection
|
* @param _otherNode the other node which was part of the connection
|
||||||
* @param isThisSource whether this instance of the node was the source in the connection, true = yes.
|
* @param _isThisSource whether this instance of the node was the source in the connection, true = yes.
|
||||||
*/
|
*/
|
||||||
export function StartConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
|
export function StartConnects(_thisNode: Node, _otherNode: Node, _isThisSource: boolean) {
|
||||||
// Replace this for connection logic
|
|
||||||
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
|
|
||||||
console.warn("Impossible node connection called in EndConnects")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -80,14 +80,10 @@ export default function TriggerNode(props: NodeProps<TriggerNode>) {
|
|||||||
/**
|
/**
|
||||||
* Reduces each Trigger, including its children down into its core data.
|
* Reduces each Trigger, including its children down into its core data.
|
||||||
* @param node - The Trigger node to reduce.
|
* @param node - The Trigger node to reduce.
|
||||||
* @param nodes - The list of all nodes in the current flow graph.
|
* @param _nodes - The list of all nodes in the current flow graph.
|
||||||
* @returns A simplified object containing the node label and its list of triggers.
|
* @returns A simplified object containing the node label and its list of triggers.
|
||||||
*/
|
*/
|
||||||
export function TriggerReduce(node: Node, nodes: Node[]) {
|
export function TriggerReduce(node: Node, _nodes: Node[]) {
|
||||||
// Replace this for nodes functionality
|
|
||||||
if (nodes.length <= -1) {
|
|
||||||
console.warn("Impossible nodes length in TriggerReduce")
|
|
||||||
}
|
|
||||||
const data = node.data;
|
const data = node.data;
|
||||||
switch (data.triggerType) {
|
switch (data.triggerType) {
|
||||||
case "keywords":
|
case "keywords":
|
||||||
@@ -106,17 +102,13 @@ export function TriggerReduce(node: Node, nodes: Node[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles logic that occurs when a connection is made involving a Trigger node.
|
* This function is called whenever a connection is made with this node type (trigger)
|
||||||
*
|
* @param _thisNode the node of this node type which function is called
|
||||||
* @param thisNode - The current Trigger node being connected.
|
* @param _otherNode the other node which was part of the connection
|
||||||
* @param otherNode - The other node involved in the connection.
|
* @param _isThisSource whether this instance of the node was the source in the connection, true = yes.
|
||||||
* @param isThisSource - Whether this node was the source of the connection.
|
|
||||||
*/
|
*/
|
||||||
export function TriggerConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
|
export function TriggerConnects(_thisNode: Node, _otherNode: Node, _isThisSource: boolean) {
|
||||||
// Replace this for connection logic
|
|
||||||
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
|
|
||||||
console.warn("Impossible node connection called in EndConnects")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definitions for the possible triggers, being keywords and emotions
|
// Definitions for the possible triggers, being keywords and emotions
|
||||||
|
|||||||
@@ -127,10 +127,10 @@ describe("Logging component", () => {
|
|||||||
|
|
||||||
render(<Logging/>);
|
render(<Logging/>);
|
||||||
|
|
||||||
expect(screen.getByText("Logs")).toBeInTheDocument();
|
expect(screen.getByText("Logs")).toBeDefined();
|
||||||
expect(screen.getByText("WARNING")).toBeInTheDocument();
|
expect(screen.getByText("WARNING")).toBeDefined();
|
||||||
expect(screen.getByText("logging")).toBeInTheDocument();
|
expect(screen.getByText("logging")).toBeDefined();
|
||||||
expect(screen.getByText("Ping")).toBeInTheDocument();
|
expect(screen.getByText("Ping")).toBeDefined();
|
||||||
|
|
||||||
let timestamp = screen.queryByText("ABS TIME");
|
let timestamp = screen.queryByText("ABS TIME");
|
||||||
if (!timestamp) {
|
if (!timestamp) {
|
||||||
@@ -141,7 +141,7 @@ describe("Logging component", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await user.click(timestamp);
|
await user.click(timestamp);
|
||||||
expect(screen.getByText("00:00:12.345")).toBeInTheDocument();
|
expect(screen.getByText("00:00:12.345")).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows the scroll-to-bottom button after a manual scroll and scrolls when clicked", async () => {
|
it("shows the scroll-to-bottom button after a manual scroll and scrolls when clicked", async () => {
|
||||||
@@ -188,7 +188,7 @@ describe("Logging component", () => {
|
|||||||
logCell.set({...current, message: "Updated"});
|
logCell.set({...current, message: "Updated"});
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(screen.getByText("Updated")).toBeInTheDocument();
|
expect(screen.getByText("Updated")).toBeDefined();
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(scrollSpy).toHaveBeenCalledTimes(1);
|
expect(scrollSpy).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|||||||
167
test/pages/robot/Robot.test.tsx
Normal file
167
test/pages/robot/Robot.test.tsx
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
import { render, screen, act, cleanup, fireEvent } from '@testing-library/react';
|
||||||
|
import Robot from '../../../src/pages/Robot/Robot';
|
||||||
|
|
||||||
|
// Mock EventSource
|
||||||
|
const mockInstances: MockEventSource[] = [];
|
||||||
|
class MockEventSource {
|
||||||
|
url: string;
|
||||||
|
onmessage: ((event: MessageEvent) => void) | null = null;
|
||||||
|
closed = false;
|
||||||
|
|
||||||
|
constructor(url: string) {
|
||||||
|
this.url = url;
|
||||||
|
mockInstances.push(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(data: string) {
|
||||||
|
this.onmessage?.({ data } as MessageEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
this.closed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mock global EventSource
|
||||||
|
beforeAll(() => {
|
||||||
|
(globalThis as any).EventSource = jest.fn((url: string) => new MockEventSource(url));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock fetch
|
||||||
|
beforeEach(() => {
|
||||||
|
globalThis.fetch = jest.fn(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
json: () => Promise.resolve({ reply: 'ok' }),
|
||||||
|
})
|
||||||
|
) as jest.Mock;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
afterEach(() => {
|
||||||
|
cleanup();
|
||||||
|
jest.restoreAllMocks();
|
||||||
|
mockInstances.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Robot', () => {
|
||||||
|
test('renders initial state', () => {
|
||||||
|
render(<Robot />);
|
||||||
|
expect(screen.getByText('Robot interaction')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Force robot speech')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Listening 🔴')).toBeInTheDocument();
|
||||||
|
expect(screen.getByPlaceholderText('Enter a message')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sends message via button', async () => {
|
||||||
|
render(<Robot />);
|
||||||
|
const input = screen.getByPlaceholderText('Enter a message');
|
||||||
|
const button = screen.getByText('Speak');
|
||||||
|
|
||||||
|
fireEvent.change(input, { target: { value: 'Hello' } });
|
||||||
|
await act(async () => fireEvent.click(button));
|
||||||
|
|
||||||
|
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||||
|
'http://localhost:8000/message',
|
||||||
|
expect.objectContaining({
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ message: 'Hello' }),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sends message via Enter key', async () => {
|
||||||
|
render(<Robot />);
|
||||||
|
const input = screen.getByPlaceholderText('Enter a message');
|
||||||
|
fireEvent.change(input, { target: { value: 'Hi Enter' } });
|
||||||
|
|
||||||
|
await act(async () =>
|
||||||
|
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 })
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||||
|
'http://localhost:8000/message',
|
||||||
|
expect.objectContaining({
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ message: 'Hi Enter' }),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
expect((input as HTMLInputElement).value).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles fetch errors', async () => {
|
||||||
|
globalThis.fetch = jest.fn(() => Promise.reject('Network error')) as jest.Mock;
|
||||||
|
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||||
|
|
||||||
|
render(<Robot />);
|
||||||
|
const input = screen.getByPlaceholderText('Enter a message');
|
||||||
|
const button = screen.getByText('Speak');
|
||||||
|
fireEvent.change(input, { target: { value: 'Error test' } });
|
||||||
|
|
||||||
|
await act(async () => fireEvent.click(button));
|
||||||
|
|
||||||
|
expect(consoleSpy).toHaveBeenCalledWith(
|
||||||
|
'Error sending message: ',
|
||||||
|
'Network error'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('updates conversation on SSE', async () => {
|
||||||
|
render(<Robot />);
|
||||||
|
const eventSource = mockInstances[0];
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
eventSource.sendMessage(JSON.stringify({ voice_active: true }));
|
||||||
|
eventSource.sendMessage(JSON.stringify({ speech: 'User says hi' }));
|
||||||
|
eventSource.sendMessage(JSON.stringify({ llm_response: 'Assistant replies' }));
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(screen.getByText('Listening 🟢')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('User says hi')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Assistant replies')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles invalid SSE JSON', async () => {
|
||||||
|
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||||
|
render(<Robot />);
|
||||||
|
const eventSource = mockInstances[0];
|
||||||
|
|
||||||
|
await act(async () => eventSource.sendMessage('bad-json'));
|
||||||
|
|
||||||
|
expect(logSpy).toHaveBeenCalledWith('Unparsable SSE message:', 'bad-json');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resets conversation with Reset button', async () => {
|
||||||
|
render(<Robot />);
|
||||||
|
const eventSource = mockInstances[0];
|
||||||
|
|
||||||
|
await act(async () =>
|
||||||
|
eventSource.sendMessage(JSON.stringify({ speech: 'Hello' }))
|
||||||
|
);
|
||||||
|
expect(screen.getByText('Hello')).toBeInTheDocument();
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByText('Reset'));
|
||||||
|
expect(screen.queryByText('Hello')).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('toggles conversationIndex with Stop/Start button', () => {
|
||||||
|
render(<Robot />);
|
||||||
|
const stopButton = screen.getByText('Stop');
|
||||||
|
fireEvent.click(stopButton);
|
||||||
|
expect(screen.getByText('Start')).toBeInTheDocument();
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByText('Start'));
|
||||||
|
expect(screen.getByText('Stop')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('closes EventSource on unmount', () => {
|
||||||
|
const { unmount } = render(<Robot />);
|
||||||
|
const eventSource = mockInstances[0];
|
||||||
|
const closeSpy = jest.spyOn(eventSource, 'close');
|
||||||
|
|
||||||
|
unmount();
|
||||||
|
expect(closeSpy).toHaveBeenCalled();
|
||||||
|
expect(eventSource.closed).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,239 @@
|
|||||||
|
import {act} from '@testing-library/react';
|
||||||
|
import useFlowStore from '../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx';
|
||||||
|
import { mockReactFlow } from '../../../setupFlowTests.ts';
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
mockReactFlow();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("UndoRedo Middleware", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("pushSnapshot adds a snapshot to past and clears future", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: [],
|
||||||
|
past: [],
|
||||||
|
future: [{
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
edges: []
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
})
|
||||||
|
|
||||||
|
const state = store.getState();
|
||||||
|
expect(state.past.length).toBe(1);
|
||||||
|
expect(state.past[0]).toEqual({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
expect(state.future).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("pushSnapshot does nothing during batch action", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.setState({ isBatchAction: true });
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(store.getState().past.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("undo restores last snapshot and pushes current snapshot to future", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
// initial state
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
|
||||||
|
// modified state
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
store.getState().undo();
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}]);
|
||||||
|
expect(store.getState().future.length).toBe(1);
|
||||||
|
expect(store.getState().future[0]).toEqual({
|
||||||
|
nodes: [{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("undo does nothing when past is empty", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({past: []});
|
||||||
|
|
||||||
|
act(() => { store.getState().undo(); });
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([]);
|
||||||
|
expect(store.getState().future).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("redo restores last future snapshot and pushes current to past", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
// initial
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().pushSnapshot();
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
store.getState().undo();
|
||||||
|
|
||||||
|
// redo should restore node with id 'B'
|
||||||
|
store.getState().redo();
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([{
|
||||||
|
id: 'B',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'B'}
|
||||||
|
}]);
|
||||||
|
expect(store.getState().past.length).toBe(1); // snapshot A stored
|
||||||
|
expect(store.getState().past[0]).toEqual({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("redo does nothing when future is empty", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({past: []});
|
||||||
|
act(() => { store.getState().redo(); });
|
||||||
|
|
||||||
|
expect(store.getState().nodes).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("beginBatchAction pushes snapshot and sets isBatchAction=true", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({
|
||||||
|
nodes: [{
|
||||||
|
id: 'A',
|
||||||
|
type: 'default',
|
||||||
|
position: {x: 0, y: 0},
|
||||||
|
data: {label: 'A'}
|
||||||
|
}],
|
||||||
|
edges: []
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => { store.getState().beginBatchAction(); });
|
||||||
|
|
||||||
|
expect(store.getState().isBatchAction).toBe(true);
|
||||||
|
expect(store.getState().past.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("endBatchAction sets isBatchAction=false after timeout", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
store.setState({ isBatchAction: true });
|
||||||
|
act(() => { store.getState().endBatchAction(); });
|
||||||
|
|
||||||
|
// isBatchAction should remain true before the timer has advanced
|
||||||
|
expect(store.getState().isBatchAction).toBe(true);
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(10);
|
||||||
|
|
||||||
|
// it should now be set to false as the timer has advanced enough
|
||||||
|
expect(store.getState().isBatchAction).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("multiple beginBatchAction calls clear the timeout", () => {
|
||||||
|
const store = useFlowStore;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
store.getState().beginBatchAction();
|
||||||
|
store.getState().endBatchAction(); // starts timeout
|
||||||
|
store.getState().beginBatchAction(); // should clear previous timeout
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(10);
|
||||||
|
|
||||||
|
// After advancing the timers, isBatchAction should still be true,
|
||||||
|
// as the timeout should have been cleared
|
||||||
|
expect(store.getState().isBatchAction).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +1,106 @@
|
|||||||
describe('Not implemented', () => {
|
import { getByTestId, render } from '@testing-library/react';
|
||||||
test('nothing yet', () => {
|
import userEvent from '@testing-library/user-event';
|
||||||
expect(true)
|
import useFlowStore from '../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores';
|
||||||
|
import VisProgPage from '../../../../../src/pages/VisProgPage/VisProg';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ResizeObserver {
|
||||||
|
observe() {}
|
||||||
|
unobserve() {}
|
||||||
|
disconnect() {}
|
||||||
|
}
|
||||||
|
window.ResizeObserver = ResizeObserver;
|
||||||
|
|
||||||
|
jest.mock('@neodrag/react', () => ({
|
||||||
|
useDraggable: (ref: React.RefObject<HTMLElement>, options: any) => {
|
||||||
|
// We access the real useEffect from React to attach a listener
|
||||||
|
// This bridges the gap between the test's userEvent and the component's logic
|
||||||
|
const { useEffect } = jest.requireActual('react');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const element = ref.current;
|
||||||
|
if (!element) return;
|
||||||
|
|
||||||
|
// When the test fires a "pointerup" (end of click/drag),
|
||||||
|
// we manually trigger the library's onDragEnd callback.
|
||||||
|
const handlePointerUp = (e: PointerEvent) => {
|
||||||
|
if (options.onDragEnd) {
|
||||||
|
options.onDragEnd({ event: e });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
element.addEventListener('pointerup', handlePointerUp as EventListener);
|
||||||
|
return () => {
|
||||||
|
element.removeEventListener('pointerup', handlePointerUp as EventListener);
|
||||||
|
};
|
||||||
|
}, [ref, options]);
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
// We will mock @xyflow/react so we control screenToFlowPosition
|
||||||
|
jest.mock('@xyflow/react', () => {
|
||||||
|
const actual = jest.requireActual('@xyflow/react');
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
useReactFlow: () => ({
|
||||||
|
screenToFlowPosition: ({ x, y }: { x: number; y: number }) => ({
|
||||||
|
x: x - 100,
|
||||||
|
y: y - 100,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Drag & drop node creation", () => {
|
||||||
|
|
||||||
|
test("drops a phase node inside the canvas and adds it with transformed position", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
|
||||||
|
const { container } = render(<VisProgPage />);
|
||||||
|
|
||||||
|
// --- Mock ReactFlow bounding box ---
|
||||||
|
// Your DndToolbar checks these values:
|
||||||
|
const flowEl = container.querySelector('.react-flow');
|
||||||
|
jest.spyOn(flowEl!, 'getBoundingClientRect').mockReturnValue({
|
||||||
|
left: 0,
|
||||||
|
right: 800,
|
||||||
|
top: 0,
|
||||||
|
bottom: 600,
|
||||||
|
width: 800,
|
||||||
|
height: 600,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
toJSON: () => {},
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const phaseLabel = getByTestId(container, 'draggable-phase')
|
||||||
|
|
||||||
|
await user.pointer([
|
||||||
|
// touch the screen at element1
|
||||||
|
{keys: '[TouchA>]', target: phaseLabel},
|
||||||
|
// move the touch pointer to element2
|
||||||
|
{pointerName: 'TouchA', coords: {x: 300, y: 250}},
|
||||||
|
// release the touch pointer at the last position (element2)
|
||||||
|
{keys: '[/TouchA]'},
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Read the Zustand store
|
||||||
|
const { nodes } = useFlowStore.getState();
|
||||||
|
|
||||||
|
// --- Assertions ---
|
||||||
|
expect(nodes.length).toBe(1);
|
||||||
|
|
||||||
|
const node = nodes[0];
|
||||||
|
|
||||||
|
expect(node.type).toBe("phase");
|
||||||
|
expect(node.id).toBe("phase-1");
|
||||||
|
|
||||||
|
// screenToFlowPosition was mocked to subtract 100
|
||||||
|
expect(node.position).toEqual({
|
||||||
|
x: 200,
|
||||||
|
y: 150,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { render } from '@testing-library/react';
|
||||||
|
import { act } from '@testing-library/react';
|
||||||
|
import ScrollIntoView from '../../../../../src/components/ScrollIntoView';
|
||||||
|
|
||||||
|
test('scrolls the element into view on render', () => {
|
||||||
|
const scrollMock = jest.fn();
|
||||||
|
HTMLElement.prototype.scrollIntoView = scrollMock;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
render(<ScrollIntoView />);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(scrollMock).toHaveBeenCalledWith({ behavior: 'smooth' });
|
||||||
|
});
|
||||||
@@ -0,0 +1,744 @@
|
|||||||
|
import { describe, it, beforeEach } from '@jest/globals';
|
||||||
|
import { screen, waitFor } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import { renderWithProviders } from '../.././/./../../test-utils/test-utils';
|
||||||
|
import NormNode, { NormReduce, NormConnects, type NormNodeData } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/NormNode'
|
||||||
|
import useFlowStore from '../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores';
|
||||||
|
import type { Node } from '@xyflow/react';
|
||||||
|
import '@testing-library/jest-dom'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
describe('NormNode', () => {
|
||||||
|
let user: ReturnType<typeof userEvent.setup>;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
user = userEvent.setup();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Rendering', () => {
|
||||||
|
it('should render the norm node with default data', () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByPlaceholderText('Pepper should ...')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render with pre-populated norm text', () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Be respectful to humans',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByDisplayValue('Be respectful to humans');
|
||||||
|
expect(input).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render with selected state', () => {
|
||||||
|
const mockNode: Node<NormNodeData> = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const norm = screen.getByText("Norm :")
|
||||||
|
expect(norm).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render with dragging state', () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Dragged norm',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByDisplayValue('Dragged norm');
|
||||||
|
expect(input).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('User Interactions', () => {
|
||||||
|
it('should update norm text when user types in the input field', async () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('Pepper should ...');
|
||||||
|
await user.type(input, 'Be polite to guests{enter}');
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const state = useFlowStore.getState();
|
||||||
|
const updatedNode = state.nodes.find(n => n.id === 'norm-1');
|
||||||
|
expect(updatedNode?.data.norm).toBe('Be polite to guests');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle clearing the norm text', async () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Initial norm text',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByDisplayValue('Initial norm text') as HTMLInputElement;
|
||||||
|
|
||||||
|
// clearing the norm text is the same as just deleting all characters one by one
|
||||||
|
// TODO: FIGURE OUT A MORE EFFICIENT WAY, I"M SORRY, user.clear() just doesn't work:/
|
||||||
|
for (let a = 0; a < 'Initial norm text'.length; a++){
|
||||||
|
await user.type(input, '{backspace}')
|
||||||
|
}
|
||||||
|
await user.type(input,'{enter}')
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const state = useFlowStore.getState();
|
||||||
|
const updatedNode = state.nodes.find(n => n.id === 'norm-1');
|
||||||
|
expect(updatedNode?.data.norm).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update norm text multiple times', async () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('Pepper should ...');
|
||||||
|
await user.type(input, 'First norm{enter}');
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe('First norm');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: FIGURE OUT A MORE EFFICIENT WAY, I"M SORRY, user.clear() just doesn't work:/
|
||||||
|
for (let a = 0; a < 'First norm'.length; a++){
|
||||||
|
await user.type(input, '{backspace}')
|
||||||
|
}
|
||||||
|
|
||||||
|
await user.type(input, 'Second norm{enter}');
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe('Second norm');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle special characters in norm text', async () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('Pepper should ...');
|
||||||
|
await user.type(input, "Don't harm & be nice!{enter}" );
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe("Don't harm & be nice!");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle long norm text', async () => {
|
||||||
|
const longText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('Pepper should ...');
|
||||||
|
await user.type(input, longText);
|
||||||
|
await user.type(input, "{enter}")
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe(longText);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('NormReduce Function', () => {
|
||||||
|
it('should reduce a norm node to its essential data', () => {
|
||||||
|
const normNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Safety Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Never harm humans',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const allNodes: Node[] = [normNode];
|
||||||
|
const result = NormReduce(normNode, allNodes);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: 'norm-1',
|
||||||
|
label: 'Safety Norm',
|
||||||
|
norm: 'Never harm humans',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reduce multiple norm nodes independently', () => {
|
||||||
|
const norm1: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Norm 1',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Be helpful',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const norm2: Node = {
|
||||||
|
id: 'norm-2',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 100, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Norm 2',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Be honest',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const allNodes: Node[] = [norm1, norm2];
|
||||||
|
|
||||||
|
const result1 = NormReduce(norm1, allNodes);
|
||||||
|
const result2 = NormReduce(norm2, allNodes);
|
||||||
|
|
||||||
|
expect(result1.id).toBe('norm-1');
|
||||||
|
expect(result1.norm).toBe('Be helpful');
|
||||||
|
expect(result2.id).toBe('norm-2');
|
||||||
|
expect(result2.norm).toBe('Be honest');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle empty norm text', () => {
|
||||||
|
const normNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Empty Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = NormReduce(normNode, [normNode]);
|
||||||
|
|
||||||
|
expect(result.norm).toBe('');
|
||||||
|
expect(result.id).toBe('norm-1');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should preserve node label in reduction', () => {
|
||||||
|
const normNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Custom Label',
|
||||||
|
droppable: false,
|
||||||
|
norm: 'Test norm',
|
||||||
|
hasReduce: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = NormReduce(normNode, [normNode]);
|
||||||
|
|
||||||
|
expect(result.label).toBe('Custom Label');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('NormConnects Function', () => {
|
||||||
|
it('should handle connection without errors', () => {
|
||||||
|
const normNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Test',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const phaseNode: Node = {
|
||||||
|
id: 'phase-1',
|
||||||
|
type: 'phase',
|
||||||
|
position: { x: 100, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Phase 1',
|
||||||
|
droppable: true,
|
||||||
|
children: [],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
NormConnects(normNode, phaseNode, true);
|
||||||
|
}).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle connection when norm is target', () => {
|
||||||
|
const normNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Test',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const phaseNode: Node = {
|
||||||
|
id: 'phase-1',
|
||||||
|
type: 'phase',
|
||||||
|
position: { x: 100, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Phase 1',
|
||||||
|
droppable: true,
|
||||||
|
children: [],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
NormConnects(normNode, phaseNode, false);
|
||||||
|
}).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle self-connection', () => {
|
||||||
|
const normNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Test',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
NormConnects(normNode, normNode, true);
|
||||||
|
}).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Integration with Store', () => {
|
||||||
|
it('should properly update the store when editing norm text', async () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: '',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('Pepper should ...');
|
||||||
|
|
||||||
|
// TODO: FIGURE OUT A MORE EFFICIENT WAY, I"M SORRY, user.clear() just doesn't work:/
|
||||||
|
for (let a = 0; a < 20; a++){
|
||||||
|
await user.type(input, '{backspace}')
|
||||||
|
}
|
||||||
|
await user.type(input, 'New norm value{enter}');
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const state = useFlowStore.getState();
|
||||||
|
expect(state.nodes).toHaveLength(1);
|
||||||
|
expect(state.nodes[0].id).toBe('norm-1');
|
||||||
|
expect(state.nodes[0].data.norm).toBe('New norm value');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not affect other nodes when updating one norm node', async () => {
|
||||||
|
const norm1: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Norm 1',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Original norm 1',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const norm2: Node = {
|
||||||
|
id: 'norm-2',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 100, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Norm 2',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'Original norm 2',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [norm1, norm2],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={norm1.id}
|
||||||
|
type={norm1.type as string}
|
||||||
|
data={norm1.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByDisplayValue('Original norm 1') as HTMLInputElement;
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: FIGURE OUT A MORE EFFICIENT WAY, I"M SORRY, user.clear() just doesn't work:/
|
||||||
|
for (let a = 0; a < 20; a++){
|
||||||
|
await user.type(input, '{backspace}')
|
||||||
|
}
|
||||||
|
await user.type(input, 'Updated norm 1{enter}');
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const state = useFlowStore.getState();
|
||||||
|
const updatedNorm1 = state.nodes.find(n => n.id === 'norm-1');
|
||||||
|
const unchangedNorm2 = state.nodes.find(n => n.id === 'norm-2');
|
||||||
|
|
||||||
|
expect(updatedNorm1?.data.norm).toBe('Updated norm 1');
|
||||||
|
expect(unchangedNorm2?.data.norm).toBe('Original norm 2');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should maintain data consistency with multiple rapid updates', async () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Test Norm',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'haa haa fuyaaah - link',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({
|
||||||
|
nodes: [mockNode],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<NormNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('Pepper should ...');
|
||||||
|
|
||||||
|
await user.type(input, 'a');
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe('haa haa fuyaaah - link');
|
||||||
|
});
|
||||||
|
|
||||||
|
await user.type(input, 'b');
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe('haa haa fuyaaah - link');
|
||||||
|
});
|
||||||
|
|
||||||
|
await user.type(input, 'c');
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(useFlowStore.getState().nodes[0].data.norm).toBe('haa haa fuyaaah - link');
|
||||||
|
}, { timeout: 3000 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
import { describe, it } from '@jest/globals';
|
||||||
|
import '@testing-library/jest-dom';
|
||||||
|
import { screen } from '@testing-library/react';
|
||||||
|
import type { Node } from '@xyflow/react';
|
||||||
|
import { renderWithProviders } from '../.././/./../../test-utils/test-utils';
|
||||||
|
import StartNode, { StartReduce, StartConnects } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/StartNode';
|
||||||
|
|
||||||
|
|
||||||
|
describe('StartNode', () => {
|
||||||
|
|
||||||
|
|
||||||
|
describe('Rendering', () => {
|
||||||
|
it('renders the StartNode correctly', () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'start-1',
|
||||||
|
type: 'start', // TypeScript now knows this is a string
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Start Node',
|
||||||
|
droppable: false,
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<StartNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type!} // <--- fix here
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={false}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
expect(screen.getByText('Start')).toBeInTheDocument();
|
||||||
|
|
||||||
|
// The handle should exist in the DOM
|
||||||
|
expect(document.querySelector('[data-handleid="source"]')).toBeInTheDocument();
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('StartReduce Function', () => {
|
||||||
|
it('reduces the StartNode to its minimal structure', () => {
|
||||||
|
const mockNode: Node = {
|
||||||
|
id: 'start-1',
|
||||||
|
type: 'start',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Start Node',
|
||||||
|
droppable: false,
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = StartReduce(mockNode, [mockNode]);
|
||||||
|
expect(result).toEqual({ id: 'start-1' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('StartConnects Function', () => {
|
||||||
|
it('handles connections without throwing', () => {
|
||||||
|
const startNode: Node = {
|
||||||
|
id: 'start-1',
|
||||||
|
type: 'start',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Start Node',
|
||||||
|
droppable: false,
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const otherNode: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 100, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Norm Node',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'test',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => StartConnects(startNode, otherNode, true)).not.toThrow();
|
||||||
|
expect(() => StartConnects(startNode, otherNode, false)).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,246 @@
|
|||||||
|
import { describe, it, beforeEach } from '@jest/globals';
|
||||||
|
import { screen, waitFor } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import { renderWithProviders } from '../.././/./../../test-utils/test-utils';
|
||||||
|
import TriggerNode, { TriggerReduce, TriggerConnects, TriggerNodeCanConnect, type TriggerNodeData } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode';
|
||||||
|
import useFlowStore from '../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores';
|
||||||
|
import type { Node } from '@xyflow/react';
|
||||||
|
import '@testing-library/jest-dom';
|
||||||
|
|
||||||
|
describe('TriggerNode', () => {
|
||||||
|
let user: ReturnType<typeof userEvent.setup>;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
user = userEvent.setup();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Rendering', () => {
|
||||||
|
it('should render TriggerNode with keywords type', () => {
|
||||||
|
const mockNode: Node<TriggerNodeData> = {
|
||||||
|
id: 'trigger-1',
|
||||||
|
type: 'trigger',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Keyword Trigger',
|
||||||
|
droppable: true,
|
||||||
|
triggerType: 'keywords',
|
||||||
|
triggers: [],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<TriggerNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/Triggers when the keyword is spoken/i)).toBeInTheDocument();
|
||||||
|
expect(screen.getByPlaceholderText('...')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render TriggerNode with emotion type', () => {
|
||||||
|
const mockNode: Node<TriggerNodeData> = {
|
||||||
|
id: 'trigger-2',
|
||||||
|
type: 'trigger',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Emotion Trigger',
|
||||||
|
droppable: true,
|
||||||
|
triggerType: 'emotion',
|
||||||
|
triggers: [],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<TriggerNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/Emotion\?/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('User Interactions', () => {
|
||||||
|
it('should add a new keyword', async () => {
|
||||||
|
const mockNode: Node<TriggerNodeData> = {
|
||||||
|
id: 'trigger-1',
|
||||||
|
type: 'trigger',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Keyword Trigger',
|
||||||
|
droppable: true,
|
||||||
|
triggerType: 'keywords',
|
||||||
|
triggers: [],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({ nodes: [mockNode], edges: [] });
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<TriggerNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText('...');
|
||||||
|
await user.type(input, 'hello{enter}');
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const node = useFlowStore.getState().nodes.find(n => n.id === 'trigger-1') as Node<TriggerNodeData> | undefined;
|
||||||
|
expect(node?.data.triggers.length).toBe(1);
|
||||||
|
expect(node?.data.triggers[0].keyword).toBe('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove a keyword when cleared', async () => {
|
||||||
|
const mockNode: Node<TriggerNodeData> = {
|
||||||
|
id: 'trigger-1',
|
||||||
|
type: 'trigger',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Keyword Trigger',
|
||||||
|
droppable: true,
|
||||||
|
triggerType: 'keywords',
|
||||||
|
triggers: [{ id: 'kw1', keyword: 'hello' }],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
useFlowStore.setState({ nodes: [mockNode], edges: [] });
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<TriggerNode
|
||||||
|
id={mockNode.id}
|
||||||
|
type={mockNode.type as string}
|
||||||
|
data={mockNode.data as any}
|
||||||
|
selected={false}
|
||||||
|
isConnectable={true}
|
||||||
|
zIndex={0}
|
||||||
|
dragging={false}
|
||||||
|
selectable={true}
|
||||||
|
deletable={true}
|
||||||
|
draggable={true}
|
||||||
|
positionAbsoluteX={0}
|
||||||
|
positionAbsoluteY={0}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = screen.getByDisplayValue('hello');
|
||||||
|
for (let i = 0; i < 'hello'.length; i++) {
|
||||||
|
await user.type(input, '{backspace}');
|
||||||
|
}
|
||||||
|
await user.type(input, '{enter}');
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const node = useFlowStore.getState().nodes.find(n => n.id === 'trigger-1') as Node<TriggerNodeData> | undefined;
|
||||||
|
expect(node?.data.triggers.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('TriggerReduce Function', () => {
|
||||||
|
it('should reduce a trigger node to its essential data', () => {
|
||||||
|
const triggerNode: Node = {
|
||||||
|
id: 'trigger-1',
|
||||||
|
type: 'trigger',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Keyword Trigger',
|
||||||
|
droppable: true,
|
||||||
|
triggerType: 'keywords',
|
||||||
|
triggers: [{ id: 'kw1', keyword: 'hello' }],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const allNodes: Node[] = [triggerNode];
|
||||||
|
const result = TriggerReduce(triggerNode, allNodes);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: 'trigger-1',
|
||||||
|
type: 'keywords',
|
||||||
|
label: 'Keyword Trigger',
|
||||||
|
keywords: [{ id: 'kw1', keyword: 'hello' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('TriggerConnects Function', () => {
|
||||||
|
it('should handle connection without errors', () => {
|
||||||
|
const node1: Node = {
|
||||||
|
id: 'trigger-1',
|
||||||
|
type: 'trigger',
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Trigger 1',
|
||||||
|
droppable: true,
|
||||||
|
triggerType: 'keywords',
|
||||||
|
triggers: [],
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const node2: Node = {
|
||||||
|
id: 'norm-1',
|
||||||
|
type: 'norm',
|
||||||
|
position: { x: 100, y: 0 },
|
||||||
|
data: {
|
||||||
|
label: 'Norm 1',
|
||||||
|
droppable: true,
|
||||||
|
norm: 'test',
|
||||||
|
hasReduce: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
TriggerConnects(node1, node2, true);
|
||||||
|
TriggerConnects(node1, node2, false);
|
||||||
|
}).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true for TriggerNodeCanConnect if connection exists', () => {
|
||||||
|
const connection = { source: 'trigger-1', target: 'norm-1' };
|
||||||
|
expect(TriggerNodeCanConnect(connection as any)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
import { describe, beforeEach } from '@jest/globals';
|
||||||
|
import { screen } from '@testing-library/react';
|
||||||
|
import { renderWithProviders } from '../.././/./../../test-utils/test-utils';
|
||||||
|
import type { XYPosition } from '@xyflow/react';
|
||||||
|
import { NodeTypes, NodeDefaults, NodeConnects, NodeReduces, NodesInPhase } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/NodeRegistry';
|
||||||
|
import '@testing-library/jest-dom'
|
||||||
|
import { createElement } from 'react';
|
||||||
|
import useFlowStore from '../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores';
|
||||||
|
|
||||||
|
|
||||||
|
describe('NormNode', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
function createNode(id: string, type: string, position: XYPosition, data: Record<string, unknown>, deletable?: boolean) {
|
||||||
|
const defaultData = NodeDefaults[type as keyof typeof NodeDefaults]
|
||||||
|
const newData = {
|
||||||
|
id: id,
|
||||||
|
type: type,
|
||||||
|
position: position,
|
||||||
|
data: data,
|
||||||
|
deletable: deletable,
|
||||||
|
}
|
||||||
|
return {...defaultData, ...newData}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces the graph into its phases' information and recursively calls their reducing function
|
||||||
|
*/
|
||||||
|
function graphReducer() {
|
||||||
|
const { nodes } = useFlowStore.getState();
|
||||||
|
return nodes
|
||||||
|
.filter((n) => n.type == 'phase')
|
||||||
|
.map((n) => {
|
||||||
|
const reducer = NodeReduces['phase'];
|
||||||
|
return reducer(n, nodes)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAllTypes() {
|
||||||
|
return Object.entries(NodeTypes).map(([t])=>t)
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Rendering', () => {
|
||||||
|
test.each(getAllTypes())('it should render %s node with the default data', (nodeType) => {
|
||||||
|
const lengthBefore = screen.getAllByText(/.*/).length;
|
||||||
|
|
||||||
|
const newNode = createNode(nodeType + "1", nodeType, {x: 200, y:200}, {});
|
||||||
|
|
||||||
|
const found = Object.entries(NodeTypes).find(([t]) => t === nodeType);
|
||||||
|
const uiElement = found ? found[1] : null;
|
||||||
|
|
||||||
|
expect(uiElement).not.toBeNull();
|
||||||
|
const props = {
|
||||||
|
id: newNode.id,
|
||||||
|
type: newNode.type as string,
|
||||||
|
data: newNode.data as any,
|
||||||
|
selected: false,
|
||||||
|
isConnectable: true,
|
||||||
|
zIndex: 0,
|
||||||
|
dragging: false,
|
||||||
|
selectable: true,
|
||||||
|
deletable: true,
|
||||||
|
draggable: true,
|
||||||
|
positionAbsoluteX: 0,
|
||||||
|
positionAbsoluteY: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
renderWithProviders(createElement(uiElement as React.ComponentType<any>, props));
|
||||||
|
const lengthAfter = screen.getAllByText(/.*/).length;
|
||||||
|
|
||||||
|
expect(lengthBefore + 1 === lengthAfter);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('Connecting', () => {
|
||||||
|
test.each(getAllTypes())('it should call the connect function when %s node is connected', (nodeType) => {
|
||||||
|
// Create two nodes - one of the current type and one to connect to
|
||||||
|
const sourceNode = createNode('source-1', nodeType, {x: 100, y: 100}, {});
|
||||||
|
const targetNode = createNode('target-1', 'end', {x: 300, y: 100}, {});
|
||||||
|
|
||||||
|
// Add nodes to store
|
||||||
|
useFlowStore.setState({ nodes: [sourceNode, targetNode] });
|
||||||
|
|
||||||
|
// Spy on the connect functions
|
||||||
|
const sourceConnectSpy = jest.spyOn(NodeConnects, nodeType as keyof typeof NodeConnects);
|
||||||
|
const targetConnectSpy = jest.spyOn(NodeConnects, 'end');
|
||||||
|
|
||||||
|
// Simulate connection
|
||||||
|
useFlowStore.getState().onConnect({
|
||||||
|
source: 'source-1',
|
||||||
|
target: 'target-1',
|
||||||
|
sourceHandle: null,
|
||||||
|
targetHandle: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the connect functions were called
|
||||||
|
expect(sourceConnectSpy).toHaveBeenCalledWith(sourceNode, targetNode, true);
|
||||||
|
expect(targetConnectSpy).toHaveBeenCalledWith(targetNode, sourceNode, false);
|
||||||
|
|
||||||
|
sourceConnectSpy.mockRestore();
|
||||||
|
targetConnectSpy.mockRestore();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Reducing', () => {
|
||||||
|
test.each(getAllTypes())('it should correctly call/ not call the reduce function when %s node is in a phase', (nodeType) => {
|
||||||
|
// Create a phase node and a node of the current type
|
||||||
|
const phaseNode = createNode('phase-1', 'phase', {x: 200, y: 100}, { label: 'Test Phase', children: [] });
|
||||||
|
const testNode = createNode('node-1', nodeType, {x: 100, y: 100}, {});
|
||||||
|
|
||||||
|
// Add the test node as a child of the phase
|
||||||
|
(phaseNode.data as any).children.push(testNode.id);
|
||||||
|
|
||||||
|
// Add nodes to store
|
||||||
|
useFlowStore.setState({ nodes: [phaseNode, testNode] });
|
||||||
|
|
||||||
|
// Spy on the reduce functions
|
||||||
|
const phaseReduceSpy = jest.spyOn(NodeReduces, 'phase');
|
||||||
|
const nodeReduceSpy = jest.spyOn(NodeReduces, nodeType as keyof typeof NodeReduces);
|
||||||
|
|
||||||
|
// Simulate reducing - using the graphReducer
|
||||||
|
const result = graphReducer();
|
||||||
|
|
||||||
|
// Verify the reduce functions were called
|
||||||
|
expect(phaseReduceSpy).toHaveBeenCalledWith(phaseNode, [phaseNode, testNode]);
|
||||||
|
// Check if this node type is in NodesInPhase and returns false
|
||||||
|
const nodesInPhaseFunc = NodesInPhase[nodeType as keyof typeof NodesInPhase];
|
||||||
|
if (nodesInPhaseFunc && nodesInPhaseFunc() === false && nodeType !== 'phase') {
|
||||||
|
// Node is NOT in phase, so it should NOT be called
|
||||||
|
expect(nodeReduceSpy).not.toHaveBeenCalled();
|
||||||
|
} else {
|
||||||
|
// Node IS in phase, so it SHOULD be called
|
||||||
|
expect(nodeReduceSpy).toHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the correct structure is present using NodesInPhase
|
||||||
|
expect(result).toHaveLength(nodeType !== 'phase' ? 1 : 2);
|
||||||
|
expect(result[0]).toHaveProperty('id', 'phase-1');
|
||||||
|
expect(result[0]).toHaveProperty('label', 'Test Phase');
|
||||||
|
|
||||||
|
// Restore mocks
|
||||||
|
phaseReduceSpy.mockRestore();
|
||||||
|
nodeReduceSpy.mockRestore();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -69,6 +69,9 @@ beforeAll(() => {
|
|||||||
useFlowStore.setState({
|
useFlowStore.setState({
|
||||||
nodes: [],
|
nodes: [],
|
||||||
edges: [],
|
edges: [],
|
||||||
|
past: [],
|
||||||
|
future: [],
|
||||||
|
isBatchAction: false,
|
||||||
edgeReconnectSuccessful: true
|
edgeReconnectSuccessful: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -78,6 +81,9 @@ afterEach(() => {
|
|||||||
useFlowStore.setState({
|
useFlowStore.setState({
|
||||||
nodes: [],
|
nodes: [],
|
||||||
edges: [],
|
edges: [],
|
||||||
|
past: [],
|
||||||
|
future: [],
|
||||||
|
isBatchAction: false,
|
||||||
edgeReconnectSuccessful: true
|
edgeReconnectSuccessful: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
41
test/test-utils/mocks.ts
Normal file
41
test/test-utils/mocks.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { jest } from '@jest/globals';
|
||||||
|
import React from 'react';
|
||||||
|
import '@testing-library/jest-dom';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock for @xyflow/react
|
||||||
|
* Provides simplified versions of React Flow hooks and components
|
||||||
|
*/
|
||||||
|
jest.mock('@xyflow/react', () => ({
|
||||||
|
useReactFlow: jest.fn(() => ({
|
||||||
|
screenToFlowPosition: jest.fn((pos: any) => pos),
|
||||||
|
getNode: jest.fn(),
|
||||||
|
getNodes: jest.fn(() => []),
|
||||||
|
getEdges: jest.fn(() => []),
|
||||||
|
setNodes: jest.fn(),
|
||||||
|
setEdges: jest.fn(),
|
||||||
|
})),
|
||||||
|
ReactFlowProvider: ({ children }: { children: React.ReactNode }) =>
|
||||||
|
React.createElement('div', { 'data-testid': 'react-flow-provider' }, children),
|
||||||
|
ReactFlow: ({ children, ...props }: any) =>
|
||||||
|
React.createElement('div', { 'data-testid': 'react-flow', ...props }, children),
|
||||||
|
Handle: ({ type, position, id }: any) =>
|
||||||
|
React.createElement('div', { 'data-testid': `handle-${type}-${id}`, 'data-position': position }),
|
||||||
|
Panel: ({ children, position }: any) =>
|
||||||
|
React.createElement('div', { 'data-testid': 'panel', 'data-position': position }, children),
|
||||||
|
Controls: () => React.createElement('div', { 'data-testid': 'controls' }),
|
||||||
|
Background: () => React.createElement('div', { 'data-testid': 'background' }),
|
||||||
|
}));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock for @neodrag/react
|
||||||
|
* Simplifies drag behavior for testing
|
||||||
|
*/
|
||||||
|
jest.mock('@neodrag/react', () => ({
|
||||||
|
useDraggable: jest.fn((ref: any, options?: any) => {
|
||||||
|
// Store the options so we can trigger them in tests
|
||||||
|
if (ref && ref.current) {
|
||||||
|
(ref.current as any)._dragOptions = options;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}));
|
||||||
24
test/test-utils/test-utils.tsx
Normal file
24
test/test-utils/test-utils.tsx
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// __tests__/utils/test-utils.tsx
|
||||||
|
import { render, type RenderOptions } from '@testing-library/react';
|
||||||
|
import { type ReactElement, type ReactNode } from 'react';
|
||||||
|
import { ReactFlowProvider } from '@xyflow/react';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom render function that wraps components with necessary providers
|
||||||
|
* This ensures all components have access to ReactFlow context
|
||||||
|
*/
|
||||||
|
export function renderWithProviders(
|
||||||
|
ui: ReactElement,
|
||||||
|
options?: Omit<RenderOptions, 'wrapper'>
|
||||||
|
) {
|
||||||
|
function Wrapper({ children }: { children: ReactNode }) {
|
||||||
|
return <ReactFlowProvider>{children}</ReactFlowProvider>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return render(ui, { wrapper: Wrapper, ...options });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Re-export everything from testing library
|
||||||
|
//eslint-disable-next-line react-refresh/only-export-components
|
||||||
|
export * from '@testing-library/react';
|
||||||
Reference in New Issue
Block a user