Skip to content

Commit

Permalink
fix(test): selection
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Oct 3, 2019
1 parent 68ec185 commit b95883a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
18 changes: 11 additions & 7 deletions cypress/integration/flow/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ describe('Basic Flow Rendering', () => {
cy.get('.react-graph__node:first').should('not.have.class', 'selected');
});

it('select all nodes', () => {
// @FIX: why is there no selection__pane visible?
// https://docs.cypress.io/api/commands/type.html#Do-a-shift-click
it('selects all nodes', () => {
cy.get('body')
.type('{shift}', { release: false })
.type('{Shift}', { release: false })
.get('.react-graph__selectionpane')
.trigger('mousedown', 'topLeft', { which: 1, force: true })
.trigger('mousemove', 'bottomRight', { which: 1 })
.trigger('mouseup', 'bottomRight', { force: true });
.trigger('mouseup', 'bottomRight', { force: true })
.get('.react-graph__node')
.should('have.class', 'selected')
.get('.react-graph__nodesselection-rect');
});

cy.get('.react-graph__node').should('have.class', 'selected');
it('remove selection', () => {
cy.get('.react-graph__renderer').click('bottomRight');
cy.get('.react-graph__nodesselection-rect').should('not.exist');
});

it('selects an edge', () => {
Expand All @@ -55,7 +59,7 @@ describe('Basic Flow Rendering', () => {
cy.get('.react-graph__edge').should('have.length', 1);
});

it('connect nodes', () => {
it('connects nodes', () => {
cy.get('.react-graph__node')
.contains('Node 3')
.find('.react-graph__handle.source')
Expand Down
8 changes: 4 additions & 4 deletions src/GlobalKeyHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useStoreState, useStoreActions } from 'easy-peasy';
import useKeyPress from '../hooks/useKeyPress';
import { isEdge, getConnectedEdges } from '../graph-utils';

export default memo(({ deleteKey, onElementsRemove }) => {
export default memo(({ deleteKeyCode, onElementsRemove }) => {
const state = useStoreState(s => ({ selectedElements: s.selectedElements, edges: s.edges }))
const setNodesSelection = useStoreActions(a => a.setNodesSelection);
const removePressed = useKeyPress(deleteKey);
const deleteKeyPressed = useKeyPress(deleteKeyCode);

useEffect(() => {
if (removePressed && state.selectedElements.length) {
if (deleteKeyPressed && state.selectedElements.length) {
let elementsToRemove = state.selectedElements;

// we also want to remove the edges if only one node is selected
Expand All @@ -22,7 +22,7 @@ export default memo(({ deleteKey, onElementsRemove }) => {
onElementsRemove(elementsToRemove);
setNodesSelection({ isActive: false });
}
}, [removePressed])
}, [deleteKeyPressed])

return null;
});
4 changes: 2 additions & 2 deletions src/GraphView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { fitView, zoomIn, zoomOut } from '../graph-utils';
const GraphView = memo(({
nodeTypes, edgeTypes, onMove, onLoad,
onElementClick, onNodeDragStop, connectionLineType, connectionLineStyle,
selectionKey
selectionKeyCode
}) => {
const zoomPane = useRef();
const rendererNode = useRef();
Expand All @@ -28,7 +28,7 @@ const GraphView = memo(({
const updateSize = useStoreActions(actions => actions.updateSize);
const setNodesSelection = useStoreActions(actions => actions.setNodesSelection);

const selectionKeyPressed = useKeyPress(selectionKey);
const selectionKeyPressed = useKeyPress(selectionKeyCode);
const updateDimensions = () => {
const size = getDimensions(rendererNode.current);
updateSize(size);
Expand Down
10 changes: 5 additions & 5 deletions src/ReactGraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ReactGraph = ({
style, onElementClick, elements, children,
nodeTypes, edgeTypes, onLoad, onMove, onElementsRemove,
onConnect, onNodeDragStop, connectionLineType, connectionLineStyle,
deleteKey, selectionKey
deleteKeyCode, selectionKeyCode
}) => {
const nodeTypesParsed = useMemo(() => createNodeTypes(nodeTypes), []);
const edgeTypesParsed = useMemo(() => createEdgeTypes(edgeTypes), []);
Expand All @@ -45,11 +45,11 @@ const ReactGraph = ({
edgeTypes={edgeTypesParsed}
connectionLineType={connectionLineType}
connectionLineStyle={connectionLineStyle}
selectionKey={selectionKey}
selectionKeyCode={selectionKeyCode}
/>
<GlobalKeyHandler
onElementsRemove={onElementsRemove}
deleteKey={deleteKey}
deleteKeyCode={deleteKeyCode}
/>
{children}
</StoreProvider>
Expand Down Expand Up @@ -78,8 +78,8 @@ ReactGraph.defaultProps = {
},
connectionLineType: 'bezier',
connectionLineStyle: {},
deleteKey: 'Backspace',
selectionKey: 'Shift'
deleteKeyCode: 8,
selectionKeyCode: 16
};

export default ReactGraph;
11 changes: 6 additions & 5 deletions src/hooks/useKeyPress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { useState, useEffect } from 'react';

import { isInputNode } from '../utils';

export default function useKeyPress(targetKey) {
export default function useKeyPress(keyCode) {
const [keyPressed, setKeyPressed] = useState(false);

function downHandler({ key, target }) {
if (key === targetKey && !isInputNode(target)) {
function downHandler(evt) {
console.log(keyCode, evt.keyCode);
if (evt.keyCode === keyCode && !isInputNode(evt.target)) {
setKeyPressed(true);
}
}

const upHandler = ({ key, target }) => {
if (key === targetKey && !isInputNode(target)) {
const upHandler = (evt) => {
if (evt.keyCode === keyCode && !isInputNode(evt.target)) {
setKeyPressed(false);
}
};
Expand Down

0 comments on commit b95883a

Please sign in to comment.