Skip to content

Commit 4949402

Browse files
justinsisleybkrem
authored andcommitted
Ensures onClick cb fires on non-collapsible tree (#25)
* Fix for #24 * Added unit test for #24
1 parent e8eacf3 commit 4949402

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Tree/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,17 @@ export default class Tree extends React.Component {
185185
* @return {void}
186186
*/
187187
handleNodeToggle(nodeId) {
188+
const data = clone(this.state.data);
189+
const matches = this.findNodesById(nodeId, data, []);
190+
const targetNode = matches[0];
191+
188192
if (this.props.collapsible) {
189-
const data = clone(this.state.data);
190-
const matches = this.findNodesById(nodeId, data, []);
191-
const targetNode = matches[0];
192193
targetNode._collapsed
193194
? this.expandNode(targetNode)
194195
: this.collapseNode(targetNode);
195196
this.setState({ data }, () => this.handleOnClickCb(targetNode));
197+
} else {
198+
this.handleOnClickCb(targetNode);
196199
}
197200
}
198201

src/Tree/tests/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ describe('<Tree />', () => {
211211
});
212212

213213

214+
it('calls the onClick callback when `props.collapsible` is false', () => {
215+
const onClickSpy = jest.fn();
216+
const renderedComponent = mount(
217+
<Tree
218+
data={mockData}
219+
collapsible={false}
220+
onClick={onClickSpy}
221+
/>
222+
);
223+
224+
renderedComponent.find(Node).first().simulate('click');
225+
226+
expect(onClickSpy).toHaveBeenCalledTimes(1);
227+
});
228+
229+
214230
it('clones the clicked node\'s data & passes it to the onClick callback if defined', () => {
215231
const onClickSpy = jest.fn();
216232
const renderedComponent = mount(

0 commit comments

Comments
 (0)