diff --git a/__mocks__/@neodrag/react.ts b/__mocks__/@neodrag/react.ts new file mode 100644 index 0000000..7ea5455 --- /dev/null +++ b/__mocks__/@neodrag/react.ts @@ -0,0 +1,3 @@ +jest.mock('@neodrag/react', () => ({ + useDraggable: jest.fn(), +})); \ No newline at end of file diff --git a/src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx b/src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx index 3540b75..8317518 100644 --- a/src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx +++ b/src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx @@ -69,7 +69,9 @@ function DraggableNode({className, children, nodeType, onDrop}: DraggableNodePro ); } -function addNode(nodeType: string, position: XYPosition) { + +// eslint-disable-next-line react-refresh/only-export-components +export function addNode(nodeType: string, position: XYPosition) { const {setNodes} = useFlowStore.getState(); const nds = useFlowStore.getState().nodes; const newNode = () => { diff --git a/test/pages/visProgPage/visualProgrammingUI/components/DragDropSidebar.test.tsx b/test/pages/visProgPage/visualProgrammingUI/components/DragDropSidebar.test.tsx new file mode 100644 index 0000000..da90945 --- /dev/null +++ b/test/pages/visProgPage/visualProgrammingUI/components/DragDropSidebar.test.tsx @@ -0,0 +1,20 @@ +import { mockReactFlow } from '../../../../setupFlowTests.ts'; +import {act} from "@testing-library/react"; +import useFlowStore from "../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx"; +import {addNode} from "../../../../../src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx"; + + +beforeAll(() => { + mockReactFlow(); +}); + +describe('Drag-and-Drop sidebar', () => { + test.each(['phase', 'phase'])('new nodes get added correctly', (nodeType: string) => { + act(()=> { + addNode(nodeType, {x:100, y:100}); + }) + const updatedState = useFlowStore.getState(); + expect(updatedState.nodes.length).toBe(1); + expect(updatedState.nodes[0].type).toBe(nodeType); + }); +}); \ No newline at end of file diff --git a/test/setupFlowTests.ts b/test/setupFlowTests.ts index 99920df..21a4945 100644 --- a/test/setupFlowTests.ts +++ b/test/setupFlowTests.ts @@ -58,11 +58,13 @@ export const mockReactFlow = () => { (globalThis.SVGElement as never).prototype.getBBox = () => ({ x: 0, y: 0, - width: 0, - height: 0, + width: 200, + height: 200, }); }; + + beforeAll(() => { useFlowStore.setState({ nodes: [],