fix: fix the tests by simulating user actions rather than the function, and avoid the cyclic dependancy which was present

ref: N25B-371
This commit is contained in:
Björn Otgaar
2025-12-04 12:33:27 +01:00
parent c167144b4d
commit 95397ceccc
5 changed files with 111 additions and 51 deletions

View File

@@ -1,16 +1,78 @@
import useFlowStore from '../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores';
import addNode from "../../../../../src/pages/VisProgPage/visualProgrammingUI/utils/AddNode";
import type { PhaseNodeData } from "../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/PhaseNode";
import { getByTestId, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
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]);
},
}));
describe('PhaseNode', () => {
it('each created phase gets its own children array, not the same reference ', () => {
// Create nodes
addNode("phase", {x:10,y:10})
addNode("phase", {x:20,y:20})
addNode("norm", {x:30,y:30})
addNode("norm", {x:40,y:40})
addNode("goal", {x:50,y:50})
it('each created phase gets its own children array, not the same reference ', 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: () => {},
});
// Find the draggable norm node in the toolbar
const phaseButton = getByTestId(container, 'draggable-phase')
// Simulate dropping phase down in graph (twice)
for (let i = 0; i < 2; i++) {
await user.pointer([
// touch the screen at element1
{keys: '[TouchA>]', target: phaseButton},
// move the touch pointer to element2
{pointerName: 'TouchA', coords: {x: 300, y: 250}},
// release the touch pointer at the last position (element2)
{keys: '[/TouchA]'},
]);
}
// Find nodes
const nodes = useFlowStore.getState().nodes;