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 {
189
189
targetNode . _collapsed
190
190
? this . expandNode ( targetNode )
191
191
: this . collapseNode ( targetNode ) ;
192
- this . setState ( { data } ) ;
192
+ this . setState ( { data } , this . props . onClick ) ;
193
193
}
194
194
}
195
195
@@ -291,6 +291,7 @@ export default class Tree extends React.Component {
291
291
}
292
292
293
293
Tree . defaultProps = {
294
+ onClick : undefined ,
294
295
orientation : 'horizontal' ,
295
296
translate : { x : 0 , y : 0 } ,
296
297
pathFunc : 'diagonal' ,
@@ -322,6 +323,7 @@ Tree.defaultProps = {
322
323
323
324
Tree . propTypes = {
324
325
data : PropTypes . array . isRequired ,
326
+ onClick : PropTypes . func ,
325
327
orientation : PropTypes . oneOf ( [
326
328
'horizontal' ,
327
329
'vertical' ,
Original file line number Diff line number Diff line change @@ -5,58 +5,9 @@ import { shallow, mount } from 'enzyme';
5
5
import Node from '../../Node' ;
6
6
import Link from '../../Link' ;
7
7
import Tree from '../index' ;
8
+ import { mockData , mockData2 } from './mockData' ;
8
9
9
10
describe ( '<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
-
60
11
jest . spyOn ( Tree . prototype , 'generateTree' ) ;
61
12
jest . spyOn ( Tree . prototype , 'assignInternalProperties' ) ;
62
13
jest . spyOn ( Tree . prototype , 'handleNodeToggle' ) ;
@@ -243,4 +194,19 @@ describe('<Tree />', () => {
243
194
expect ( zoomableComponent . find ( '.rd3t-tree-container' ) . hasClass ( 'rd3t-grabbable' ) ) . toBe ( true ) ;
244
195
expect ( nonZoomableComponent . find ( '.rd3t-tree-container' ) . hasClass ( 'rd3t-grabbable' ) ) . toBe ( false ) ;
245
196
} ) ;
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
+ } ) ;
246
212
} ) ;
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