fix: extract handleNodeDoubleClick into useCallback and add tests
Replace inline arrow with named useCallback handler to avoid creating a new fn ref on every render. Add two CanvasContainer tests covering the double-click callback and the no-op path when prop is omitted.
This commit is contained in:
@@ -343,6 +343,10 @@ export default function App() {
|
|||||||
setEditEdgeId(edge.id)
|
setEditEdgeId(edge.id)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const handleNodeDoubleClick = useCallback((node: Node<NodeData>) => {
|
||||||
|
handleEditNode(node.id)
|
||||||
|
}, [handleEditNode])
|
||||||
|
|
||||||
const handleEdgeUpdate = useCallback((data: EdgeData) => {
|
const handleEdgeUpdate = useCallback((data: EdgeData) => {
|
||||||
if (!editEdgeId) return
|
if (!editEdgeId) return
|
||||||
snapshotHistory()
|
snapshotHistory()
|
||||||
@@ -400,7 +404,7 @@ export default function App() {
|
|||||||
<CanvasContainer
|
<CanvasContainer
|
||||||
onConnect={handleEdgeConnect}
|
onConnect={handleEdgeConnect}
|
||||||
onEdgeDoubleClick={handleEdgeDoubleClick}
|
onEdgeDoubleClick={handleEdgeDoubleClick}
|
||||||
onNodeDoubleClick={(node) => handleEditNode(node.id)}
|
onNodeDoubleClick={handleNodeDoubleClick}
|
||||||
onNodeDragStart={snapshotHistory}
|
onNodeDragStart={snapshotHistory}
|
||||||
onOpenPending={(deviceId) => {
|
onOpenPending={(deviceId) => {
|
||||||
setHighlightPendingId(undefined)
|
setHighlightPendingId(undefined)
|
||||||
|
|||||||
@@ -104,6 +104,24 @@ describe('CanvasContainer', () => {
|
|||||||
}).not.toThrow()
|
}).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// ── Node double-click ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
it('calls onNodeDoubleClick prop when a node is double-clicked', () => {
|
||||||
|
const onNodeDoubleClick = vi.fn()
|
||||||
|
const node = makeNode('n1')
|
||||||
|
render(<CanvasContainer onNodeDoubleClick={onNodeDoubleClick} />)
|
||||||
|
;(rfProps.onNodeDoubleClick as (...args: unknown[]) => unknown)({} as MouseEvent, node)
|
||||||
|
expect(onNodeDoubleClick).toHaveBeenCalledWith(node)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not throw when onNodeDoubleClick is not provided', () => {
|
||||||
|
const node = makeNode('n1')
|
||||||
|
render(<CanvasContainer />)
|
||||||
|
expect(() => {
|
||||||
|
;(rfProps.onNodeDoubleClick as (...args: unknown[]) => unknown)({} as MouseEvent, node)
|
||||||
|
}).not.toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
// ── Connection validation ─────────────────────────────────────────────────
|
// ── Connection validation ─────────────────────────────────────────────────
|
||||||
|
|
||||||
it('isValidConnection returns false for self-connections', () => {
|
it('isValidConnection returns false for self-connections', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user