Skip to content

Commit 9bbf5b7

Browse files
LonelyPrincessdanielcaldas
authored andcommitted
fix: Trigger custom click handler in collapsible nodes (#137)
* fix: Trigger custom click handler in collapsible nodes There was a mistake in the `_tick` method which caused the user's `onNodeClick` custom handler to never be triggered when the collapsible feature was enabled. The problem was that it called `setState` without using a callback parameter if the received `cb` was not empty. The contents of that ternary operator should be the opposite for things to work. This fix closes #136. * Add e2e test for node click callback
1 parent 0d59101 commit 9bbf5b7

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

cypress/integration/graph.e2e.js

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,50 +116,72 @@ describe('[rd3g-graph] graph tests', function() {
116116

117117
describe('when clicking a node', function() {
118118
before(function() {
119-
this.sandboxPO = new SandboxPO();
120119
// visit sandbox
121120
cy.visit(`${SANDBOX_URL}?data=small`);
122121
// sleep 2 seconds
123122
cy.wait(2000);
124123
// pause the graph
125124
this.sandboxPO.pauseGraph();
126125

127-
cy.contains('collapsible').scrollIntoView();
128-
this.sandboxPO.getFieldInput('collapsible').click();
129-
130126
this.node1PO = new NodePO(1);
131-
this.node2PO = new NodePO(2);
132-
this.node3PO = new NodePO(3);
133-
this.node4PO = new NodePO(4);
134-
this.link12PO = new LinkPO(0);
135127
});
136128

137-
it('should collapse leaf nodes', function() {
138-
const line = this.link12PO.getLine();
129+
it('should trigger alert with message containing node id', async function() {
130+
// stub window.alert
131+
const stub = cy.stub();
132+
133+
cy.on('window:alert', stub);
139134

140-
// Check the leaf node & link is present
141-
this.node2PO.getPath().should('be.visible');
142-
line.should('be.visible');
135+
// Click node 1
136+
this.node1PO
137+
.getPath()
138+
.click()
139+
.then(() => {
140+
expect(stub.getCall(0)).to.be.calledWith('laraalarla');
141+
});
142+
});
143143

144-
// Click 'Node 1' in order to collapse the leafs
145-
this.node1PO.getPath().click();
144+
describe('and graph is collapsible', function() {
145+
before(function() {
146+
['node.renderLabel', 'collapsible'].forEach(formKey => {
147+
cy.contains(formKey).scrollIntoView();
148+
this.sandboxPO.getFieldInput(formKey).click();
149+
});
146150

147-
// Check the leaf node & link is no longer visible
148-
this.node2PO.getPath().should('not.be.visible');
149-
line.should('not.be.visible');
151+
this.node1PO = new NodePO(1);
152+
this.node2PO = new NodePO(2);
153+
this.node3PO = new NodePO(3);
154+
this.node4PO = new NodePO(4);
155+
this.link12PO = new LinkPO(0);
156+
});
150157

151-
// Check if other nodes and links are still visible
152-
this.node1PO.getPath().should('be.visible');
153-
this.node3PO.getPath().should('be.visible');
154-
this.node4PO.getPath().should('be.visible');
158+
it('should collapse leaf nodes', function() {
159+
const line = this.link12PO.getLine();
155160

156-
const link13PO = new LinkPO(1);
157-
const link14PO = new LinkPO(2);
158-
const link34PO = new LinkPO(3);
161+
// Check the leaf node & link is present
162+
this.node2PO.getPath().should('be.visible');
163+
line.should('be.visible');
159164

160-
link13PO.getLine().should('be.visible');
161-
link14PO.getLine().should('be.visible');
162-
link34PO.getLine().should('be.visible');
165+
// Click 'Node 1' in order to collapse the leafs
166+
this.node1PO.getPath().click();
167+
168+
// Check the leaf node & link is no longer visible
169+
this.node2PO.getPath().should('not.be.visible');
170+
line.should('not.be.visible');
171+
172+
// Check if other nodes and links are still visible
173+
this.node1PO.getPath().should('be.visible');
174+
this.node3PO.getPath().should('be.visible');
175+
this.node4PO.getPath().should('be.visible');
176+
177+
const link13PO = new LinkPO(1);
178+
const link14PO = new LinkPO(2);
179+
const link34PO = new LinkPO(3);
180+
181+
link13PO.getLine().should('be.visible');
182+
link14PO.getLine().should('be.visible');
183+
link34PO.getLine().should('be.visible');
184+
});
163185
});
164186
});
165187
});

src/components/graph/Graph.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export default class Graph extends React.Component {
185185
* @param {Function} [cb] - optional callback to fed in to {@link setState()|https://reactjs.org/docs/react-component.html#setstate}.
186186
* @returns {undefined}
187187
*/
188-
_tick = (state = {}, cb) => (cb ? this.setState(state) : this.setState(state, cb));
188+
_tick = (state = {}, cb) => (cb ? this.setState(state, cb) : this.setState(state));
189189

190190
/**
191191
* Configures zoom upon graph with default or user provided values.<br/>

0 commit comments

Comments
 (0)