File tree Expand file tree Collapse file tree 3 files changed +73
-51
lines changed Expand file tree Collapse file tree 3 files changed +73
-51
lines changed Original file line number Diff line number Diff line change @@ -189,7 +189,7 @@ export default class Tree extends React.Component {
189189 targetNode . _collapsed
190190 ? this . expandNode ( targetNode )
191191 : this . collapseNode ( targetNode ) ;
192- this . setState ( { data } ) ;
192+ this . setState ( { data } , this . props . onClick ) ;
193193 }
194194 }
195195
@@ -291,6 +291,7 @@ export default class Tree extends React.Component {
291291}
292292
293293Tree . defaultProps = {
294+ onClick : undefined ,
294295 orientation : 'horizontal' ,
295296 translate : { x : 0 , y : 0 } ,
296297 pathFunc : 'diagonal' ,
@@ -322,6 +323,7 @@ Tree.defaultProps = {
322323
323324Tree . propTypes = {
324325 data : PropTypes . array . isRequired ,
326+ onClick : PropTypes . func ,
325327 orientation : PropTypes . oneOf ( [
326328 'horizontal' ,
327329 'vertical' ,
Original file line number Diff line number Diff line change @@ -5,58 +5,9 @@ import { shallow, mount } from 'enzyme';
55import Node from '../../Node' ;
66import Link from '../../Link' ;
77import Tree from '../index' ;
8+ import { mockData , mockData2 } from './mockData' ;
89
910describe ( '<Tree />' , ( ) => {
10- const mockData = [
11- {
12- name : 'Top Level' ,
13- parent : 'null' ,
14- attributes : {
15- keyA : 'val A' ,
16- keyB : 'val B' ,
17- keyC : 'val C' ,
18- } ,
19- children : [
20- {
21- name : 'Level 2: A' ,
22- parent : 'Top Level' ,
23- attributes : {
24- keyA : 'val A' ,
25- keyB : 'val B' ,
26- keyC : 'val C' ,
27- } ,
28- } ,
29- {
30- name : 'Level 2: B' ,
31- parent : 'Top Level' ,
32- } ,
33- ] ,
34- } ,
35- ] ;
36-
37- const mockData2 = [
38- {
39- name : 'Top Level' ,
40- parent : 'null' ,
41- attributes : {
42- keyA : 'val A' ,
43- keyB : 'val B' ,
44- keyC : 'val C' ,
45- } ,
46- children : [
47- {
48- name : 'Level 2: A' ,
49- parent : 'Top Level' ,
50- attributes : {
51- keyA : 'val A' ,
52- keyB : 'val B' ,
53- keyC : 'val C' ,
54- } ,
55- } ,
56- ] ,
57- } ,
58- ] ;
59-
6011 jest . spyOn ( Tree . prototype , 'generateTree' ) ;
6112 jest . spyOn ( Tree . prototype , 'assignInternalProperties' ) ;
6213 jest . spyOn ( Tree . prototype , 'handleNodeToggle' ) ;
@@ -243,4 +194,19 @@ describe('<Tree />', () => {
243194 expect ( zoomableComponent . find ( '.rd3t-tree-container' ) . hasClass ( 'rd3t-grabbable' ) ) . toBe ( true ) ;
244195 expect ( nonZoomableComponent . find ( '.rd3t-tree-container' ) . hasClass ( 'rd3t-grabbable' ) ) . toBe ( false ) ;
245196 } ) ;
197+
198+
199+ it ( 'calls the onClick callback when a node is toggled' , ( ) => {
200+ const onClickSpy = jest . fn ( ) ;
201+ const renderedComponent = mount (
202+ < Tree
203+ data = { mockData }
204+ onClick = { onClickSpy }
205+ />
206+ ) ;
207+
208+ renderedComponent . find ( Node ) . first ( ) . simulate ( 'click' ) ;
209+
210+ expect ( onClickSpy ) . toHaveBeenCalledTimes ( 1 ) ;
211+ } ) ;
246212} ) ;
Original file line number Diff line number Diff line change 1+ const mockData = [
2+ {
3+ name : 'Top Level' ,
4+ parent : 'null' ,
5+ attributes : {
6+ keyA : 'val A' ,
7+ keyB : 'val B' ,
8+ keyC : 'val C' ,
9+ } ,
10+ children : [
11+ {
12+ name : 'Level 2: A' ,
13+ parent : 'Top Level' ,
14+ attributes : {
15+ keyA : 'val A' ,
16+ keyB : 'val B' ,
17+ keyC : 'val C' ,
18+ } ,
19+ } ,
20+ {
21+ name : 'Level 2: B' ,
22+ parent : 'Top Level' ,
23+ } ,
24+ ] ,
25+ } ,
26+ ] ;
27+
28+ const mockData2 = [
29+ {
30+ name : 'Top Level' ,
31+ parent : 'null' ,
32+ attributes : {
33+ keyA : 'val A' ,
34+ keyB : 'val B' ,
35+ keyC : 'val C' ,
36+ } ,
37+ children : [
38+ {
39+ name : 'Level 2: A' ,
40+ parent : 'Top Level' ,
41+ attributes : {
42+ keyA : 'val A' ,
43+ keyB : 'val B' ,
44+ keyC : 'val C' ,
45+ } ,
46+ } ,
47+ ] ,
48+ } ,
49+ ] ;
50+
51+ export {
52+ mockData ,
53+ mockData2 ,
54+ } ;
You can’t perform that action at this time.
0 commit comments